<LinearLayout
android:id="@+id/ll_nickName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="17dp"
android:background="@drawable/bg_nickname_bt"
android:gravity="center_vertical"
android:minWidth="236dp"
android:minHeight="57dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:text="@string/nickname"
android:textColor="#808C60F3"
android:textSize="17sp"
android:textStyle="bold" />
<EditText
android:id="@+id/et_nickName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:singleLine="true"
android:maxLines="1"
android:maxLength="30"
android:cursorVisible="true"
android:ellipsize="end"
android:layout_marginEnd="10dp"
android:layout_marginStart="17dp"
android:textColor="#8C60F3"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
binding.etNickName.requestFocus();
binding.llContent.setOnClickListener(v->{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binding.etNickName.getWindowToken(), 0);
});
binding.llNickName.setOnClickListener(v->{
binding.etNickName.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(binding.etNickName, InputMethodManager.SHOW_FORCED);
});
<activity android:name=".activity.SignUpActivity" android:screenOrientation="portrait" android:windowSoftInputMode="stateAlwaysHidden"/>
1.显示光标的同时不自动获取焦点弹出键盘
首先在Manifest文件中所在的activty节点上加android:windowSoftInputMode="stateAlwaysHidden"这条属性
然后在布局文件中的EditText加上
android:cursorVisible="true"
然后在初始化代码中加上
editText.requestFocus();
2.如何在点击父布局的时候显示小键盘,(想把点击区域扩大好交互),点击其他区域隐藏小键盘(为了优化用户体验)
binding.llContent.setOnClickListener(v->{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binding.etNickName.getWindowToken(), 0);
});
binding.llNickName.setOnClickListener(v->{
binding.etNickName.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(binding.etNickName, InputMethodManager.SHOW_FORCED);
});
llContent是整个布局文件的父布局的id
llNickName是EditeText的父布局这样设置点击区域就扩大了好交互
|