在MainActivity中,点击添加按钮,在当前的MainActivity中出现一个编辑框和软键盘
/**
* EditText获取焦点并显示软键盘
*/
public static void showSoftInputFromWindow(Activity activity, EditText editText) {
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
点击事件处理:如果你的EditText已经写好布局。
protected void showSoftKeyBoard(){
etRelationship.setFocusable(true);
etRelationship.setFocusableInTouchMode(true);
etRelationship.requestFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etRelationship,0);
}
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@color/page_background"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击出现输入框"
/>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/white"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/dp_15dp"
android:visibility="invisible"
/>
2.在AndroidManifest.xml设置 android:windowSoftInputMode="adjustResize"
android:name=".ui.MainActivity"
android:exported="false"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
在 MainActivity里面 点击button方法里面设置
mEdit.setVisiable(View.VISIBLE);
KeyboardUtils.showSoftInput(this, mEdit);
其中 KeyboardUtils.showSoftInput()方法:
public static void showSoftInput(final Context context, final View view) {
InputMethodManager inputMethodManager = ((InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE));
if (inputMethodManager != null) {
if (!inputMethodManager.isActive()) {
inputMethodManager.toggleSoftInput(0, HIDE_NOT_ALWAYS);
}
inputMethodManager.showSoftInput(view, 0);
}
}
以上, 如果觉得ok , 请给分 谢谢
AndroidManifest.xml中windowSoftInputMode="adjustResize"
代码中button.onclick{edit.setVisibility = visible;
public static void showKb(Context context){
try {
InputMethodManager inputMethodManager =
(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception e) {
BCBLog.d("", e);
}
}
}