在最后一个EditText上敲击键盘上的Done后隐式"提交"

J. *_* K. 91 android

我已经使用了一些应用程序,当我填写用户名,然后转到我的密码,如果我在键盘上点击"完成",登录表单会自动提交,而不必单击提交按钮.这是怎么做到的?

Har*_*ran 174

试试这个:

在你的布局中输入/编辑:

<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" />
Run Code Online (Sandbox Code Playgroud)

在你的活动中把这个(例如在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;
     }
 });
Run Code Online (Sandbox Code Playgroud)

submit_btn附带onclick处理程序的提交按钮在哪里.

  • @LaurentMeyer模拟用户输入通常比在这些情况下直接调用底层逻辑更好.例如,提交按钮当前可能被禁用,因此performClick()将不执行任何操作(按预期),但如果您直接调用了submit方法,则必须先检查该按钮是否未被禁用.它也会播放"咔嗒"声,就好像按下按钮一样. (26认同)
  • `submit_btn.performClick();`正在燃烧我的眼睛.Srsly?为什么不调用submit方法? (14认同)
  • @LaurentMeyer你对UI敏感是什么意思?肯定是过去6个月中有5个人.给他们时间,人们也可能同意我的意见.;) (3认同)
  • TWIMC,在我的 EditText 中使用 `imeActionLabel` 禁用了所有这些行为。小心 (2认同)

flx*_*flx 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" />
Run Code Online (Sandbox Code Playgroud)

然后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;
    }
});
Run Code Online (Sandbox Code Playgroud)

官方API文档:https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent


Fra*_*llo 19

使用Kotlin简单有效的解决方案

延伸EditText:

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->

       if (actionId == EditorInfo.IME_ACTION_DONE) {
           func()
       }

       true

    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用这样的新方法:

editText.onSubmit { submit() }
Run Code Online (Sandbox Code Playgroud)

哪里submit()是这样的:

fun submit() {
    // call to api
}
Run Code Online (Sandbox Code Playgroud)

更通用的扩展

fun EditText.on(actionId: Int, func: () -> Unit) {
    setOnEditorActionListener { _, receivedActionId, _ ->

       if (actionId == receivedActionId) {
           func()
       }

        true
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以用它来听你的活动:

email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
Run Code Online (Sandbox Code Playgroud)


Jit*_*Dev 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;
    }
});
Run Code Online (Sandbox Code Playgroud)

别忘了添加

<EditText android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:imeOptions="actionDone"/>
Run Code Online (Sandbox Code Playgroud)

EditText中的actionDone.