智者不为愚者谋,勇者不为怯者死。这篇文章主要讲述Android 更新UI相关的知识,希望能为你提供帮助。
本篇简单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");
}}
}
【Android 更新UI】
文章图片
推荐阅读
- Android BitmapFactory.Options 解决大图片加载OOM问题
- 考拉Android统一弹框
- Android SharedPreferences
- Spring源码解析 – AnnotationConfigApplicationContext容器创建过程
- Android-自定义仿QQ列表Item滑动
- 迈向高阶(优秀Android程序员必知必会的网络基础)
- Android PDA扫描枪广播接搜条码并使用
- 高德定位腾讯定位在APP上无法开启定位权限的解决方案
- Android开发资源收集