一般的EditText是这样的:
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:imeOptions="actionDone"
/>
当点击EditText的时候会自动弹出输入法键盘,或者直接调用editText.requestFocus()使显示输入法键盘,
其中android:imeOptions指定了弹出键盘时右下角的按键的显示文字,未指定时默认为回车图标。
android:imeOptions="flagNoExtractUi" //使软键盘不全屏显示,只占用一部分屏幕
同时,这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone" //输入框右侧不带任何提示
android:imeOptions="actionGo" //右下角按键内容为'开始'
android:imeOptions="actionSearch" //右下角按键为放大镜图片,搜索
android:imeOptions="actionSend" //右下角按键内容为'发送'
android:imeOptions="actionNext" //右下角按键内容为'下一步'
android:imeOptions="actionDone" //右下角按键内容为'完成'
详细使用见 http://blog.csdn.net/caiwenfeng_for_23/article/details/37900503
当然也需要对这个按键进行设置点击事件:
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// do sth when pressed the enter key in the Soft InputMethod
}
});
但是在AlertDialog或这PoupWIndow的EditText当调用requestFocus()方法的时候却不会自动弹出键盘,
此时需要直接调用显示键盘命令,当editText获取焦点的时候。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
在必要的时候隐藏输入法界面:
//隐藏输入法界面
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(input.getWindowToken(), 0);