在 Android 中,如何仅在 textView 中有文本时才启用复选框?
In Android, how can I enable a checkBox only if there is text in a textView?
在我的布局中,我有一个 textView 和一个复选框。我喜欢仅当用户在 textView 中输入文本时才启用复选框。如果用户删除了 textView 中的文本,复选框应该再次变灰。
你能给我一些想法吗?
提前致谢
我想 textView
你的意思是 editText
对吧?
在 EditText
中有一个方法可以设置文本更改侦听器。方法名称是 addTextChangedListener
,您可以在其中检查输入大小是否为 0。所以这是示例代码:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int
i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
checkBox.setEnabled(editable.toString().length() > 0);
}
});
在我的布局中,我有一个 textView 和一个复选框。我喜欢仅当用户在 textView 中输入文本时才启用复选框。如果用户删除了 textView 中的文本,复选框应该再次变灰。
你能给我一些想法吗?
提前致谢
我想 textView
你的意思是 editText
对吧?
在 EditText
中有一个方法可以设置文本更改侦听器。方法名称是 addTextChangedListener
,您可以在其中检查输入大小是否为 0。所以这是示例代码:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int
i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
checkBox.setEnabled(editable.toString().length() > 0);
}
});