How to make Dynamic change of TextVIew and UI in general?

烈酒焚心 提交于 2019-12-14 02:27:17

问题


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);
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/8388341/how-to-make-dynamic-change-of-textview-and-ui-in-general

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!