Java Android setOnEditorActionListener()不';t火

Java Android setOnEditorActionListener()不';t火,java,android,ime,Java,Android,Ime,当按下回车按钮时,我试图将侦听器设置为EditText,但根本没有启动。我用Android 4.2.2在LG Nexus 4上测试了这个setOnEditorActionListener在亚马逊Kindle Fire上使用Android 2.3,而setImeActionLabel在任何地方都不起作用!我也无法设置Enter按钮的文本。下面是代码: mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED); m

当按下回车按钮时,我试图将侦听器设置为
EditText
,但根本没有启动。我用Android 4.2.2在LG Nexus 4上测试了这个
setOnEditorActionListener
在亚马逊Kindle Fire上使用Android 2.3,而
setImeActionLabel
在任何地方都不起作用!我也无法设置
Enter
按钮的文本。下面是代码:

mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
            return true;
        }  
    });

我做错了什么?如何修复此问题?

您可以尝试使用

你可以用


确保在布局文件中设置了IME_操作:

<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />


有关完整的解释,请参见。

我知道了,它可以在活动中的Edittext上工作,但不能在Fragement中工作

<EditText
                android:id="@+id/mEditText"
                android:imeOptions="actionSearch"
                android:imeActionLabel="搜索"
                android:inputType="text"
                android:singleLine="true"
                android:textSize="18dp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:paddingLeft="3dp"
                android:paddingRight="3dp"
                android:textColor="@color/black"
                android:layout_marginTop="7dp"
                android:layout_marginBottom="7dp"
                android:shadowColor="#4b000000"
                android:shadowDy="1"
                android:shadowDx="1"
                android:shadowRadius="1"
                android:cursorVisible="true"
                android:textCursorDrawable="@null"
                android:gravity="center_vertical|left"
                android:background="@color/transparent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                tools:ignore="UnusedAttribute">
            <requestFocus/>
        </EditText>


mEditText.setImeActionLabel("搜索", EditorInfo.IME_ACTION_SEARCH);
    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                MToastUtil.show("搜索");
            }
            return false;
        }
    });

mEditText.setImeActionLabel(“搜索", EditorInfo.IME\u ACTION\u SEARCH);
mEditText.setOnEditorActionListener(新的TextView.OnEditorActionListener(){
@凌驾
公共布尔onEditorAction(TextView v、int actionId、KeyEvent事件){
if(actionId==EditorInfo.IME\u ACTION\u SEARCH){
MToastUtil.show(“搜索");
}
返回false;
}
});

再搜索一段时间后-这就是我的解决方案():

只需卸下
imeAction

确保:

inputType=“text”


imeOptions=“actionSend”

对我有效的是,我已将这一行添加到EditText的下面

android:imeOptions="actionSend"
这一行使当点击编辑文本时弹出的键盘具有发送按钮而不是搜索按钮

在setOnEditorActionListener中,您重写以下方法以查找操作发送

@Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
        //implement stuff here
          }
        }

在xml文件中,将tag
android:inputType=“text”
添加到EditText

我使用以下代码

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/ic_magnify"
    android:layout_centerVertical="true"
    android:textSize="15sp"
    android:textColor="#000"
    android:id="@+id/input_search"
    android:inputType="text"
    android:background="@null"
    android:hint="Enter Address, City or Zip Code"
    android:imeOptions="actionSearch"/>

mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {

                if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
                        || keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

                        geoLocate();

                }

                return false;
            }
        });

mSearchText.setOnEditorActionListener(新的TextView.OnEditorActionListener(){
@凌驾
公共布尔onEditorAction(TextView v、int actionId、KeyEvent KeyEvent){
如果(actionId==EditorInfo.IME_ACTION_SEARCH | | actionId==EditorInfo.IME_ACTION_DONE
||keyEvent.getAction()==keyEvent.ACTION_向下| | keyEvent.getAction()==keyEvent.KEYCODE_回车){
地理定位();
}
返回false;
}
});

我在我的日志中有这样的记录:Action ID=66KeyEvent=KeyEvent{Action=Action\u UP,keyCode=keyCode\u ENTER,scanCode=0,metaState=0,flags=0x6,repeatCount=0,eventTime=653776952,down=653776952,deviceId=-1,source=0x0}我正在使用我的代码读取文档以查看。onkeylister,“这只对硬件键盘有用;软件输入法没有义务触发此侦听器。“谢谢。它可以工作。但您不知道为什么。setImeActionLabel和setOnEditorActionListener在Nexus 4上不工作,但在其他设备上工作?Android碎片魔术。对于我的Galaxy Nexus,它们也不适用于我。只有当您设置singleLine=“true”时,imeOptions才起作用“在编辑文本上。看起来很奇怪很烦人,这可能是出于设计,但这就是我发现的。对我来说,即使在ET属性中添加了singleLine=“true”之后,它也不起作用。我注意到的设备是Nexus6、Nexus4和MotoG。它们的相似之处在于Android 5.0!所以,如果有人遇到同样的问题。请提供答案。对于Kotlin,条件如下:
if(s[s.length-1]='\n')
和此行:android:inputType=“text”Add
android:imeOptions=“actionSend”
@Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
        //implement stuff here
          }
        }
<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/ic_magnify"
    android:layout_centerVertical="true"
    android:textSize="15sp"
    android:textColor="#000"
    android:id="@+id/input_search"
    android:inputType="text"
    android:background="@null"
    android:hint="Enter Address, City or Zip Code"
    android:imeOptions="actionSearch"/>

mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {

                if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
                        || keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

                        geoLocate();

                }

                return false;
            }
        });