106

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

4

10 回答 10

208

尝试这个:

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

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

于 2013-10-07T05:27:08.800 回答
29

您需要在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在视图中添加一个来监听“完成”动作。

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

于 2013-10-07T05:27:25.280 回答
29

使用 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() })
于 2018-02-15T15:03:41.820 回答
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中完成操作。

于 2013-10-07T05:29:34.523 回答
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 something here 
            return true;
        }
        return false; 
    } 
});
于 2018-02-15T16:00:15.233 回答
1

在edittext中添加以下行

android:imeOptions="actionDone"

快乐编码

于 2018-04-20T07:17:11.567 回答
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
于 2018-09-12T11:42:23.473 回答
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)
}
于 2019-09-17T21:08:41.137 回答
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;
    }
});
于 2013-10-07T05:35:49.667 回答
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;
     }
 });
于 2016-04-30T04:51:02.783 回答