在上篇文章中,我们知道了软键盘软键盘其实是一个Dialog.InputMethodService为我们的输入法创建了一个Dialog和如何调整软键盘的显示。接下来,我们将会了解到更多关于软键盘的知识。
自动弹出软键盘
在我们修改信息或者搜索,修改密码等界面的时候,用户进入这个界面的主要目的就是输入修改/查找 某些信息,为了用户体验应该自动弹出软键盘而不是让用户主动点击输入框才弹出。
private void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, 0);
}
对于界面比较复杂的情况的时候,软键盘可能无法正常的弹出,需要延迟加载。即在界面加载完成之后,弹出软键盘。
使用定时器schedule:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
InputMethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
}
}, 998);
使用handler:
可参考图1-1
可参考图1-2
不自动弹出软键盘
带有EditText控件的在第一次显示的时候会自动获得focus,并弹出键盘,如果不想自动弹出键盘,有两种方法:
方法一:在mainfest文件中把对应的activity设置
android:windowSoftInputMode="stateHidden" 或者android:windowSoftInputMode="stateUnchanged"。
方法二:可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus。
注意TextView不要设置Visiable=gone,否则会失效,可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus。
android:id="@+id/text_notuse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
/>
TextView textView = (TextView)findViewById(R.id.text_notuse);
textView.requestFocus();
EditText设置ScrollView压缩背景图片解决办法
在你的Activity里加上
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
动态关闭软键盘
方法一:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(YourView.getWindowToken(), 0);
方法二:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 得到InputMethodManager的实例
if (imm.isActive()) {
// 如果开启
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_NOT_ALWAYS);
// 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
}
软键盘界面按钮功能设置方法
在android发开发程中,有时候需要对EditText的软键盘进行监听。
当点击软键盘回车位置按键的时候,需要实现完成、前进、下一项、搜索、发送或其他功能,这就需要开发者对软键盘回车的点击事件进行捕捉。
比如在登录界面,需要用户在输入密码之后点击软键盘回车直接登录,不必再去点击屏幕上的登录按钮。我们就可以在密码使用的EditText设置 android:imeOptions=”actionDone”,然后对EditText设置OnEditorActionListener监听,当捕捉到用户点击完成时,调用登录方法即可。(IME英文全称Input Method Editors,中文名称输入法编辑器)
先看一个demo,布局文件如下:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:id="@+id/actionDoneEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionDone"
android:hint="actionDone" />
android:id="@+id/actionGoEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionGo"
android:hint="actionGo" />
android:id="@+id/actionNextEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionNext"
android:hint="actionNext" />
android:id="@+id/actionNoneEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionNone"
android:hint="actionNone" />
android:id="@+id/actionPreviousEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionPrevious"
android:hint="actionPrevious" />
android:id="@+id/actionSearchEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionSearch"
android:hint="actionSearch" />
android:id="@+id/actionUnspecifiedEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionUnspecified"
android:hint="actionUnspecified" />
android:id="@+id/actionSendEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:inputType="text"
android:imeOptions="actionSend"
android:hint="actionSend" />
注意一:
使用android:imeOptions属性的时候,一定要对EditText设置 android:inputType 或者 设置 android:singleline=”true”。
在activity_main.xml文件中,定义了8个EditText,imeOptions分别是:
actionDone 完成 对应 EditorInfo.IME_ACTION_DONE
actionGo 前进 对应 EditorInfo.IME_ACTION_GO
actionNext 下一项 对应 EditorInfo.IME_ACTION_NEXT
actionNone 无动作 对应 EditorInfo.IME_ACTION_NONE
actionPrevious 上一项 对应 EditorInfo.IME_ACTION_PREVIOUS
actionSearch 搜索 对应 EditorInfo.IME_ACTION_SEARCH
actionUnspecified 未指定 对应 EditorInfo.IME_ACTION_UNSPECIFIED
actionSend 发送 对应 EditorInfo.IME_ACTION_SEND
在MainActivity中:
public class MainActivity extends Activity implements TextView.OnEditorActionListener {
private EditText mActionDoneEditText;
private EditText mActionGoEditText;
private EditText mActionNextEditText;
private EditText mActionNoneEditText;
private EditText mActionPreviousEditText;
private EditText mActionSearchEditText;
private EditText mActionSendEditText;
private EditText mActionUnspecifiedEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mActionDoneEditText = (EditText) findViewById(R.id.actionDoneEditText);
mActionGoEditText = (EditText) findViewById(R.id.actionGoEditText);
mActionNextEditText = (EditText) findViewById(R.id.actionNextEditText);
mActionNoneEditText = (EditText) findViewById(R.id.actionNoneEditText);
mActionPreviousEditText = (EditText) findViewById(R.id.actionPreviousEditText);
mActionSearchEditText = (EditText) findViewById(R.id.actionSearchEditText);
mActionSendEditText = (EditText) findViewById(R.id.actionSendEditText);
mActionUnspecifiedEditText = (EditText) findViewById(R.id.actionUnspecifiedEditText);
mActionDoneEditText.setOnEditorActionListener(this);
mActionGoEditText.setOnEditorActionListener(this);
mActionNextEditText.setOnEditorActionListener(this);
mActionNoneEditText.setOnEditorActionListener(this);
mActionPreviousEditText.setOnEditorActionListener(this);
mActionSearchEditText.setOnEditorActionListener(this);
mActionSendEditText.setOnEditorActionListener(this);
mActionUnspecifiedEditText.setOnEditorActionListener(this);
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
doWhichOperation(actionId);
Log.e("BALLACK", "event: " + event);
Log.e("BALLACK", "v.getImeActionId(): " + v.getImeActionId());
Log.e("BALLACK", "v.getImeOptions(): " + v.getImeOptions());
Log.e("BALLACK", "----------------------------------------------");
return true;
}
private void doWhichOperation(int actionId) {
switch (actionId) {
case EditorInfo.IME_ACTION_DONE:
Log.e("BALLACK", "IME_ACTION_DONE");
break;
case EditorInfo.IME_ACTION_GO:
Log.e("BALLACK", "IME_ACTION_GO");
break;
case EditorInfo.IME_ACTION_NEXT:
Log.e("BALLACK", "IME_ACTION_NEXT");
break;
case EditorInfo.IME_ACTION_NONE:
Log.e("BALLACK", "IME_ACTION_NONE");
break;
case EditorInfo.IME_ACTION_PREVIOUS:
Log.e("BALLACK", "IME_ACTION_PREVIOUS");
break;
case EditorInfo.IME_ACTION_SEARCH:
Log.e("BALLACK", "IME_ACTION_SEARCH");
break;
case EditorInfo.IME_ACTION_SEND:
Log.e("BALLACK", "IME_ACTION_SEND");
break;
case EditorInfo.IME_ACTION_UNSPECIFIED:
Log.e("BALLACK", "IME_ACTION_UNSPECIFIED");
break;
default:
break;
}
}