Reputation: 169
In my application , i have 3 buttons A,B,C . on the press of button A, i am doing some computations that takes some time ... in that period , i want to disable the other two buttons. I am providing the code for the onClick listener for one button..
case R.id.buttona:
//Disabling other two buttons
start1b.setVisibility(v.INVISIBLE);
start1c.setVisibility(v.INVISIBLE);
stop1.setVisibility(v.INVISIBLE);
//this is the process that takes time
String x ="/databank/Reading18.wav";
timedata1a = fe.returningtimedata(x);
rawdata1a = fe.returningrawdata(x);
Log.d("now press", "button");
//features of Reading 1 hav been extracted into timedata1a
start1b.setVisibility(v.VISIBLE);
start1c.setVisibility(v.VISIBLE);
stop1.setVisibility(v.VISIBLE);
break;
but when i press 1 button A and then immediately press button B , the application force closes .. can any1 help ??
Upvotes: 0
Views: 786
Reputation: 29199
you need to perform operations which you want to perform in disabled state in other thread than UI. cause event thread never gets time to disable the buttons. so You can do following:
case R.id.buttona:
//Disabling other two buttons
start1b.setVisibility(v.INVISIBLE);
start1c.setVisibility(v.INVISIBLE);
stop1.setVisibility(v.INVISIBLE);
Thread thread=new Thread()
{
public void run()
{
//this is the process that takes time
String x ="/databank/Reading18.wav";
timedata1a = fe.returningtimedata(x);
rawdata1a = fe.returningrawdata(x);
Log.d("now press", "button");
handler.sendEmptyMessage(1);
}
}
thread.start();
break;
}
You need to define a handler to post results back to UI thread after thread processiing completes.
Handler handler=new Handler();
{
public void handleMessage(Message msg)
{
int what=msg.what;
switch(what)
{
case 1:
{
//features of Reading 1 hav been extracted into timedata1a
start1b.setVisibility(v.VISIBLE);
start1c.setVisibility(v.VISIBLE);
stop1.setVisibility(v.VISIBLE);
}
break;
.........
}
}
};
Upvotes: 1
Reputation: 584
In your case if you want to disable Buttons B,C when you perform onClick() operation on Button A
btnA.setOnClickListerner(new OnClickListerner() {
public void onClick(){
btnB.setEnabled(false);
btnC.setEnabled(false);
}
});
In the above code when you click on Button A The Button B,C get Disabled.
Upvotes: 0