目录
1.2、按下按键,每隔1s刷新一次文本(非UI线程想要控制控件用Handler)
一、按键改变文本数据
1.1、按下按键,改变文本控件的显示
- 页面xml,添加一个文本和按键,添加ID,用onClick按键响应
<TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="37dp" android:onClick="testFunc" android:text="改变文本" />
- Activity文件,按键按下改变文本,3句
package com.example.zz; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { public TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text1); } public void testFunc(View v){ textView.setText("xiaowei"); } }
1.2、按下按键,每隔1s刷新一次文本(非UI线程想要控制控件用Handler)
- 创建新线程,每隔1s刷新一次,发现会崩
package com.example.zz; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { public TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text1); } public void testFunc(View v){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++) { textView.setText("xiaowei"+i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
- 解决上面问题就要用到一个类,Handler,用os的Handler
public Handler h;
- 处理消息
h = new Handler(){ //UI主线程的电话,接到电话,去处理其他线程无法处理的事件 @Override public void handleMessage(Message msg) {//区分事件的类型 // TODO Auto-generated method stub super.handleMessage(msg); textView.setText("xiaowei"+msg.what);//Ui线程改变控件 } };
- 新线程发送要处理的事件到Handler
Message msg = new Message();//做一个Message出来 msg.what = i; //打电话,去把UI要显示,处理的事件交给UI线程的Handler h.sendMessage(msg); //发送到Handler
- 完整代码
package com.example.zz; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { public TextView textView; public Handler h; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text1); h = new Handler(){ //UI主线程的电话,接到电话,去处理其他线程无法处理的事件 @Override public void handleMessage(Message msg) {//区分事件的类型 // TODO Auto-generated method stub super.handleMessage(msg); textView.setText("xiaowei"+msg.what);//Ui线程改变控件 } }; } public void testFunc(View v){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++) { Message msg = new Message();//做一个Message出来 msg.what = i; //打电话,去把UI要显示,处理的事件交给UI线程的Handler h.sendMessage(msg); //发送到Handler try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
二、10s倒计时APP
使横屏,去掉上面标题
//横屏,在配置文件里加上
android:screenOrientation="landscape">
//去掉上面标题,在Activity文件的setContentView(R.layout.activity_main);语句前面加上
requestWindowFeature(Window.FEATURE_NO_TITLE);
- xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" tools:context=".MainActivity" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="100dp" android:textColor="#ffffff" android:textStyle="bold" android:text="10s" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="37dp" android:onClick="testFunc" android:text="10s倒计时" /> </RelativeLayout>
- java
package com.example.zz; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { public TextView textView; public Handler h; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text1); h = new Handler(){ //UI主线程的电话,接到电话,去处理其他线程无法处理的事件 @Override public void handleMessage(Message msg) {//区分事件的类型 // TODO Auto-generated method stub super.handleMessage(msg); textView.setText(msg.what + "s");//Ui线程改变控件 } }; } public void testFunc(View v){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 10; i >= 0; i--) { Message msg = new Message();//做一个Message出来 msg.what = i; //打电话,去把UI要显示,处理的事件交给UI线程的Handler h.sendMessage(msg); //发送到Handler try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
三、安卓APP接收数据并刷新界面
- xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" tools:context=".MainActivity" > <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:onClick="bottonBeCliecked" android:text="开灯" /> /> <TextView android:id="@+id/te1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="40dp" android:textColor="#ffffff" android:text="" /> </RelativeLayout>
- java
package com.example.xw; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.TextView; import com.example.NetUtills.xw.NetUtills; public class MainActivity extends Activity { private Handler h; public TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.te1); h = new Handler(){ public void handleMessage(Message msg) { super.handleMessage(msg); Bundle b = msg.getData(); String string = b.getString("msg"); //取出msg里的字符串 textView.setText(string); //改变文本控件的内容 } }; } public void bottonBeCliecked(View v){ NetUtills.sendMessageHandler("on light", h); //按下按键往服务器发送一个数据,如何等待接收服务器发回的数据 } }
package com.example.NetUtills.xw; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class NetUtills { public static void sendMessageHandler(final String message, final Handler h){ new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub try { byte[] data = new byte[128]; Socket client = new Socket("192.168.1.42", 8989); OutputStream out = client.getOutputStream(); out.write(message.getBytes()); InputStream in = client.getInputStream(); int len = in.read(data); String str = new String(data,0,len); Message msg = new Message(); Bundle b = new Bundle(); b.putString("msg", str); //可以放很多类型,string int 等 msg.setData(b); //只能放Bundle h.sendMessage(msg); //只能放Message } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } }
师承上官可编程 —— 陈立臣