我有一个 editText 代表搜索条件的输入。我想知道是否有办法检测用户何时停止编辑此editText,以便我可以查询数据库以获取我的列表的数据。例如,如果用户键入“test”,我只想在用户键入单词后收到通知,而不是在用户键入每个字母后,就像 text watcher 那样。你有什么想法?我会避免使用一些计时器来测量按键事件之间经过的毫秒数。
问问题
8927 次
5 回答
10
不是非常优雅,但这应该可以。
初始化:
long idle_min = 4000; // 4 seconds after user stops typing
long last_text_edit = 0;
Handler h = new Handler();
boolean already_queried = false;
设置将从文本观察器调用的可运行文件:
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);
于 2013-03-19T10:56:27.593 回答
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);
}
于 2013-06-05T16:05:36.287 回答
6
以下是如何检测您正在寻找的事件。
声明和初始化:
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);
}
}
});
因此,当文本更改时,计时器开始等待任何下一次更改发生。当它们发生时,计时器被取消,然后再次启动。
于 2015-02-20T09:40:40.523 回答
1
这就是我所做的和为我工作的方式!
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 {
}
}
});
于 2016-07-22T10:21:41.193 回答
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;
}
于 2020-09-01T10:59:48.383 回答