我有一个editText,它表示搜索条件的输入。我想知道是否有一种方法来检测用户何时停止编辑这个editText,这样我就可以查询数据库的数据为我的列表。例如,如果用户键入"test",我希望只在用户键入单词之后才收到通知,而不是像文本监视器那样在用户键入每个字母之后才收到通知。你有什么主意吗?我会避免使用计时器来测量按键事件之间的毫秒间隔。
不是非常优雅,但应该可以工作。
初始化:
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);
首先创建以下字段:
private Date _lastTypeTime = null;
然后确保你的editText实现了' texttwatcher ':
_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);
}
下面是你如何检测你正在寻找的事件。
声明和初始化:
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);
}
}
});
因此,当文本被更改时,计时器开始等待任何下一个更改发生。当它们发生时,计时器被取消,然后再次启动。
我就是这么做的!
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 {
}
}
});
检查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;
}