【问题标题】:enable and disable the edittext in android onclick of CheckBox在CheckBox的android onclick中启用和禁用edittext
【发布时间】:2017-01-28 12:53:03
【问题描述】:

'

//这是第一个复选框,点击它会启用 f1 & n1

check1=(CheckBox)findViewById(R.id.checkbox1);

//禁用editext1

        f1=(EditText)findViewById(R.id.editTextf1);
         f1.setEnabled(false);
        f1.setInputType(InputType.TYPE_NULL);
        f1.setFocusable(false);
         n1=(EditText)findViewById(R.id.editTextn1);
         n1.setEnabled(false);
        n1.setInputType(InputType.TYPE_NULL);
        n1.setFocusable(false);

//这是第二个复选框,点击它会启用 f2 & n2

        check2=(CheckBox)findViewById(R.id.checkBox2);

//禁用editext2 f2

        f2=(EditText)findViewById(R.id.editTextf2;
        f2.setEnabled(false);
        f2.setInputType(InputType.TYPE_NULL);
        f2.setFocusable(false);

//禁用editext2 n2

        n2=(EditText)findViewById(R.id.editTextn2);
        n2.setEnabled(false);
        n2.setInputType(InputType.TYPE_NULL);
        n2.setFocusable(false);

        btnfinal=(Button)findViewById(R.id.button_final_submission);

//启用editext1 f1 & n1

        if (check1.isChecked()) {
            f1.setEnabled(true);
            f1.setInputType(InputType.TYPE_CLASS_TEXT);
            f1.setFocusable(true);

            n1.setEnabled(true);
            n1.setInputType(InputType.TYPE_CLASS_TEXT);
            nc1.setFocusable(true);


        }

//启用editext1 f2 & n2

        if (check2.isChecked()) {
            f2.setEnabled(true);
            f2.setInputType(InputType.TYPE_CLASS_TEXT);
            f2.setFocusable(true);

            n2.setEnabled(true);
            n2.setInputType(InputType.TYPE_CLASS_TEXT);
            n2.setFocusable(true);


        }**

'

【问题讨论】:

  • 你有什么问题?
  • 您忘记解释您发布的代码中的问题所在。
  • 问题是我无法使用上面的代码启用和禁用edittext文本框

标签: android android-layout android-checkedtextview


【解决方案1】:

您需要在复选框上设置侦听器,然后必须捕获事件,例如选中复选框和删除复选框以使您的编辑文本按您想要的方式工作。您当前的代码将执行并且它会处理没有未来的事件,因为没有注册监听器来捕获事件。 最简单的开始方式是

CheckBox someCheckBox= (CheckBox) findViewById (R.id.someID);
someCheckBox.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        if (((CheckBox) v).isChecked()) {
                //CODE TO MAKE THE EDITTEXT ENABLED         
        }
        else 
           //CODE TO MAKE THE EDITTEXT DISABLED     

      }
    });

但是也有其他方法可以执行此任务,但您可以立即开始。

【讨论】:

  • thanx @nobalG 我还有一个问题可以在单选按钮中应用相同的方式吗??
  • 是的,你可以...See this simple example
  • 此代码仅在我 uckeck 框禁用它不工作时工作一次
猜你喜欢
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
相关资源
最近更新 更多