简体   繁体   中英

After onCreate Android

I'm new to android and I want to do a simple example where I click on a start button and another Activity is open, there a simple number starting in one and counting upwards, but I'm facing a problem, after I initialize some variables on onCreate method (In the second activity), where should I actually start the while statement to count and modify the text view?.

I wrote this class:

public class Counter extends Thread{

    private TextView tv;
    private int i;


    public Counter( TextView tv ){
        this.tv = tv;
    }

    @Override
    public void run() {
        while( true ){
            tv.setText(i);
            try {
                sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            i++;
        }
    } 

And started the thread over here:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
            counter = new Counter( (TextView) findViewById(R.id.textView2) );
            counter.start( );
}
@SuppressLint("UseValueOf")
final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                // change your text here
                if (condition) {
                    i++;
                    txt_TimeRecord.setText("" + i);
                } else {
                    i = 0;
                }
                handler.postDelayed(this, 1 * 1000L);
            }
        });

[Formatted the code properly]

//In first activity 
//set onclick method to that button.

public void onclick(view v)
{ 
        Intent intent = new Intent(this, secondactivity.class); 
        startActivity(intent);
}  

// in second activity in
// initi i value. 

while(i<10) 
{ 
  i++ ; 

 // sleep statement  
 //print that i in textview

}

I've found what I need from a similar post, the handler was needed since only the UI Thread can update the user interface.

private void countNumbers() {
    final TextView numbers = (TextView) findViewById(R.id.textView2);


    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
            int i = 0;
            while (i++ < 500) {  
                try {
                    Thread.sleep(10);
                }    
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                final int j = i;
                handler.post(new Runnable(){
                    public void run() {
                        numbers.setText(Integer.toString(j));
                }
            });
            }
        }
    };
    new Thread(runnable).start();
}

The method countNumbers() is called in onCreate().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM