2012-05-08 20 views
0

我试图禁用和启用edittext框的聚焦取决于复选框。虽然复选框被选中,但是文本框是可调焦的,如果没有选中,文本框应该是不可调焦的。当我尝试使用复选框禁用文本框后启用对焦文本框时,它不起作用。这是我的代码。启用可编辑文本框的焦点取决于复选框不工作后禁用编辑文本框在Android

check_box.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 
       if(isChecked){ 
        txt_time.setFocusable(true); 
       } else { 
        txt_time.setFocusable(false); 
       } 

      } 
     }); 

在此先感谢..

回答

1

尝试

启用:

txt_time.setFocusableInTouchMode(true); 
txt_time.setFocusable(true); 

禁用:的

txt_time.setFocusableInTouchMode(false); 
txt_time.setFocusable(false); 

代替

启用:

txt_time.setFocusable(true); 

禁用:

txt_time.setFocusable(false); 
0

而不是

txt_time.setFocusable(true); 

尝试

txt_time.setEditable(true); 
+0

当我用你的答案我有一个未定义的错误消息。所以我使用txt_time.setEnabled(true);我的代码运行良好。 –

+0

您可以在xml代码中使用,android:editable =“false”,或者您可以使用inputType,因为“editable”已被弃用。 –