Android: updating the screen during computer controlled UI changes
I've checked the other questions asked here and didn't succeed in implementing the solutions. My application uses controls, not drawing on the canvas. Basically this sums up my issue: -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (ToggleButton) findViewById(R.id.btn1);
btn2 = (ToggleButton) findViewById(R.id.btn2);
btn3 = (ToggleButton) findViewById(R.id.btn2);
btn4 = (ToggleButton) findViewById(R.id.btn4);
btn5 = (ToggleButton) findViewById(R.id.btn5);
btn6 = (ToggleButton) findViewById(R.id.btn6);
btn7 = (ToggleButton) findViewById(R.id.btn7);
//blah blah blah
btn1.requestFocus();
wait(100);
btn2.requestFocus();
wait(100);
btn3.requestFocus();
wait(100);
btn3.setChecked(true);
wait(100);
btn4.requestFocus();
wait(100);
btn4.setChecked(true);
wait(100);
btn5.requestFocus();
wait(100);
btn6.requestFocus();
wait(100);
btn6.setChecked(true);
wait(100);
btn7.requestFocus();
wait(100);
//etc
}
The point being the computer animates the moving to a the buttons and checking them so the user can see what it is doing. So, my issue is, what goes in my wait() method that will pause for 100ms and also update the screen, and the focus? Any help really appreciated, cheers Jonathan.
Solution applies, thanks Andrew :)
int timerCounterInterval = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ToggleButton btn1 = (ToggleButton) findViewById(R.id.btn1);
final ToggleButton btn2 = (ToggleButton) findViewById(R.id.btn2);
// more btns
timerCounterInterval = 0; //reset, for each pass of automation
setFocus(btn1);
setFocus(btn2);
setChecked(btn2);
// tidy up
timerCounterInterval++;
new Handler().postDelayed(new Runnable() {
public void run() {
btn2.clearFocus(); //whatever button that had the focus last
}
}, 100*(timerCounterInterval));
// etc - note any code from here will execute *开发者_如何学Pythonbefore* the above timer
}
public void setFocus(final ToggleButton btn) {
timerCounterInterval++;
new Handler().postDelayed(new Runnable() {
public void run() {
btn.setFocusableInTouchMode(true); //eww, messy i know
btn.requestFocus();
btn.setFocusableInTouchMode(false);
}
}, 100*timerCounterInterval);
}
public void setChecked(final ToggleButton btn) {
timerCounterInterval++;
new Handler().postDelayed(new Runnable() {
public void run() {
btn.setChecked(true);
}
}, 100*timerCounterInterval);
}
Hope this helps other people!
I highly suggest you look into the Handle object and in particular the postDelayed method. That way you can keep everything on the UI thread while not causing the thread to actually be tied up in an animate method. This also keeps the Activity from giving that dreaded this-application-is-not-responding message.
In fact, simply wrapping your calls to the control's setters in a runnable and passing them to postDelayed should be enough to update the GUI controls.
[Update] As an example per your comments you might try something like this. I haven't actually tested this but I think the idea is still there...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ToggleButton btn1 = (ToggleButton) findViewById(R.id.btn1);
final ToggleButton btn2 = (ToggleButton) findViewById(R.id.btn2);
// more btns
Handler handler = new Handler();
/* tell the handler run these bits after 1 sec, 2 sec, 3 sec, ect... */
handler.postDelayed(new Runnable() { void run () { btn1.requestFocus(); } }, 1000);
handler.postDelayed(new Runnable() { void run () { btn2.requestFocus(); } }, 2000);
handler.postDelayed(new Runnable() { void run () { btn2.setChecked(true); } }, 3000);
}
精彩评论