0

我从Button课堂上创建了两个工具按钮。

标准

1) 必须选择两者中的至少一个,或者两者都可以选择。

工作正常,

不过小问题,

当 Activity 加载了处于选中状态的按钮时,第一次取消选中需要双击,为什么?

考虑下面的示例,其中两个按钮都被选中。

这是我的全球宣言。

private boolean isMaleSelected = false;
private boolean isFemaleSelected = false;

这是我在 onCreate() 中的初始化,

//Selecting both buttons
isMaleSelected = true;
isFemaleSelected = true;

这是我的两个按钮,男性和女性

 maletoggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isFemaleSelected) {

                    view.setSelected(!view.isSelected());
                isMaleSelected = view.isSelected();
                    if (view.isSelected()) {
                        view.setBackground(getResources().getDrawable(R.drawable.selected_textview));
                        ((Button) view).setTextColor(getResources().getColor(R.color.white));
                    } else {
                        view.setBackground(getResources().getDrawable(R.drawable.unselected_textview));
                        ((Button) view).setTextColor(getResources().getColor(R.color.colorPrimary));
                    }
                }
            }
        });
femaletoggle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(isMaleSelected) {
                        view.setSelected(!view.isSelected());
                    isFemaleSelected = view.isSelected();
                        if (view.isSelected()) {
                            view.setBackground(getResources().getDrawable(R.drawable.selected_textview));
                            ((Button) view).setTextColor(getResources().getColor(R.color.white));
                        } else {
                            view.setBackground(getResources().getDrawable(R.drawable.unselected_textview));
                            ((Button) view).setTextColor(getResources().getColor(R.color.colorPrimary));
                        }
                    }
                }
            });
4

2 回答 2

0

问题在下面一行

view.setSelected(!view.isSelected());

在maletoggle中更改此行单击以

view.setSelected(!isFemaleSelected);

在女性切换点击

view.setSelected(!isMaleSelected);
于 2015-12-14T05:40:56.147 回答
0

请检查以下解决方案...可能对您有帮助

    public class Example extends Activity implements OnClickListener {

    private Button button1;
    private Button button2;
    private Button button3;

    private Boolean b1,b2,b3 =  false;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.example);
        button1 = (Button) findViewById(R.id.Button1);
        button2 = (Button) findViewById(R.id.Button2);
        button3 = (Button) findViewById(R.id.button3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.Button1:
            if(b1)
            {             
               b1 = false;         
               button1.setBackground(getResources().getDrawable(R.drawable.unselected_textview));
               button1.setTextColor(getResources().getColor(R.color.colorPrimary));  

            else
             {
              b1 = true;         
              button1.setBackground(getResources().getDrawable(R.drawable.selected_textview));
              button1.setTextColor(getResources().getColor(R.color.white));

            }

            break;

           case R.id.Button2:
            if(b2)
            {             
               b2 = false;         
               button2.setBackground(getResources().getDrawable(R.drawable.unselected_textview));
               button2.setTextColor(getResources().getColor(R.color.colorPrimary));  

            else
             {
              b2 = true;         
              button2.setBackground(getResources().getDrawable(R.drawable.selected_textview));
              button2.setTextColor(getResources().getColor(R.color.white));

              }

            break;
       case R.id.Button3:
            if(b3)
            {             
               b3 = false;         
               button3.setBackground(getResources().getDrawable(R.drawable.unselected_textview));
               button3.setTextColor(getResources().getColor(R.color.colorPrimary));  

            else
             {
              b3 = true;         
              button3.setBackground(getResources().getDrawable(R.drawable.selected_textview));
              button3.setTextColor(getResources().getColor(R.color.white));

              }

            break;
        default:
            break;
        }
       }
     }
于 2015-12-14T05:33:21.903 回答