在最后一个EditText上按键盘上的“完成”后,隐式“提交”


Answers:


185

试试这个:

在您的布局中放置/编辑此内容:

<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处理程序的提交按钮在哪里。


15
submit_btn.performClick();在灼伤我的眼睛。不好意思 为什么不调用Submit方法呢?
洛朗·迈尔

28
@LaurentMeyer在这些情况下,模拟用户输入通常比直接调用基础逻辑更好。例如,提交按钮可能当前处于禁用状态,因此performClick()不会执行任何操作(如预期的那样),但是如果您直接调用Submit方法,则必须检查该按钮是否未首先被禁用。它也将扮演“咔嚓”的声音,仿佛是按键敲击等
Extragorey

3
@LaurentMeyer对UI敏感是什么意思?当然,最近6个月有5个人。给他们时间,人们可能也会同意我的看法。;)
Extragorey's

让我们考虑一下您更改了UI,将按钮用于其他用途。该代码将是一团糟,甚至更糟,您需要具有广泛的测试程序来检测这种错误。更糟糕的是,当您通过这种做法共享UI组件时。
洛朗·迈尔

1
TWIMC imeActionLabel在我的EditText中使用是禁用所有此行为。当心
Alwin Kesler '18

25

您需要在上设置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" />

然后将a添加OnEditorActionListener到视图以侦听“完成”操作。

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文档:https//developer.android.com/guide/topics/ui/controls/text.html#ActionEvent


22

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() })

6

这是怎么做的

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"/>

您的EditText中的actionDone


2

在您的edittext标记内的XML文件中,添加以下代码段

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; 
   } 
  });


1
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

这完全一样的答案,这一个。您应该解释一下您如何解决OP的问题。
阿德里安W

1

只要扩展这个答案

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)
}

0
<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;
        }
    });

0
 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;
     }
 });
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.