【问题标题】:Android: Edit Text Go ButtonAndroid:编辑文本转到按钮
【发布时间】:2011-03-17 15:04:53
【问题描述】:

我有一个定义如下的编辑文本。

<EditText  
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text" 
android:hint="@string/field_text"
android:id="@+id/field"
/>

我想设置一个自定义命令,以便当有人单击屏幕键盘上的“完成/执行”按钮时,会单击一个按钮或仅运行该按钮运行的方法。我认为这与 ime 选项有关,但我无法弄清楚它们是如何工作的。提前感谢您的帮助!

【问题讨论】:

    标签: android edit ime


    【解决方案1】:

    你想要一个 android:imeOptions 和 setOnEditorActionListener 的组合

    <EditText android:id="@+id/some_edittext"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:imeOptions="actionSend">
    </EditText>
    
    
    some_edittext.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                some_button.performClick();
                return true;
            }
            return false;
        }
    });
    

    显然你应该将actionSend更改为你想要的动作,并相应地更新IME_ACTION_SEND。

    【讨论】:

    • 只是想跟进这个答案并提到这并不一定适用于所有设备。例如,我将 OnKeyListener 代码更改为在我的应用程序中使用 OnEditorActionListener,但突然我的 HTC Evo 停止执行该操作。有关更多信息,请参阅此内容:stackoverflow.com/questions/3886677/imeoptions-on-htc-devices
    • 但是使用这个例子你不能创建多行编辑文本。就像一个聊天文本,在这个你添加的文本不是自动Wrodwrap。
    【解决方案2】:

    看看setImeActionLabel 方法(或imeActionLabelimeActionId 属性)和setOnEditorActionListener 设置一个监听器来响应事件。

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 2019-05-25
      • 2015-07-21
      • 2016-11-13
      • 2020-07-22
      • 2016-04-27
      相关资源
      最近更新 更多