如何反复更新的android TextView的模拟动画 [英] how to repeatedly update android TextView to simulate animation

查看:218
本文介绍了如何反复更新的android TextView的模拟动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个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. Handlers 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>

这篇关于如何反复更新的android TextView的模拟动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆