使用android中的单选按钮启用/禁用edittext

时间:2017-02-07 10:33:07

标签: android

我是android新手。我有两个单选按钮和编辑文本。检查单选按钮,它禁用编辑文本并防止输入板出现,并检查单选按钮2启用编辑文本...我尝试了很多答案但没有任何作用。 这是我的代码

public  class MainActivity extends AppCompatActivity {

    RadioButton radioButton, radioButton2;
    EditText editText;
    RadioGroup radioGroup;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioButton = (RadioButton) findViewById(R.id.radioButton);
        radioButton.setChecked(true);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        if ((radioButton.isChecked())) {
            editText.setEnabled(false);
            editText.setInputType(InputType.TYPE_NULL);
            editText.setFocusableInTouchMode(false);
        } 
        else {
            editText.setEnabled(false);
            editText.setFocusableInTouchMode(false);
        }
    }
}

4 个答案:

答案 0 :(得分:3)

嘿,先把两个单选按钮放在一个Radio组。

检查此代码。

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            switch(checkedId)
            {
            case R.id.radio0:
                editText.setEnabled(false);
                editText.setInputType(InputType.TYPE_NULL);
                editText.setFocusableInTouchMode(false);
                break;
            case R.id.radio1:
                 editText.setEnabled(false);
                 editText.setFocusableInTouchMode(false);
                break;
            }
        }
    });

希望这有帮助。快乐的编码。

答案 1 :(得分:0)

使用setOnCheckedChangeListener的方法并管理checked和uncheck事件。

答案 2 :(得分:0)

你应该删除这部分代码:

if ((radioButton.isChecked())) {
    editText.setEnabled(false);
    editText.setInputType(InputType.TYPE_NULL);
    editText.setFocusableInTouchMode(false);
} else {
    editText.setEnabled(false);
    editText.setFocusableInTouchMode(false);
}

并将其替换为:

radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            editText.setEnabled(false);
            editText.setInputType(InputType.TYPE_NULL);
            editText.setFocusableInTouchMode(false);
        } else {
            editText.setEnabled(false);
            editText.setFocusableInTouchMode(false);
        }  
    }
});

如果您使用CheckBox而不是RadioButton,则此解决方案会更好,因为您的需求与CheckBox更兼容。

答案 3 :(得分:0)

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch(checkedId)
        {
            case R.id.radio0:
                editText.setEnabled(false);
                editText.setInputType(InputType.TYPE_NULL);
                editText.setFocusableInTouchMode(false);
                break;
            case R.id.radio1:
                editText.setEnabled(false);
                editText.setInputType(InputType.TYPE_CLASS_TEXT);
                editText.setFocusableInTouchMode(false);
                break;
        }
    }
});
相关问题