本文介绍了Android的研究背景线程无法通过处理程序改变的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法理解为什么我的后台线程是无法更改的TextView的含量( txtName的)后,屏幕方向改变。
如果我做的 txtName的为静态它的工作原理,但没有静态这是行不通的。它只是它的初始值没有得到由后台线程更新。
私人TextView的txtName的;公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.screen2); txtName的=(的TextView)findViewById(R.id.txtName);
TextView的txtEmail =(的TextView)findViewById(R.id.txtEmail);
按钮btnClose =(按钮)findViewById(R.id.btnClose); 按钮btnBack =(按钮)findViewById(R.id.btnBack); 意向I = getIntent();
//接收数据
字符串名称= i.getStringExtra(名称);
字符串email = i.getStringExtra(电子邮件);
//字符串数据= i.getStringExtra(数据);
//显示接收到的数据
txtName.setText(HI);
txtEmail.setText(电子邮件); //绑定Click事件按钮
btnClose.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看为arg0){
//关闭SecondScreen活动
主题背景=新主题(新的Runnable(){
@覆盖
公共无效的run(){
对(INT I = 0; I&小于30;我++){
尝试{
视频下载(1000);
消息味精=新的Message();
束B =新包();
b.putString(我的钥匙,我的价值:+将String.valueOf(I));
msg.setData(二);
//发送消息到处理程序与当前的消息处理程序
handler.sendMessage(MSG);
Log.e(错误,螺纹);
}赶上(例外五){
Log.d(错误,e.toString());
}
}
}
});
background.start();
}
});
btnBack.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看为arg0){
意向returnIntent = getIntent();
returnIntent.putExtra(returnResult,我想要回页。);
的setResult(RESULT_OK,returnIntent);
完(); }
});}
处理程序处理程序=新的处理程序(){ 公共无效的handleMessage(消息MSG){
//获取包和关键提取数据
束B = msg.getData();
字符串键= b.getString(我的钥匙);
txtName.setText(项目+键);
txtName.setVisibility(View.VISIBLE);
Log.e(测试信息,txtName.getText()的toString());
} };
解决方案
您的UI只能由UI线程进行更新。尝试这样的事情要问你的UI线程尽快更新界面。这样,我认为你能避免使用的处理程序。
btnClose.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看为arg0){
//关闭SecondScreen活动
主题背景=新主题(新的Runnable(){
@覆盖
公共无效的run(){
对(INT I = 0; I&小于30;我++){
尝试{
视频下载(1000);
最后弦乐VAL =我的价值:+将String.valueOf(I)
//问UI线程进行更改尽快
txtName.post(新的Runnable(){
公共无效的run(){
txtName.setText(项目+ VAL);
txtName.setVisibility(View.VISIBLE);
});
}赶上(例外五){
Log.d(错误,e.toString());
}
}
}
});
background.start();
}
});
I am unable to understand why my background thread is not able to change the content of textView(txtName) after screen orientation is changed.
If I make txtname as static it works, but without static it does not work. It just has its initial value not getting updated by the background thread.
private TextView txtName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
txtName = (TextView) findViewById(R.id.txtName);
TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
Button btnClose = (Button) findViewById(R.id.btnClose);
Button btnBack = (Button) findViewById(R.id.btnBack);
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String email = i.getStringExtra("email");
// String data = i.getStringExtra("data");
// Displaying Received data
txtName.setText("HI");
txtEmail.setText(email);
// Binding Click event to Button
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
Thread background = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
try {
Thread.sleep(1000);
Message msg = new Message();
Bundle b = new Bundle();
b.putString("My Key", "My Value: " + String.valueOf(i));
msg.setData(b);
// send message to the handler with the current message handler
handler.sendMessage(msg);
Log.e("Error", "IN THREAD");
} catch (Exception e) {
Log.d("Error", e.toString());
}
}
}
});
background.start();
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = getIntent();
returnIntent.putExtra("returnResult","i want to back page.");
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
// get the bundle and extract data by key
Bundle b = msg.getData();
String key = b.getString("My Key");
txtName.setText( "Item " + key);
txtName.setVisibility(View.VISIBLE);
Log.e("TEST MESSAGE", txtName.getText().toString());
}
};
解决方案
Your UI can only be updated by the UI thread. Try something like this to ask your UI thread to update the interface as soon as possible. This way i think you can avoid using a handler.
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
Thread background = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
try {
Thread.sleep(1000);
final String val = "My Value: " + String.valueOf(i);
// ask UI thread to make the changes as soon as possible
txtName.post(new Runnable(){
public void run(){
txtName.setText( "Item " + val);
txtName.setVisibility(View.VISIBLE);
});
} catch (Exception e) {
Log.d("Error", e.toString());
}
}
}
});
background.start();
}
});
这篇关于Android的研究背景线程无法通过处理程序改变的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!