弹出软键盘

/**
* 弹出软键盘
*/
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);
}
  • 效果图

Android控制软键盘的弹出和隐藏-LMLPHP

隐藏软键盘

/**
* 关闭软键盘
*/
public void closeKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
  • 效果图

Android控制软键盘的弹出和隐藏-LMLPHP

软键盘开关

/**
* 软键盘开关
*/
public void toggleKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);
}
  • 效果图

Android控制软键盘的弹出和隐藏-LMLPHP

Android控制软键盘的弹出和隐藏-LMLPHP

测试类

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>
04-19 17:05
查看更多