我想在一个TextView不断调整一次按钮,在菜单中的文本pssed $ P $。这是我的菜单的onOptionsItemSelected:
I would like the text in a TextView to constantly change once a button in the menu is pressed. Here is my menu's onOptionsItemSelected:
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_animate:
for(int i=0;i<100;i++){
MessageIn.setText("here"+i);
}
break;
}
return super.onOptionsItemSelected(item);
}
正如你所看到的,如果onOptionsItemSelected认为menu_animate按钮是$ pssed在P菜单$,它会在修改我在TextView中被称为MessageIn的文字。
As you can see, if onOptionsItemSelected thinks the "menu_animate" button was pressed in the menu, it goes on to modify the text in my TextView called "MessageIn".
我试图让它一再显示了一些计数从0到99。然而,当我运行应用程序和preSS的按钮,我只看到我的TextView中显示here99一些文字。因此,它看起来像所有的code显示最终结果之前执行。
I've tried to get it to repeatedly display some text with a number counting up from 0 to 99. However when I run the app and press the button, I only see "here99" displayed in my TextView. So it looks like all of the code is executed before the final result is displayed.
如何才能得到它,每次刷新显示在TextView中我将更新的数量?
How can I get it to refresh the display in the TextView each time the number i is updated?
感谢。
您可以使用处理程序
和的Runnable
你的目的。我共享一个编辑code我目前使用,就像是你的目的。
You can use a Handler
and a Runnable
for your purpose. I'm sharing an edited code which I currently use just like a purpose of yours.
您可以直接复制类和布局,那就试试吧。
You can directly copy the class and layout, then try it.
主要思想是使用处理程序
及其后
和 postDelayed
方法与的Runnable
。当您的休闲区的按钮时,它开始每3秒对修改文本。如果你再次按下按钮,它复原。
Main idea is using a Handler
with its post
and postDelayed
methods with a Runnable
. When you pust the button, it start to change text on every 3 seconds. If you push the button again it resets itself.
试试这个code,试着去了解它。 处理程序
S能用于多种目的来改变用户界面。了解更多关于它。
Try this code, try to understand it. Handler
s can be used for many purpose to change UI. Read more about it.
MainActivity.java:
MainActivity.java:
public class MainActivity extends Activity {
private Handler handler;
private TextView textView;
private TextUpdateRunnable textUpdateRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
textView = (TextView)findViewById(R.id.textView);
Button startButton = (Button)findViewById(R.id.button);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(textUpdateRunnable != null) {
handler.removeCallbacks(textUpdateRunnable);
}
TextUpdateRunnable newTextUpdateRunnable = new TextUpdateRunnable(handler, textView);
textUpdateRunnable = newTextUpdateRunnable;
handler.post(textUpdateRunnable);
}
});
}
private static class TextUpdateRunnable implements Runnable {
private static final int LIMIT = 5;
private int count = 0;
private Handler handler;
private TextView textView;
public TextUpdateRunnable(Handler handler, TextView textView) {
this.handler = handler;
this.textView = textView;
}
public void run() {
if(textView != null) {
textView.setText("here" + count);
count++;
if(handler != null && count < LIMIT) {
handler.postDelayed(this, 3000);
}
}
}
};
}
activity_main.xml:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>