2014-04-08 73 views
0

我是新手,我想知道如果我的EditText是空的可以启用或禁用botton。但是当EditText为空时,按钮将继续显示为启用。这是我的代码。知道是否EditText为空

if((getActivity().findViewById(R.id.textcodigo)).toString().matches("")){ 
       Button aceptar= (Button) getActivity().findViewById(R.id.aceptar); 
       aceptar.setEnabled(false); 
    } 

回答

2

的问题是你需要findViewById(R.id.textcodigo)).getText().toString().equals(""),而不是你目前有

+0

我使用的片段,它说,该方法的getText()是未定义 – user3383415

+0

(只是出于好奇):不会'findViewById(R.id.textcodigo))的getText ().toString()。length == 0'会更快吗? –

+0

我不知道它是更快或不是,但这是另一种方式来检查 – tyczj

0

你想要的是.equal("")matches(这需要一个正则表达式作为参数)

2

也许尝试

.getText().equals("") 

而不是

.toString().matches("") 

编辑完整代码:

if(((EditText) getActivity().findViewById(R.id.textcodigo)).toString().matches("")){ 
       Button aceptar= (Button) getActivity().findViewById(R.id.aceptar); 
       aceptar.setEnabled(false); 
    } 
+0

我使用片段,它说方法getText()是undefined – user3383415

+0

,因为你必须投影到EditText像你一样按钮 ( Button)getActivity()。findViewById(R.id.aceptar); 其他Android认为它不是一个EditText – guikk

相关问题