开发者

Block the other buttons after one button was pressed

In m开发者_StackOverflow社区y activity I have many buttons that launch other activities. The problem is that when I press a button I don't want to be able to press another, and so to launch 2 activities (or more). What is the best solution to block the other views (buttons) after one of them was pressed?


Just in each of your onClickListener for each button make button.setDisable(true);. Don't forget to enable them in onResume().


Add one onClickListener() for all buttons. In listener switch the actions as per id.

public .. onClickListener(){
// Disable all buttons here..
button1.setDisabled(true);
button2.setDisabled(true);
button3.setDisabled(true);
...
switch(view.getId()){
 case R.id.button1:
// Start some activiy ... or do some task...
break;
 case R.id.button2:
...
break;. ...

}

Don't forget to enable all buttons in onResume()

public .. onResume(){
button1.setDisabled(true);
    button2.setDisabled(true);
    button3.setDisabled(true);
}

OR, Try This to enable/disable all views in a viewGroup...

private void enableDisableView(View view, boolean enabled) {
    view.setEnabled(enabled);

    if ( view instanceof ViewGroup ) {
        ViewGroup group = (ViewGroup)view;

        for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
            enableDisableView(group.getChildAt(idx), enabled);
        }
    }
}

Pass the view parameter as the parent layout containing all buttons.

I haven't used this, but this should work. Try playing around this code.


Try this to disable the Buttons

button.setEnabled(false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜