在我的一个活动中,我有三个EditTexts和一个OK按钮. OnFocusChangeListener设置为所有三个EditTexts.每次焦点丢失时,听众都应该触发.
在EditTexts之间切换非常有效.但是,如果用户按下OK按钮,则在按下按钮之前没有针对用户聚焦的EditText触发的焦点改变(失去焦点).
我的代码出了什么问题?
private class MyOnFocusChangeListener implements OnFocusChangeListener { private EditText editText; public MyOnFocusChangeListener(final EditText editText) { super(); this.editText = editText; } @Override public void onFocusChange(final View view,final boolean isFocused) { if (!isFocused) { if (editText == editText1) { // Do a calculation } else if (editText == editText2) { // Do another calculation } else if (editText == editText3) { // Do a different calculation } } } } @Override public void onCreate(final Bundle bundle) { // ... editText1.setOnFocusChangeListener(new MyOnFocusChangeListener(editText1)); editText2.setOnFocusChangeListener(new MyOnFocusChangeListener(editText2)); editText3.setOnFocusChangeListener(new MyOnFocusChangeListener(editText3)); // ... }
解决方法
当用户点击OK或其他按钮时,您可以尝试清除焦点….
例如
builder.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { editText1.clearfocus(); editText2.clearfocus(); editText3.clearfocus(); .... } }