keep android button selected state

前端 未结 5 797
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 18:03

I know this is a question that has been asked many times before, but I can\'t seem to solve it in my code. I have two buttons, and when one is pressed, I would like to keep

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 18:21

    A checkbox could work, but if you're after a UI closer UISegmentedControl on iOS, which is possible wrapping all the buttons needed in a LinearLayout, then you could do something along the lines of:

    public void onClick( View v ){
        if ( v.getID == R.id.btn1 ) {
            btn1.setEnabled(false);
            btn2.setEnabled(true);
            // do stuff
        }
        else if ( v.getId() == R.id.btn2 ) {
            btn2.setEnabled(false);
            btn1.setEnabled(true);
            // do stuff
        }
    }
    

    Of course, in your drawable folder, you would add background colour, borders, and other visual stuff for each state.

提交回复
热议问题