我正在使用Thread
来更新用户界面
我有一个基本的注册表单,在输入地址时要求 State 和 Country 。我在最终屏幕上显示了这两个值的摘要。
现在,有一个Edit
按钮可以编辑我想在相应字段中检索 State 和 Country 值的信息,因此我就是使用以下方法。
但它的工作不一致,即有时候两者都被填充,有时是其中一部分,有时甚至都没有。
请注意我在我的应用程序中使用ViewFlipper
(我不知道它是否有任何关系)。
我从头开始敲打我的头,但没有找到任何解决方案/建议。
任何帮助表示赞赏。
Thread thread = new Thread() {
public void run() {
while (true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
setState.setText(dispState.getText());
setCountry.setText(dispCoun.getText());
}
});
constants.threadCount =0;
try {
if (constants.threadCount == 0)
Thread.sleep(500);
constants.threadCount = 1;
} else {
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// DO SOMETHING
// Starting Thread
vf.setDisplayedChild(9);
}
});
答案 0 :(得分:0)
以下为我工作:
constants.threadCount = 0;
final Handler handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
if (constants.threadCount <4) {
setState.setText(dispState.getText());
setCountry.setText(dispCoun.getText());
handler.postDelayed(this, 1000);
constants.threadCount++;
}
}
};
Thread threadnew = new Thread()
{
@Override
public void run() {
try {
if (constants.threadCount <4) {
sleep(1000);
handler.post(r);
constants.threadCount++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
threadnew.start();