【问题标题】:setOnEditorActionListener not working with soft keyboard submit button, but does with laptop Enter key?setOnEditorActionListener 不能使用软键盘提交按钮,但可以使用笔记本电脑的 Enter 键?
【发布时间】:2017-01-02 07:37:50
【问题描述】:

谁能提供一个解决方案,让软键盘DONE 按钮的监听器正常工作,和/或解释我在当前方法中做错了什么?

我的 XML 和 Java 设置

  • 在 Xml 中,有一个简单的 EditText 设置为 android:imeOptions="actionDone"
  • 在 Java 中,它只有一个基本的 TextView.OnEditorActionListener 声明

所以当点击设备软键盘提交(又名DONE)按钮时,setOnEditorActionListener() 不会被调用 - 下面的绿色箭头按钮; 只有 EditText 字段被清除

但是当点击计算机Enter(通过 ADB 附加) 时会调用该侦听器。

我认为它应该对这两个按钮都有效.. 这不对吗?

XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.demo.MainActivity">

    <EditText
        android:id="@+id/comment_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:maxLines="1"
        android:imeOptions="actionDone"
        />    
</LinearLayout>

Java 活动文件:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                int i = 5; // added this to set a break point
                return false;
            }
        });
    }
}

解决方案更新

感谢@Apoorv Mehrotra 的回答,事实证明我的 EditText 缺少这一属性以便识别软键盘事件。将其添加到上述 Xml 即可解决问题。

android:inputType="text"

【问题讨论】:

  • 这解决了我的问题以及我有`android:inputType="textMultiLine"` 但是text 的问题是,在我按下回车后,键盘被隐藏,而textMultiLine 在按下后输入消息已发送,但键盘保持焦点
  • 感谢您根据上下文发布解决方案。知道为什么需要为关键侦听器声明输入吗?
  • 这显然是不合逻辑的,这是一个错误

标签: android


【解决方案1】:

我遇到了同样的问题,对我来说,在 EditText 中添加 android:singleLine="true" 就可以了

【讨论】:

  • 哇,这花了很长时间。
【解决方案2】:

试试这个代码 这是一个包含 2 个字段的代码,其中我有用户时间功能。在第一个键盘显示下一个按钮,在第二个软键盘显示完成选项和完成选项我隐藏软键盘。

下面是xml文件代码。

  <EditText
                android:id="@+id/txt1"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:gravity="top|left"
                android:imeOptions="actionNext"
                android:inputType="text|textNoSuggestions"
                android:text=""
                android:visibility="visible" />

    <EditText
                android:id="@+id/txt2"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:gravity="top|left"
                android:imeOptions="actionDone"
                android:inputType="text|textNoSuggestions"
                android:text=""
                android:visibility="visible"
                />

现在下面是 java 代码:用于在 next 上隐藏软键盘,也可以对 done 选项执行相同的操作。

此代码添加到您的类的 onCreate() 中。

    EditText first_txt = (EditText) findViewById(R.id.txt1);
    EditText second_txt = (EditText) findViewById(R.id.txt2);

    first_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {

                InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                return true; // Focus will do whatever you put in the logic.
            }
            return false;  // Focus will change according to the actionId
        }
    });

second_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                return true; // Focus will do whatever you put in the logic.
            }
            return false;  // Focus will change according to the actionId
        }
    });

此代码适用于 ,希望对您有所帮助!

【讨论】:

  • 根据您的回答,我能够确定我的代码中仅缺少 1 个属性。有关详细信息,请参阅原始帖子中的更新。感谢您的快速回复并帮助我找到解决方案,我真的很感激(我花了几个小时试图解决这个问题)
  • 我很乐意将您的回复标记为已接受的答案。但是——为了获得这种状态,我认为应该将 XML 精简为解决方案所必需的,减去任何不必要的属性——这样其他人就更容易识别解决方案是什么。由于您的回答描述了现在存在的 xml,我认为如果我不尝试编辑它会更好。仅供参考 - 我只使用 EditText "@+id/txt2" 创建了我的演示项目 - 它为我的帖子提供了解决方案,与我拥有的相同 imeOptions=actionDone 设置相匹配。再次感谢!
  • 好的..好吧,我现在就去标记它,这样你就可以获得声誉。答案肯定在那里。也许在您方便的时候,您可以将其删减一些,以便其他人更容易阅读(我总是先寻找简短的答案:))。新年快乐
  • 再次感谢您,也祝您新年快乐!
  • 好的,我一定会尽快将我的代码调整为所需的内容。
【解决方案3】:

这是一个似乎尚未解决的老问题。这些情况有一种解决方法。只需获取 EditText(或您可能需要的任何视图,并为其分配一个 onEditorActionListener,如下所示:

final EditText editor = (EditText) findViewById(R.id.myEditText);
editor.setOnEditorActionListener(EnterOnText);

现在像这样定义 EnterOnText 实现:

TextView.OnEditorActionListener EnterOnText = new TextView.OnEditorActionListener() {
  public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_GO) {
      final String msg = view.getText().toString();
      if (!msg.isEmpty()) {
        // Do whatever you need here
        ...
      }
    }
    return true;
  }
};

现在,只需将布局的 imeOptions 属性更改为:

android:imeOptions="actionGo"

相反,这可能会使您的模拟器的输入键不起作用,但会(应该)在所有物理设备上起作用。

【讨论】:

    【解决方案4】:
    ((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new OnEditorActionListener() {        
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId==EditorInfo.IME_ACTION_DONE){
                //do something
            }
        return false;
        }
    });
    

    【讨论】:

      【解决方案5】:

      试试这个,

          ((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
                      @Override
                      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      
          if (event != null && (actionId == EditorInfo.IME_ACTION_GO
                                  || event.getKeyCode() == event.KEYCODE_ENTER))
          {
          //do whatever you want
          }
      
                      }
                  });
      
      
      
      <EditText
          android:id="@+id/comment_text"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical"
          android:maxLines="1"
          android:imeOptions="actionGo"
          /> 
      

      【讨论】:

      • 如果您检查事件是否为空,您将忽略软输入 IME_ACTIONs。
      猜你喜欢
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 2018-04-19
      • 2010-12-14
      • 2013-04-10
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      相关资源
      最近更新 更多