Android - 当用户输入'。'时结束TextWatcher

时间:2013-08-10 05:22:57

标签: java android textwatcher

我有一个TextWatcher设置和工作(几乎)完全按照我的意愿。但是,我希望这个TextWatcher在用户输入'。'后立即停止。如果用户完全删除了文本,我到目前为止找到的唯一解决方案会崩溃应用程序。在用户输入'。'。

时,整个TextWatcher结束也很重要

我尝试将TextWatcher放在循环中,但它似乎不起作用。

private TextWatcher userEnterListener = new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub


    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {



        if(after==0) {
            shownText = "Please try again";
        }
        else if(after==1) {
            shownText = "A";
        }
        else if(after==2) {
            shownText = "An";
        }
        else if(after==3) {
            shownText = "And";
        }
        else if(after==4) {
            shownText = "Andr";
        }
        else if(after==5) {
            shownText = "Andro";
        }
        else if(after==6) {
            shownText = "Androi";
        }
        else if(after==7) {
            shownText = "Android";
        }
        else if(after==8) {
            shownText = "Android A";
        }
        else if(after==9) {
            shownText = "Android Ap";
        }
        else if(after==10) {
            shownText = "Android App";
        }
        else {
            shownText = "Please try again";
        }




    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {

        recordedString = (s.toString());


        update();


    }




};

1 个答案:

答案 0 :(得分:0)

也许这可行:

public void onTextChanged(CharSequence s, int start, int before,
        int count) {

    if(s.toString().equals("Android App") && start == '.'){
        //here add an Empty TextWatcher to the EditText who has this TextWatcher added.
 }
    recordedString = (s.toString());


    update();


}