检查OnTextChanged中的空EditText

时间:2015-01-27 14:17:30

标签: java android

我是一个新手Android开发者..现在,我想检查我的所有EditTexts是否为空。以下是我的方法:

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);
    }
}

正如您所看到的,当我尝试调试EditText时,它显示qwe﹕ [ 01-27 20:15:03.783 214:0x103 I/InputReader ]而不是空字符串..任何想法?

3 个答案:

答案 0 :(得分:5)

您可以使用下面的内容,其中edtContent是EditText。如果您有多个EditText's在其他位置定义TextWatcher,并通过EditText方法将同一个实例作为TextChangedListener传递给所有addTextChangedListener个实例。

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 {

            }
        }
    });

答案 1 :(得分:4)

尝试以下代码

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) {}
    };

答案 2 :(得分:0)

这是问题所在。

getText()不返回字符串。它返回一个可编辑对象。

请参阅http://developer.android.com/reference/android/widget/EditText.htmlhttp://developer.android.com/reference/android/text/Editable.html

因此,当您使用String.valueOf()将可编辑转换为日志语句中的字符串时,实际上是从可编辑中打印信息,而不是获取它所持有的字符串值。

尝试:

log.d("qwe", inputSearchNama.getText().toString());