本篇简单Android
里面更新UI
的四种方法。
更新UI简介
Android
更新UI
主要是主线程进行更新,即UI
线程更新。如果在主线程外更新则会报错。
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
- 只有创建这个视图层次的线程才能修改它的视图
Handler
消息传递
package com.example.chenjy.chenjy;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv_token;
Handler handler = new Handler()
{
public void handleMessage(Message msg) {
if(msg.what==0x12)
{
tv_token.setText("update UI");
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new MyThread().start();
}
public void initView(){
tv_token = (TextView)findViewById(R.id.tv_token);
}
class MyThread extends Thread
{
@Override
public void run() {
//延迟两秒更新
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessage(0x12);
}
}
}
使用runOnUiThread
package com.example.chenjy.chenjy;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv_token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new MyThread().start();
}
public void initView(){
tv_token = (TextView)findViewById(R.id.tv_token);
}
class MyThread extends Thread
{
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
//延迟两秒更新
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tv_token.setText("update UI");
}
});
}
}
}
Handler
的post(Runnable runnable)
package com.example.chenjy.chenjy;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv_token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_token = (TextView) findViewById(R.id.tv_token);
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
try {
//延迟两秒更新
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tv_token.setText("update UI");
}
});
}
}
AsyncTask
异步任务
package com.example.chenjy.chenjy;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv_token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_token = (TextView) findViewById(R.id.tv_token);
new UpdateUI().execute();
}
class UpdateUI extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tv_token.setText("update UI");
}
}
}