android editText:检测用户何时停止编辑

时间:2013-03-19 10:20:54

标签: android event-handling android-edittext edit

我有一个editText,表示搜索条件的输入。我想知道是否有办法检测用户何时停止编辑此editText,以便我可以在数据库中查询我的列表的数据。例如,如果用户键入“test”,我希望仅在用户键入单词后才会收到通知,而不是在用户键入每个字母之后,就像文本观察者那样。你有什么想法?我会避免使用一些计时器来测量关键预事件之间经过的毫秒数。

5 个答案:

答案 0 :(得分:10)

不是很优雅,但这应该有用。

初始化:

long idle_min = 4000; // 4 seconds after user stops typing
long last_text_edit = 0;
Handler h = new Handler();
boolean already_queried = false;

设置将从文本观察器调用的runnable:

private Runnable input_finish_checker = new Runnable() {
    public void run() {
            if (System.currentTimeMillis() > (last_text_edit + idle_min - 500)) {
                 // user hasn't changed the EditText for longer than
                 // the min delay (with half second buffer window)
                 if (!already_queried) { // don't do this stuff twice.
                     already_queried = true;
                     do_stuff();  // your queries
                 }
            }
    }
};

将它放在文本观察器中:

last_text_edit = System.currentTimeMillis();
h.postDelayed(input_finish_checker, idle_min); 

答案 1 :(得分:7)

首先创建以下字段:

private Date                _lastTypeTime   = null;

然后确保您的editText实现'TextWatcher':

_editText.addTextChangedListener(this);

然后,覆盖接口的方法如下:

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
    _lastTypeTime = new Date();
}

@Override
public void afterTextChanged(Editable arg0)
{
}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
    // dispatch after done typing (1 sec after)
    Timer t = new Timer();
    TimerTask tt = new TimerTask()
    {
        @Override
        public void run()
        {
            Date myRunTime = new Date();

            if ((_lastTypeTime.getTime() + 1000) <= myRunTime.getTime())
            {
                post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Log.d("<tag>", "typing finished!!!");
                    }
                });
            }
            else
            {
                Log.d("<tag>", "Canceled");
            }
        }
    };

    t.schedule(tt, 1000);
}

答案 2 :(得分:4)

以下是如何检测您正在寻找的事件。

声明和初始化:

private Timer timer = new Timer();
private final long DELAY = 1000; // in ms

听众在例如的onCreate()

EditText editText = (EditText) findViewById(R.id.editTextStopId);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        @Override
        public void onTextChanged(final CharSequence s, int start, int before,
                int count) {
            if(timer != null)
                timer.cancel();
        }
        @Override
        public void afterTextChanged(final Editable s) {
            //avoid triggering event when text is too short
            if (s.length() >= 3) {              

                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        // TODO: do what you need here (refresh list)
                        // you will probably need to use
                        // runOnUiThread(Runnable action) for some specific
                        // actions
                        queryDB();
                    }

                }, DELAY);
            }
        }
    });

因此,当文本被更改时,计时器开始等待任何下一次更改发生。当它们发生时,计时器被取消,然后再次启动。

答案 3 :(得分:0)

这就是我为我所做的工作!

long delay = 1000; // 1 seconds after user stops typing
long last_text_edit = 0;
Handler handler = new Handler();

private Runnable input_finish_checker = new Runnable() {
    public void run() {
        if (System.currentTimeMillis() > (last_text_edit + delay - 500)) {
            // TODO: do what you need here
            DoStaff();
        }
    }
};

EditText editText = (EditText) findViewById(R.id.editTextStopId);
editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }
    @Override
    public void onTextChanged(final CharSequence s, int start, int before,
            int count) {
         //You need to remove this to run only once
         handler.removeCallbacks(input_finish_checker);

    }
    @Override
    public void afterTextChanged(final Editable s) {
        //avoid triggering event when text is empty
        if (s.length() > 0) {              
          last_text_edit = System.currentTimeMillis();
          handler.postDelayed(input_finish_checker, delay);
        } else {

        }
    }
});

答案 4 :(得分:0)

最简单的检查editText是否包含文字或非文字(仅一次)的方法:

    private boolean newState;


  protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.somLayout);

    edt.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) {
                    if (!editable.trim().isEmpty()) {
                        checkIsTyping(true);
                    } else {
                        checkIsTyping(false);
                    }
                }
            });

}
    private void checkIsTyping(boolean typeState) {
            if (newState != typeState) {
                Toast.makeText(appCompatActivity, "typingState  " + newState, 
            Toast.LENGTH_SHORT).show();
            }
            newState = typeState;
    
        }