【问题标题】:Disable EditText with radio button checked在选中单选按钮的情况下禁用 EditText
【发布时间】:2017-07-03 19:39:42
【问题描述】:

这段代码不工作,怎么了?在选中radiobutton2 之前,我不想允许用户输入

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


        }
    });

【问题讨论】:

标签: android android-edittext radio-button


【解决方案1】:
t4.setEnable(rb2.isChecked());

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

【讨论】:

    【解决方案2】:

    你能试试下面给出的代码吗?

    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

    【讨论】:

      【解决方案3】:
          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);
      }
      

      试试这个代码

      【讨论】:

        【解决方案4】:

        问题是您在初始化视图后还没有初始化验证 试试这个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);
                    }
        
        
                }
            });
        

        这将限制用户在屏幕启动时输入

        【讨论】:

          【解决方案5】:

          解决方法是在 EditText 上调用 setFocusable(false)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);
          
                      }
          
          
                  }
              });
          

          【讨论】:

            【解决方案6】:

            你可以试试这个

            检查“单选按钮”是否被选中: isChecked()

            在 android 中禁用 EditText: 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);
            }
            

            【讨论】:

            • 这仅在第一次检查时有效。意思是如果我都尝试过,那么 t4 将永远禁用。我又要回去了。你能告诉我如何编码,以便在我更改单选按钮的选择时它可以多次工作吗??
            • @MuhammadAkram 问题是为什么你需要同时检查两个按钮??
            • @MuhammadAkram 好的,我改变你可以试试
            猜你喜欢
            • 2019-03-29
            • 2012-04-05
            • 1970-01-01
            • 2019-06-28
            • 2014-01-30
            • 2018-09-04
            • 1970-01-01
            • 2019-03-24
            • 2011-08-01
            相关资源
            最近更新 更多