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.
但是,它对我不起作用。
3 个解决方案
#1
267
Implement onFocusChange
of setOnFocusChangeListener
and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.
实现setOnFocusChangeListener的onFocusChange,并且有一个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
}
}
});
#2
7
Have your Activity
implement OnFocusChangeListener()
if you want a factorized use of this interface, example:
让您的活动实现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中编写的代码都是针对失去焦点的项目的行为,这应该会起到作用!但是要注意,在这种状态下,焦点的改变可能会覆盖用户的条目!
#3
0
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();
}
}
#1
267
Implement onFocusChange
of setOnFocusChangeListener
and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.
实现setOnFocusChangeListener的onFocusChange,并且有一个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
}
}
});
#2
7
Have your Activity
implement OnFocusChangeListener()
if you want a factorized use of this interface, example:
让您的活动实现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中编写的代码都是针对失去焦点的项目的行为,这应该会起到作用!但是要注意,在这种状态下,焦点的改变可能会覆盖用户的条目!
#3
0
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();
}
}