2017-02-15 72 views
0

此代碼無法正常工作,錯在哪裏?我不希望允許用戶輸入直到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://stackoverflow.com/a/23197277/7320259希望它可以幫助您 –

回答

2
t4.setEnable(rb2.isChecked()); 

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

你可以試試下面給出的代碼?

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

0

你可以試試這個

檢查「單選框」被選中: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好吧,我改變你可以嘗試 –

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

試試這個代碼

0

的問題是,你還沒有初始化,初始化的意見後,驗證 試試這個在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); 
      } 


     } 
    }); 

這將限制用戶輸入時屏幕發射

0

解決方法是致電除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); 

      } 


     } 
    });