简体   繁体   English

在CheckBox的android onclick中启用和禁用edittext

[英]enable and disable the edittext in android onclick of CheckBox

'

//this is first checkbox on click this it will enable f1 & n1 //这是第一个单击此复选框,它将启用f1和n1

check1=(CheckBox)findViewById(R.id.checkbox1); CHECK1 =(复选框)findViewById(R.id.checkbox1);

//to disable the editext1 //禁用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);

//this is second checkbox on click this it will enable f2 & n2 //这是第二个复选框,单击此按钮将启用f2&n2

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

//to disable the editext2 f2 //禁用editext2 f2

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

//to disable the editext2 n2 //禁用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);

//to enable the editext1 f1 & n1 //启用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);


        }

//to enable the editext1 f2 & n2 //启用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);


        }**

'

You need to set listeners on your checkboxes, and then have to capture the events like when checkbox is checked and when check is removed to make your editexts work the way you want.Your current code will execute and it will take care of no future events because no listener is registered to capture the events. 您需要在复选框上设置侦听器,然后必须捕获事件,例如选中复选框时以及何时删除检查以使您的editexts按您希望的方式工作。您的当前代码将执行并且它将处理未来的事件因为没有注册监听器来捕获事件。 The Simplest way to start is 最简单的方法是

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     

      }
    });

However there are other ways too,to perform this task, but you can kick off with this right away. 但是,还有其他方法可以执行此任务,但您可以立即启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM