[英]Check empty EditText inside OnTextChanged
I'm a newbie android developer.. Now, I want to check whether all my EditTexts are empty or not.. Below is my approach: 我是一名新手Android开发人员。.现在,我想检查我所有的EditText是否为空。.下面是我的方法:
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Log.d("qwe", String.valueOf(inputSearchNama.getText()));
if(String.valueOf(inputSearchNama.getText()).isEmpty() || String.valueOf(inputSearchJenis.getText()).isEmpty() || String.valueOf(inputSearchMerk.getText()).isEmpty()) {
DBHelper db = new DBHelper(getActivity());
try {
db.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
db.close();
List<PriceList> getPrice = db.getSearchedPriceList(cs, String.valueOf(inputSearchMerk.getText()), String.valueOf(inputSearchJenis.getText()));
ListAdapter myAdapter = new MyAdapter(getActivity(), getPrice);
theListView.setAdapter(myAdapter);
}
}
As you can see, when I try to debug my EditText, it shows qwe﹕ [ 01-27 20:15:03.783 214:0x103 I/InputReader ]
instead of empty string.. Any Idea ? 如您所见,当我尝试调试EditText时,它显示
qwe﹕ [ 01-27 20:15:03.783 214:0x103 I/InputReader ]
而不是空字符串。
You may use something like below where edtContent is an EditText
. 您可以使用类似下面的内容,其中edtContent是
EditText
。 If you have multiple EditText's
define the TextWatcher
elsewhere and pass the same instance to all of the EditText
instances as a TextChangedListener
via the addTextChangedListener
method. 如果您有多个
EditText's
, TextWatcher
在其他位置定义TextWatcher
然后通过addTextChangedListener
方法将同一实例作为TextChangedListener
传递给所有EditText
实例。
edtContent.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (TextUtils.isEmpty(s.toString().trim())) {
} else {
}
}
});
Try Below code 尝试以下代码
private TextWatcher watcher;
......................................
inputSearchNama.addTextChangedListener(watcher); //register watcher to edittext
inputSearchJenis.addTextChangedListener(watcher); //register watcher to edittext
inputSearchMerk.addTextChangedListener(watcher); //register watcher to edittext
watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(inputSearchNama.getText().toString().trim().equals("")){
//write your code here
}
if(inputSearchJenis.getText().toString().trim().equals("")){
//write your code here
}
if(inputSearchMerk.getText().toString().trim().equals("")){
//write your code here
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {}
@Override
public void afterTextChanged(Editable s) {}
};
Here is the problem. 这是问题所在。
getText() does not return a string. getText()不返回字符串。 It returns an Editable object.
它返回一个Editable对象。
See http://developer.android.com/reference/android/widget/EditText.html and http://developer.android.com/reference/android/text/Editable.html 请参阅http://developer.android.com/reference/android/widget/EditText.html和http://developer.android.com/reference/android/text/Editable.html
So, when you convert the Editable to a string in your log statement using String.valueOf(), you are actually printing info from the Editable, not getting the string value it holds. 因此,当您使用String.valueOf()在日志语句中将Editable转换为字符串时,实际上是从Editable打印信息,而不是获取它持有的字符串值。
Try: 尝试:
log.d("qwe", inputSearchNama.getText().toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.