【问题标题】:How can I check if enter on my keyboard is clicked?如何检查是否单击了键盘上的输入?
【发布时间】:2019-03-31 11:22:20
【问题描述】:

此问题不重复。

我想检查我的键盘上的 enter 是否被点击。

这段代码对我最有帮助:

 edittext.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            Toast.makeText(getActivity(), "it works", Toast
                    .LENGTH_SHORT).show();
            return false;
        }
    });

但它只适用于我的电脑键盘,不适用于我的手机。 我不知道天气很重要,但我正在使用 Fragment。

这是我的 XML:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:id="@+id/edittext"
    android:imeOptions="actionDone"
    android:padding="10dp"/>

我查看了 ALL These acticles 并尝试了所有代码,但没有任何效果。

编辑

我看了一条评论,所以我想补充一下:

我说的是软件键盘。

【问题讨论】:

  • 你说的是软件键盘还是硬件键盘?
  • @JoaquinAlvarez 关于软件键盘

标签: android onclick key onclicklistener enter


【解决方案1】:

是否依赖于您的 EditText 的 android:imeOptions=""。 例如,android:imeOptions="actionDone":

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId == EditorInfo.IME_ACTION_DONE){
            //do something
        }
    return false;
    }
});

更新: 在 xml 文件中添加 android:inputType="text" 到您的 EditText

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:id="@+id/edittext"
    android:inputType="text"
    android:imeOptions="actionDone"
    android:padding="10dp"/>

【讨论】:

  • 它什么也没改变 :(
  • 你的 'android:imeOptions' 是什么?
  • 也不工作 :( 它对你有用吗?也许是我的设备或模拟器的错。
  • @Bäumesindgrün 给我 5 分钟
  • 非常感谢 :) 成功了,你真是个救命恩人!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多