为什么我的Toggle按钮需要双击才能取消选择它是否在Activity onCreate上被选中。

时间:2015-12-14 05:30:38

标签: android android-togglebutton

我已经从Button类创建了两个toogle按钮。

标准

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

工作正常,

但小问题,

当活动加载按钮处于选定状态时,第一次取消选择它需要双击,为什么?

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

这是我的全球声明。

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));
                        }
                    }
                }
            });

2 个答案:

答案 0 :(得分:0)

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

public class Employee
{
    public string empName { get; set; }
    public string VisitDate { get; set; }
    public string VisitTime { get; set; }
}

public class EmployeeInfo
{
    public ObservableCollection<Employee> EmployeeList { get; set; }

    public EmployeeInfo()
    {
        EmployeeList = new ObservableCollection<Employee>();
        EmployeeList.Add(new Employee { empName = "John", VisitDate = "11/28/2015", VisitTime = "05:12 PM" });
        EmployeeList.Add(new Employee { empName = "Potter", VisitDate = "10/28/2015", VisitTime = "04:33 PM" });
        EmployeeList.Add(new Employee { empName = "James", VisitDate = "11/27/2015", VisitTime = "09:12 AM" });
    }
}

答案 1 :(得分:0)

问题出在以下行

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

在maletoggle中更改此行,单击

view.setSelected(!isFemaleSelected);

并在femaletoggle中点击

view.setSelected(!isMaleSelected);