Android EditText 设置键盘 搜索,回车

android的EditText在输入的时候,软键盘的回车键,可以变成搜索键、回车键、下一项或完成,这些是怎么设置的,又是怎么响应的呢。


在Layout文件中,对EditText的设置如下:
加上属性:

android:imeOptions=""

这个属性的可输入的值如图所示;
举几个例子分别代表的含义:

actionSearch: 搜索
actionSend: 发送
actionNext: 下一项

常用的还是以下几种属性以及替换的文本外观,我就不一一列举出来了,大家有兴趣可以亲自动手,加深印象。

参数 作用 方法
actionUnspecified 未指定 EditorInfo.IME_ACTION_UNSPECIFIED
actionNone 动作 EditorInfo.IME_ACTION_NONE
actionGo 去往 EditorInfo.IME_ACTION_GO
actionSearch 搜索 EditorInfo.IME_ACTION_SEARCH
actionSend 发送 EditorInfo.IME_ACTION_SEND
actionNext 下一项 EditorInfo.IME_ACTION_NEXT
actionDone 完成 EditorInfo.IME_ACTION_DONE

同样,也可在activity代码中设置imeOptions:

editText.setImeOptions(EditorInfo.IME_ACTION_SEND);  

然后,在activity代码中添加,imeOptions的监听。
方法一:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
public boolean onEditorAction(TextView v, int actionId,                   KeyEvent event)  {                          
if (actionId==EditorInfo.IME_ACTION_SEND ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) 
{                
//do something;              
return true;             
}               
return false;           
}       
});

方法二:

emailPwd.setOnKeyListener(View.OnKeyListener(){
    onKey(View v, keyCode, KeyEvent event) {
        (keyCode == KeyEvent.){

            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.);

            (imm.isActive()){

                imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), );

            }

            ;

        }

        ;


}
});

android键盘中的enter键图标是可以用EditText的android:imeOptions标签变更的。
显示search图标需要设置为android:imeOptions="actionSearch",android:inputType="text"将键盘设置为文字输入布局
则键盘中search按钮正常出现。
捕捉编辑框软键盘enter事件:

1)setOnKeyListener
2)OnEditorActionListener

你可能感兴趣的:(Android EditText 设置键盘 搜索,回车)