【问题标题】:Add action to "Enter" button on Android keyboard为 Android 键盘上的“Enter”按钮添加操作
【发布时间】:2017-01-26 06:50:31
【问题描述】:

我想知道如何为按下键盘上的“Enter”按钮添加事件/动作。

“该操作与按下我在应用程序上创建的按钮相同”

【问题讨论】:

    标签: android keyboard action enter


    【解决方案1】:

    您可以使用 imeOptions 属性来指定您的 EditText 上的操作

    <EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/password_hint"
    android:inputType="textPassword"
    android:imeOptions="actionLogin" />
    

    然后在您的活动/片段上为该操作创建一个侦听器

    EditText password = (EditText) findViewById(R.id.password);
    password.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_LOGIN) {
            login();
        }
        return false;
    }});
    

    【讨论】:

    • 但在代码中“输入”只有在使用 EditText 时才有效?我想要一种让“输入”在我的应用上执行与按钮相同的活动的方式。
    • 那么如何在没有 EditText 的情况下在 Activity 中打开键盘?
    • 哦,是的,我表达错了。在 imeOptions 上,我可以使用与按钮相同的方法吗?对于两者执行相同的操作
    • 当然。只需将 OnClickListener 添加到调用相同方法的按钮中
    • 明白。最后一个疑问:我需要在我的应用程序的所有 EditText 上编写此代码,或者只写一次,它适用于每个 EditText?
    猜你喜欢
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2017-02-23
    • 2019-05-16
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多