I am puzzled with how to use Async. I am wanting it to update the UI as soon as a button is clicked without having to kill the activity and re instantiate it. In the background, I want it to be waiting for the click to update the UI, and after the click I want it to determine whether or not the correct button was clicked. I am using the boolean isUserRight(); to determine whether or not the answer is correct, and after that I am using resetButtons(); to reset the buttons (obviously) and then setButtons(); to change the button. I have the methods all written out, I just don't know how to implement them into Async. I need the UI to be updated instantly to change the color of a button. I don't need help with any of the methods, button color, etc, just the Async, and I'm not asking anybody to spoonfeed me either, just give me a little push on where to go. Any help would be nice, thanks guys!
TL;DR need to update UI after button is pushed using async so the button color doesn't get stuck and I don't have to restart the activity to update the UI. I don't need the whole code, just a little guidance on where to go.
Edit: Added some code.
This is on my onCreate();
buttons[0] = (Button)findViewById(R.id.b0);
buttons[1] = (Button)findViewById(R.id.b1);
buttons[2] = (Button)findViewById(R.id.b2);
buttons[3] = (Button)findViewById(R.id.b3);
buttons[0].setOnClickListener(this);
buttons[1].setOnClickListener(this);
buttons[2].setOnClickListener(this);
buttons[3].setOnClickListener(this);
then under my onClick, I have a switch for the buttons which all go to this method:
public boolean isUserRight() {
if(correct == 1) {
onButton();
return true;
//Toast.makeText(getApplicationContext(), "Good job!", Toast.LENGTH_SHORT).show();
//buttons[test].getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
}
onButton();
return false;
}
public void onButton() {
int rnd = new Random().nextInt(buttons.length);
test = rnd;
buttons[rnd].getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
//ResetButtons();
}
And this works fine, the only problem is after the first button being clicked, the colors stop changing and getting updated.
void test(){
final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what == 0){
// update your ui
}
}
};
new Thread(new Runnable() {
public void run() {
// do network job
// do something you want here
mHandler.sendEmptyMessage(0);
}
}).start();
}
Alright, I fixed it. Thanks for all your help, to anybody who was wondering how I fixed it, I was using view.invalidate, which wasn't directed to anything so I had to change it to the buttons array ex.:
buttons[1].invalidate(); and that would refresh the buttons instead of the entire activity.
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.