按键改变文本数据
按下按键,改变文本控件的显示
- 页面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文件,按键按下改变文本
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("zhangsan");
}
}
按下按键,每隔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;
3. 处理消息
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("zhangdan" + 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
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="#000"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="40dp"
android:textColor="#fff"
android:textStyle="bold"
android:text="10s" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="testFunc"
android:text="begin" />
</RelativeLayout>
package com.example.wt.dzz;
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.textView);
h = new Handler() { // UI主线程家里的电话,接到电话,去处理起他线程无法处理的事件
@Override
public void handleMessage(Message msg) { // 区分事件的类型
// TODO Auto-generated method stub
super.handleMessage(msg);
textView.setText(msg.what + "s");
}
};
}
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();
msg.what = i;
// 打电话,去把UI要显示,处理的事件交给UI线程的Handler
h.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
}