Android 我如何知道 EditText 何时失去焦点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10627137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I know when an EditText loses focus?
提问by japuentem
I need to catch when an EditText
loses focus, I've searched other questions but I didn't find an answer.
我需要抓住什么时候EditText
失去焦点,我搜索了其他问题,但没有找到答案。
I used OnFocusChangeListener
like this
我OnFocusChangeListener
是这样用的
OnFocusChangeListener foco = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
}
};
But, it doesn't work for me.
但是,它对我不起作用。
回答by ρяσ?ρ?я K
Implement onFocusChange
of setOnFocusChangeListener
and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.
实现onFocusChange
,setOnFocusChangeListener
hasFocus 有一个布尔参数。如果这是错误的,您就失去了对另一个控件的关注。
EditText txtEdit = (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// code to execute when EditText loses focus
}
}
});
回答by Cerberus
Have your Activity
implement OnFocusChangeListener()
if you want a factorized use of this interface,
example:
如果您想对该接口进行分解使用,请使用您的Activity
工具OnFocusChangeListener()
,例如:
public class Shops extends AppCompatActivity implements View.OnFocusChangeListener{
In your OnCreate
you can add a listener for example:
例如,您OnCreate
可以添加一个侦听器:
editTextResearch.setOnFocusChangeListener(this);
editTextMyWords.setOnFocusChangeListener(this);
editTextPhone.setOnFocusChangeListener(this);
then android studio will prompt you to add the method from the interface, accept it... it will be like:
然后android studio会提示你从界面中添加方法,接受它......它会像:
@Override
public void onFocusChange(View v, boolean hasFocus) {
// todo your code here...
}
and as you've got a factorized code, you'll just have to do that:
并且当你有一个分解的代码时,你只需要这样做:
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editTextResearch.setText("");
editTextMyWords.setText("");
editTextPhone.setText("");
}
if (!hasFocus){
editTextResearch.setText("BlaBlaBla");
editTextMyWords.setText(" One Two Tree!");
editTextPhone.setText("\"your phone here:\"");
}
}
anything you code in the !hasFocus
is for the behavior of the item that loses focus, that should do the trick! But beware that in such state, the change of focus might overwrite the user's entries!
您在 中编码的任何内容!hasFocus
都是针对失去焦点的项目的行为,这应该可以解决问题!但请注意,在这种状态下,焦点的变化可能会覆盖用户的条目!
回答by Achraf Amil
Kotlin way
科特林方式
editText.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus) { }
}
回答by Keshav Gera
Its Working Properly
其工作正常
EditText et_mobile= (EditText) findViewById(R.id.edittxt);
et_mobile.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// code to execute when EditText loses focus
if (et_mobile.getText().toString().trim().length() == 0) {
CommonMethod.showAlert("Please enter name", FeedbackSubmtActivity.this);
}
}
}
});
public static void showAlert(String message, Activity context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
try {
builder.show();
} catch (Exception e) {
e.printStackTrace();
}
}
回答by Sandeep Yohans
Using Java 8 lambda expression:
使用 Java 8 lambda 表达式:
editText.setOnFocusChangeListener((v, hasFocus) -> {
if(!hasFocus) {
String value = String.valueOf( editText.getText() );
}
});