禁用与单选按钮检查EditText

问题描述:

此代码无法正常工作,错在哪里?我不希望允许用户输入直到radiobutton2检查禁用与单选按钮检查EditText

EditText t4 = (EditText)findViewById(R.id.editText3); 

RadioButton rb = (RadioButton) findViewById(R.id.radioButton1); 
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2); 
RadioGroup rg =(RadioGroup) findViewById(R.id.G1); 


    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 

      if(checkedId==R.id.radioButton1) 
      { 
       t4.setEnabled(false); 
      } 

      if(checkedId==R.id.radioButton2) 
      { 
       t4.setEnabled(true); 
      } 


     } 
    }); 
+0

在Android中禁用edittext有点麻烦。尝试添加't4.setFocusable(false);'结合't4.setEnabled(false);' –

+0

http://*.com/a/23197277/7320259希望它可以帮助您 –

t4.setEnable(rb2.isChecked()); 

rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       t4.setEnable(isChecked); 
      } 
     }); 

你可以试试下面给出的代码?

EditText t4 = (EditText)findViewById(R.id.editText3); 

RadioButton rb = (RadioButton) findViewById(R.id.radioButton1); 
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2); 
RadioGroup rg =(RadioGroup) findViewById(R.id.G1); 


rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 

     if(checkedId==R.id.radioButton1) 
     { 
      changeEditTextAvailability(t4, false); 
     } 

     if(checkedId==R.id.radioButton2) 
     { 
      changeEditTextAvailability(t4, true); 
     } 


    } 
}); 

private void changeEditTextAvailability(EditText editText, boolean status) { 
    editText.setFocusable(status); 
    editText.setEnabled(status); 
    editText.setCursorVisible(status); 
    editText.setFocusableInTouchMode(status) 
} 

干杯,

RENC

你可以试试这个

检查“单选框”被选中:isChecked()

禁用的EditText android系统:setFocusable()

EditText t4 = (EditText)findViewById(R.id.editText3); 

RadioButton rb = (RadioButton) findViewById(R.id.radioButton1); 
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2); 
RadioGroup rg =(RadioGroup) findViewById(R.id.G1); 

if (rb.isChecked()) { 
    t4.setFocusable(true); 
} else { 
    t4.setFocusable(false); 
} 

if (rb2.isChecked()) { 
    t4.setFocusable(false); 
} else { 
    t4.setFocusable(true); 
} 
+0

这项工作只有第一次检查。意思是如果一旦我尝试了两个然后t4禁用永远。再次我需要回去。你能告诉我如何编码,以便它可以工作很多次,因为我更改了单选按钮的选择? –

+0

@MuhammadAkram问题是为什么你需要同时检查两个按钮? –

+0

@MuhammadAkram好吧,我改变你可以尝试 –

if (cbProhibitEditPW.isChecked()) { // disable editing password 
     editTextPassword.setFocusable(false); 
     editTextPassword.setFocusableInTouchMode(false); // user touches widget on phone with touch screen 
     editTextPassword.setClickable(false); // user navigates with wheel and selects widget 
} else { // enable editing of password 
     editTextPassword.setFocusable(true); 
     editTextPassword.setFocusableInTouchMode(true); 
     editTextPassword.setClickable(true); 
} 

试试这个代码

的问题是,你还没有初始化,初始化的意见后,验证 试试这个在onCreate()

EditText t4 = (EditText)findViewById(R.id.editText3); 

RadioButton rb = (RadioButton) findViewById(R.id.radioButton1); 
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2); 
RadioGroup rg =(RadioGroup) findViewById(R.id.G1); 

if (rb.isChecked()) { 
    t4.setFocusable(true); 
} 

if (rb2.isChecked()) { 
    t4.setFocusable(false); 
} 
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 

      if(checkedId==R.id.radioButton1) 
      { 
       t4.setEnabled(false); 
      } 

      if(checkedId==R.id.radioButton2) 
      { 
       t4.setEnabled(true); 
      } 


     } 
    }); 

这将限制用户输入时屏幕发射

解决方法是致电除EditText上的setEnabled(false)之外,还有。更改您的代码,如下所示:

EditText t4 = (EditText)findViewById(R.id.editText3); 

RadioButton rb = (RadioButton) findViewById(R.id.radioButton1); 
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2); 
RadioGroup rg =(RadioGroup) findViewById(R.id.G1); 


    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 

      if(checkedId==R.id.radioButton1) 
      { 
       t4.setEnabled(false); 
       t4.setFocusable(false); 
      } 

      if(checkedId==R.id.radioButton2) 
      { 
       t4.setEnabled(true); 
       t4.setFocusable(true); 

      } 


     } 
    });