Я использовал некоторые приложения, где, когда я заполняю свое имя пользователя, а затем перейдите к моему паролю, если я нажму "Готово" на клавиатуре, форма входа будет автоматически отправлена без необходимости нажатия кнопки "Отправить". Как это делается?
Неявный "Отправить" после нажатия "Готово" на клавиатуре в последнем EditText
Ответ 1
Попробуйте следующее:
В вашем макете поставьте/отредактируйте это:
<EditText
android:id="@+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone" />
В вашей деятельности поместите это (например, в onCreate):
// your text box
EditText edit_txt = (EditText) findViewById(R.id.search_edit);
edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit_btn.performClick();
return true;
}
return false;
}
});
Где submit_btn
- ваша кнопка отправки с прикрепленным обработчиком onclick.
Ответ 2
Вам нужно установить параметры IME на EditText
.
<EditText
android:id="@+id/some_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Whatever"
android:inputType="text"
android:imeOptions="actionDone" />
Затем добавьте OnEditorActionListener
в представление, чтобы прослушать действие "done".
EditText editText = (EditText) findViewById(R.id.some_view);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
// TODO do something
handled = true;
}
return handled;
}
});
Официальный API doc: https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent
Ответ 3
Простое и эффективное решение с Kotlin
Расширить EditText
:
fun EditText.onSubmit(func: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
func()
}
true
}
}
Затем используйте новый метод следующим образом:
editText.onSubmit { submit() }
Где submit()
выглядит примерно так:
fun submit() {
// call to api
}
Более общее расширение
fun EditText.on(actionId: Int, func: () -> Unit) {
setOnEditorActionListener { _, receivedActionId, _ ->
if (actionId == receivedActionId) {
func()
}
true
}
}
И затем вы можете использовать его для прослушивания вашего мероприятия:
email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
Ответ 4
Вот как это делается
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});
Не забудьте добавить
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:imeOptions="actionDone"/>
actionDone в вашем EditText.
Ответ 5
В вашем XML файле внутри вашего тега edittext добавьте ниже фрагмент
android:imeOptions="actionDone"
Затем в вашем классе Java напишите нижеприведенный код
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int id, KeyEvent event) {
if (id == EditorInfo.IME_ACTION_DONE) {
//do your work here
return true;
}
return false;
}
});
Ответ 6
добавьте следующую строку в edittext
android:imeOptions="actionDone"
Счастливое кодирование
Ответ 7
etParola = (EditText) findViewById(R.id.etParola);
btnGiris = (Button) findViewById(R.id.btnGiris);
etParola.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
btnGiris.performClick();
return true;
}
return false;
}
});
and;
layout xml etParola
android:imeOptions="actionDone" add
Ответ 8
Просто продлите этот ответ
fun EditText.onSubmit(func: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
clearFocus() // if needed
hideKeyboard()
func()
}
true
}
}
fun EditText.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.windowToken, 0)
}
Ответ 9
<EditText
android:id="@+id/signinscr_userName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/userName"
android:imeOptions="actionNext" />
<EditText
android:id="@+id/signinscr_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:imeOptions="actionDone"
android:inputType="textPassword" />
в .java файле
EditText userNameField = (EditText) findViewById(R.id.signinscr_userName);
EditText passwordField = (EditText) findViewById(R.id.signinscr_password);
passwordField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
//Do your operation here.
return false;
}
});
Ответ 10
EditText edit_txt = (EditText) findViewById(R.id.search_edit);
edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// which is u had set a imeoption
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit_btn.performClick();
return true;
}
return false;
}
});