如何在Android软键盘中添加Go按钮及其功能?

8
我希望你能在Android应用软键盘中添加“Go”按钮,以便于搜索和其他相关场景。请问有没有人可以提供示例并指导我如何实现这个功能?非常感谢任何帮助。

我以为这是自动的... - confiq
请参考几天前的这个问题:https://dev59.com/SHE85IYBdhLWcg3w3Xr6 - Christopher Orr
4个回答

26

最终我使用了...

EditText SearchEditText =(EditText)findViewById(R.id.txtMapSearch); 
SearchEditText.setOnEditorActionListener(new OnEditorActionListener(){  

    @Override 
    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) { 
        if(arg1 == EditorInfo.IME_ACTION_SEARCH)  
        { 
            // search pressed and perform your functionality.
        }
        return false; 
    } 

}); 

2
只是补充一点,如果你的键盘在右下角显示一个“完成”按钮,请使用EditorInfo.IME_ACTION_DONE。 - Rorchackh
文档链接:http://developer.android.com/training/keyboard-input/style.html#Action - Heath Borders

16
如果您的问题是有一个EditText或可编辑TextView,并且您想让软键盘上的操作右按钮显示为“Go”,那么请将此属性添加到您的EditText / TextView中。
android:imeActionLabel="actionGo"

请注意,它还必须是单行的TextView,否则操作按钮将成为回车选择器(一个箭头)。

android:singleLine="true" 

实际上,我想执行的功能类似于在软键盘按钮上按下活动中的搜索按钮。 - UMAR-MOBITSOLUTIONS
@Jim Blackler 当用户点击键盘上的actionGo按钮时,我该如何执行操作? - Idrizi.A

2
我用了。
android:imeOptions="actionGo" 

为了处理 go 操作,我使用了...
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO || actionId == EditorInfo.IME_ACTION_DONE) {
                    //your functionality 

                    // hide virtual keyboard
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(etSearch.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);

                    return true;
                }
                return false;
            }
        });

1

我正在为“send”做同样的事情:

在您的布局中使用此类:

public class ActionEditText extends EditText { public ActionEditText(Context context) { super(context); }

public ActionEditText(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ActionEditText(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
{
    InputConnection conn = super.onCreateInputConnection(outAttrs);
    outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
    return conn;
}

}

在XML中:
<com.test.custom.ActionEditText
            android:id="@+id/postED"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="top|left"
            android:hint="@string/msg_type_message_here"
            android:imeOptions="actionSend"
            android:inputType="textMultiLine"
            android:maxLines="5"
            android:padding="5dip"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:textColor="@color/white"
            android:textSize="20sp" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接