在开发中有时候会遇到这样的需求,首页中的fragment中有输入框,输入框需要有光标显示,但是不需要显示软键盘,当用户手动点击输入框时才出来软键盘可以输入内容。如何实现?
1、在xml文件中设置 cursorVisible 属性。
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:textColor="@color/colorBlack"
android:textCursorDrawable="@drawable/my_cursor"
android:background="@null"
android:textSize="16sp" />
2、在Java代码中设置默认获取焦点。
// 显示输入框的光标
etInput.requestFocus();
3、在AndroidManifest.xml中设置 fragment的宿主Activity的 windowSoftInputMode 属性为 stateAlwaysHidden
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:windowSoftInputMode="stateAlwaysHidden"/>
搞定。