Android Studio:使用CheckBox设置EditText

时间:2016-01-11 21:18:20

标签: android checkbox android-edittext

在android studio中,我试图使用CheckBox来控制是否启用数字EditText。单击复选框时的相关代码:

    boolean checked = ((CheckBox) view).isChecked();
    EditText yzEdit = (EditText) findViewById(R.id.yzEdit);

    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.yzCheck:
            if (checked){
                yzEdit.setBackgroundColor(Color.parseColor("#FFFFFF"));
                yzEdit.setTextIsSelectable(true);
                yzEdit.setFocusable(true);
                yzEdit.setFocusableInTouchMode(true);
                yzEdit.setCursorVisible(true);
                yzEdit.setEnabled(true);
            }
            else{
                yzEdit.setBackgroundColor(Color.parseColor("#CCCCCC"));
                yzEdit.setTextIsSelectable(false);
                yzEdit.setFocusable(false);
                yzEdit.setFocusableInTouchMode(false);
                yzEdit.setCursorVisible(false);
                yzEdit.setEnabled(false);
            }
            break;
    }

一切都近乎有效。但是,当我选中该框,启用yzEdit,然后单击yzEdit,不会直接弹出用户数字键盘。因此,用户无法在新启用的EditText中输入任何数字(除非是通过"下一步"从以前的EditText关注它的粗略方式。

我正在寻找哪种属性来控制这种行为?

1 个答案:

答案 0 :(得分:1)

如果要检测对象是否已更改其状态,则需要在复选框上使用侦听器。

对于您的情况,它是在您的复选框上使用的setOnCheckedChangeListener()。

您可以在此处查看:http://developer.android.com/reference/android/widget/CompoundButton.html 并且: Android: checkbox listener

相关问题