我有一个EditText。当我点击它时,它变得可聚焦。我将输入要输入EditText的输入文本。我想为EditText实现一个监听器,这样当我停止输入时,它应该自动将该文本保存到数据库中而不是一个按钮。如何让EditText的监听器听取是否停止输入?
答案 0 :(得分:52)
试试这样。
EditText et = (EditText)findViewById(R.id.editText);
Log.e("TextWatcherTest", "Set text xyz");
et.setText("xyz");
et.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) {
Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
}
});
答案 1 :(得分:16)
设置edittext imeOption
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
通过使用这样的东西,
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Specify your database function here.
return true;
}
return false;
}
});
或者,您可以使用OnEditorActionListener
接口来避免匿名内部类。
答案 2 :(得分:4)
将此添加到您的editText
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
答案 3 :(得分:0)
还可以使用Rx Java的 debounce 运算符。
let myCustomButtonWithBackgroundsAndStuff = UIButton(type: .custom)
...
// this is the equivalent of connecting the IBAction, in code form, in case you didn't know
myCustomButtonWithBackgroundsAndStuff.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: myCustomButton)
navigationItem.rightBarButtonItem = barButtonItem
// or append to rightBarButtonItems if you have other bar button items
...
@objc func buttonAction() {
...
}
您可以在此处定义时间限制,在将其保存到数据库中的时间之后。