刷新函数内的视图
我正在尝试在 Android 上的函数内执行一些步骤。我想告诉用户在特定时刻发生了什么而不退出我的函数。类似的东西:
public boolean updateServiceList() {
LinearLayout start = (LinearLayout) findViewById(R.id.start);
LinearLayout major = (LinearLayout) findViewById(R.id.major);
TextView messenger = (TextView) findViewById(R.id.messenger);
Integer i = 0;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isOnline = cm.getActiveNetworkInfo().isConnectedOrConnecting();
if(isOnline==false) {
Button exit = (Button) findViewById(R.id.exit_btn);
ProgressBar pg = (ProgressBar) findViewById(R.id.progress);
exit.setText("OK");
exit.setVisibility(View.VISIBLE);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
gMain.this.finish();
}
});
pg.setVisibility(View.GONE);
messenger.setText(R.string.nointernet);
while(isOnline==false) {
i++;
messenger.setText(messenger.getText()+"Try: "+ i.toString);
boolean isOnline = cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
return false;
} else {
isOnline = cm.getActiveNetworkInfo().isConnected();
while (isOnline==false || 1==1) {
isOnline = cm.getActiveNetworkInfo().isConnected();
}
}
return true;
}
我的问题是在“while(isOnline)”中我看不到更新消息。我在那里尝试了 invalidate() 和 postInvalidate() 但没有结果。有什么想法吗?
编辑:
我找到了解决方案!就在那里: 如何在 Android 中循环时刷新 TextView?
Rackers 的答案是:
public class myClass extends Activity{
private Handler mHandler;
private TextView text;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
text = (TextView) findViewById(R.id.textView01);
i = 0;
mHandler = new Handler();
mHandler.post(mUpdate);
}
private Runnable mUpdate = new Runnable() { public void run() {
text.setText("My number: " + i);
i++;
mHandler.postDelayed(this, 1000);
}
};}
因此,就我而言,如果当前问题得到解决,我只需调用其他步骤。如果没有的话,我只需再次发布该步骤...
I'm trying perform some steps inside a function on Android. I would like to tell to user what is happen in a specific moment without exit of my function. something like it:
public boolean updateServiceList() {
LinearLayout start = (LinearLayout) findViewById(R.id.start);
LinearLayout major = (LinearLayout) findViewById(R.id.major);
TextView messenger = (TextView) findViewById(R.id.messenger);
Integer i = 0;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isOnline = cm.getActiveNetworkInfo().isConnectedOrConnecting();
if(isOnline==false) {
Button exit = (Button) findViewById(R.id.exit_btn);
ProgressBar pg = (ProgressBar) findViewById(R.id.progress);
exit.setText("OK");
exit.setVisibility(View.VISIBLE);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
gMain.this.finish();
}
});
pg.setVisibility(View.GONE);
messenger.setText(R.string.nointernet);
while(isOnline==false) {
i++;
messenger.setText(messenger.getText()+"Try: "+ i.toString);
boolean isOnline = cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
return false;
} else {
isOnline = cm.getActiveNetworkInfo().isConnected();
while (isOnline==false || 1==1) {
isOnline = cm.getActiveNetworkInfo().isConnected();
}
}
return true;
}
My problem is in "while(isOnline)" where I can't see the update messages. I tried invalidate() and postInvalidate() there but no results. Any ideas?
EDIT:
I found the solution! There it is:
How to refresh a TextView while looping in Android?
The Rackers's answer:
public class myClass extends Activity{
private Handler mHandler;
private TextView text;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
text = (TextView) findViewById(R.id.textView01);
i = 0;
mHandler = new Handler();
mHandler.post(mUpdate);
}
private Runnable mUpdate = new Runnable() {
public void run() {
text.setText("My number: " + i);
i++;
mHandler.postDelayed(this, 1000);
}
};}
So, at my case I just call the others steps if the current is solved. If not, I just post the step again...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方法不会起作用。
当您调用messenger.setText()时,您认为屏幕应该立即更新。你错了。
当您调用
messenger.setText()
时,实际发生的情况是消息进入消息队列,由主应用程序线程处理。这与您与while()
循环绑定的线程相同。当您处于while()
循环中时,您的整个 UI 都会被冻结:没有点击,没有更新,什么也没有。我建议您摆脱
while()
循环并与应用程序的其余部分一起移动。Your approach will not work.
When you call
messenger.setText()
, you think the screen is supposed to update right then. You are mistaken.What really happens when you call
messenger.setText()
is that a message goes on a message queue, to be processed by the main application thread. That's the same thread you're tying up with yourwhile()
loop. While you are in thatwhile()
loop, your entire UI is frozen: no clicks, no updates, nothing.I suggest that you get rid of the
while()
loop and move along with the rest of your app.