弹出软键盘
前提:必须要有一个可以编辑的控件(EditText),并且当前已经获取焦点
/**
* 弹出软键盘
*/
public void openKeyboard(View view) {
// 获取焦点
editText2.setFocusable(true);
editText2.setFocusableInTouchMode(true);
editText2.requestFocus();
// 弹出软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText2, 0);
}
参数说明
/**
* Synonym for {@link #showSoftInput(View, int, ResultReceiver)} without a result receiver: explicitly request that the current input method's soft input area be shown to the user, if needed.
*
* @param view The currently focused view, which would like to receive soft keyboard input.
* @param flags Provides additional operating flags. Currently may be 0 or have the {@link #SHOW_IMPLICIT} bit set.
*/
public boolean showSoftInput(View view, int flags) {
return showSoftInput(view, flags, null);
}
- 效果图
隐藏软键盘
/**
* 关闭软键盘
*/
public void closeKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
hideSoftInputFromWindow()方法的参数是一个IBinder窗口令牌,可以用View.getWindowToken()从附加到窗口的View对象上获得,大部分情况下,每个事件的回调方法都会有一个引用指向正在编辑的TextView,或者是发起事件的View(比如某个按钮),通过调用这些对象获得窗口令牌,再将其传递给InputMethodManager最方便的做法。
- 效果图
软键盘开关
开启软键盘的时候隐藏软键盘,隐藏软键盘的时候开启软键盘
/**
* 软键盘开关
*/
public void toggleKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);
}
弹出键盘的时候是一个道理,想要编辑哪个EditText,要先让其获取焦点
- 效果图
测试类
package com.kongqw.kqwkeysetdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText editText;
private EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
editText2 = (EditText) findViewById(R.id.edittext2);
}
/**
* 弹出软键盘
*/
public void openKeyboard(View view) {
// 获取焦点
editText2.setFocusable(true);
editText2.setFocusableInTouchMode(true);
editText2.requestFocus();
// 弹出软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText2, 0);
}
/**
* 关闭软键盘
*/
public void closeKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
/**
* 软键盘开关
*/
public void toggleKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);
}
}
页面布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_below="@+id/edittext"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext2"
android:onClick="openKeyboard"
android:text="弹出软键盘" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:onClick="closeKeyboard"
android:text="关闭软键盘" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:onClick="toggleKeyboard"
android:text="开关" />
</RelativeLayout>