Я думаю, что DialogPreference
с EditText
обычно должен всегда закрывать себя, когда пользователь нажимает Готовы кнопку и экранная клавиатура закрывается. Чтобы сделать это поведение по умолчанию, легко реализовать настроенный EditTextPreferenceCustomized
, а затем использовать его везде, а не стандартный EditTextPreference
.
import android.app.Dialog;
import android.content.Context;
import android.preference.EditTextPreference;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class EditTextPreferenceCustomized extends EditTextPreference
{
public EditTextPreferenceCustomized(Context context)
{
super(context);
}
public EditTextPreferenceCustomized(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public EditTextPreferenceCustomized(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onAddEditTextToDialogView(View dialogView, EditText editText)
{
super.onAddEditTextToDialogView(dialogView, editText);
// editText.setImeOptions(EditorInfo.IME_ACTION_DONE); - uncomment if it's not specified in preferences.xml
editText.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
onClick(getDialog(), Dialog.BUTTON_POSITIVE);
getDialog().dismiss();
return true;
}
return false;
}
});
}
}
Код частично на основе полезного решения от @LZN, но это позволит устранить необходимость установки действий слушателя для каждого предпочтения в Java коде. Просто замените в preferences.xml все экземпляры EditTextPreference
на com.yourpackage.EditTextPreferenceCustomized
.
Спасибо. Мне удалось обмануть его, чтобы думать, что нажата положительная кнопка, вызвав 'editPref.onClick (editPref.getDialog(), DialogInterface.BUTTON_POSITIVE);' перед вызовом 'reject()'. Это хакерский подход, но я могу повторить ту же логику. – dasony
Ваше решение лучше, чем мое, я объединил его в должность. спасибо: D – LZN
Еще лучше установить ime в макете xml в тексте редактирования следующим образом: 'android: imeOptions =" actionDone "'. –