如何让一般的TextView和UI的动态变化? [英] How to make Dynamic change of TextVIew and UI in general?

查看:94
本文介绍了如何让一般的TextView和UI的动态变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好

所以,我想动态改变的TextView的属性。
基本上,我定义的持续时间。我想我的处理程序/可运行将文本追加到一个TextView,直到我到达的时间。

 公共类Dynamic_testActivity扩展活动
{
    公共上下文的背景下= NULL;
    公共TextView的视图= NULL;
    公共处理器mHandler = NULL;
    众长STARTTIME = 0L;
    公众最终诠释duration_millis = 10000;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        上下文= getApplicationContext();        鉴于=(的TextView)findViewById(R.id.textView1);
        view.append(\\ n);        mHandler =新的处理程序();
    }    @覆盖
    保护无效调用onStart(){
        super.onStart();        STARTTIME = System.currentTimeMillis的();        mHandler.post(新的Runnable(){
            @覆盖
            公共无效的run(){
                view.append(Hell_yeah _ \\ N!);
                // 10个字符的lenght
            }
        });
    }
}

所以,是的,它添加文本一次,因为运行这样做。
但是,我怎么可能做出某种循环,而不会阻塞UI线程,并添加文本,直到时间的尽头。

这是我们的第一步...

第二部分现在......其实,我想改变文本的颜色。
使用

  Spannable WordtoSpan =新SpannableString(view.getText());
WordtoSpan.setSpan(新ForegroundColorSpan(Color.BLUE),0,view.getText()长(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE。);

我要的颜色变化是动态的持续时间...就像一个卡拉OK ...
那么,有可能使在可运行,而无需阻塞,直到持续时间结​​束UI线程?而如何?

如果任何人都可以解释完满成功的过程?或者发布一些源$ C ​​$ C


解决。
这是一个基本的例子...
还有一个有点麻烦......在应用程序的开始,整个TextView的是黄色的,而之后的第二,它更新,它应该是显示器。
如果有任何人知道为什么,建议,欢迎=)

请注意:这里只有两个布局简单的TextView ......持续时间以毫秒......并有10个字符的在动态的TextView,以适应持续时间......因此,基本上,一个字符=片刻...

 公共类Dynamic_testActivity扩展活动
{
    公共上下文的背景下= NULL;
    公共TextView的视图= NULL;
    公共TextView的视图2 = NULL;
    公开处理程序处理程序= NULL;
    众长START_TIME,CURRENT_TIME,ELAPSED_TIME = 0L;
    公众最终诠释长度= 10000;
    公众诠释结束= 0;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        上下文= getApplicationContext();
        鉴于=(的TextView)findViewById(R.id.textView1);
        视图2 =(的TextView)findViewById(R.id.textView2);
        处理器=新的处理程序();
    }    @覆盖
    保护无效调用onStart(){
        super.onStart();        START_TIME = Long.valueOf(System.currentTimeMillis的());
        CURRENT_TIME = START_TIME;        handler.postDelayed(新的Runnable(){
            @覆盖
            公共无效的run(){                CURRENT_TIME = Long.valueOf(System.currentTimeMillis的());
                ELAPSED_TIME = Long.valueOf(CURRENT_TIME) - Long.valueOf(START_TIME);                如果(ELAPSED_TIME> =时间+ 30){
                    Toast.makeText(背景下,完成,Toast.LENGTH_LONG).show();
                    //完();
                }其他{
                    结束=(INT)(ELAPSED_TIME / 1000);
                    Spannable WordtoSpan =新SpannableString(view.getText());
                    WordtoSpan.setSpan(新ForegroundColorSpan(Color.YELLOW),0,结束Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                    view.setText(WordtoSpan);
                    view2.setText(时间:+ ELAPSED_TIME);
                    handler.postDelayed(此,10);
                }
            }
        },10);
    }
}


解决方案

在您的run()methond,你可以调用mHandler.post(本)(或使用postDelayed延迟吧)

有在API级别14性质变化的动画,但如果你是一个目标定位较低的版本,使用postDelayed repetively逐步改变文本的颜色。

Good afternoon everyone

So, I'm trying to dynamically change textview's properties. basically, I defined a Duration. and I want to my handler/runnable to append text to a textView until I reach the duration.

public class Dynamic_testActivity extends Activity
{
    public Context      context         = null;
    public TextView     view            = null;
    public Handler      mHandler        = null;
    public long         startTime       = 0L;
    public final int    duration_millis = 10000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        context = getApplicationContext();

        view    = (TextView)    findViewById(R.id.textView1);
        view.append("\n");

        mHandler = new Handler();
    }

    @Override
    protected void onStart() {
        super.onStart();

        startTime = System.currentTimeMillis();

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                view.append("Hell_yeah_!\n");
                // 10 character lenght
            }
        });
    }
}

So yes, it append the text once, because the run do so. But how could I make some kind of loop, without blocking the UI Thread, and append text until the end of the duration.

That was the first step ...

The second part now ... In fact, I Want to change the color of the text. using

Spannable WordtoSpan = new SpannableString(view.getText());        
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, view.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

I want to the color changing to be dynamic for the duration ... like a karaoke ... So, is it possible to make that in the runnable without having the UI Thread blocked until the end of the duration ? and How ?

If anyone could explain the complet process ? or post some source code


Solved. Here is a basic example... There is still a little trouble ... at the very beginning of the application, the whole textview is yellow, and after a second, it updates the display as it should be . If any one knows why, advices are welcome =)

Note : there's only two simple Textview in the layout... Duration is in milliseconds... and there is 10 character in the dynamic textview to fit the duration ... So basically, one char = one second ...

public class Dynamic_testActivity extends Activity
{
    public Context      context                                 = null;
    public TextView     view                                    = null;
    public TextView     view2                                   = null;
    public Handler      handler                                 = null;
    public long         start_time, current_time, elapsed_time  = 0L;
    public final int    duration                                = 10000;
    public int          end                                     = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        context = getApplicationContext();
        view    = (TextView)    findViewById(R.id.textView1);
        view2   = (TextView)    findViewById(R.id.textView2);
        handler = new Handler();
    }

    @Override
    protected void onStart() {
        super.onStart();

        start_time   = Long.valueOf( System.currentTimeMillis() );
        current_time = start_time;

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                current_time = Long.valueOf( System.currentTimeMillis() );
                elapsed_time = Long.valueOf(current_time) - Long.valueOf(start_time);

                if ( elapsed_time >= duration + 30 ) {
                    Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
                    //finish();
                } else {
                    end = (int) (elapsed_time / 1000);
                    Spannable WordtoSpan = new SpannableString(view.getText());
                    WordtoSpan.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                    view.setText(WordtoSpan);
                    view2.setText("time : " + elapsed_time);
                    handler.postDelayed(this, 10);
                }
            }
        }, 10);
    }
}

解决方案

In your run() methond, you can call mHandler.post(this) (or use postDelayed to delay it)

There is property change animation in API level 14, but if you are targetting a lower version, use postDelayed repetively to change progressively the color of the text.

这篇关于如何让一般的TextView和UI的动态变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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