1.简述
本文将提及的是软键盘的使用技巧,包括一些初始进入的小细节还有一些常见的应用场景。
应用场景包括 1. 登陆注册界面时由于屏幕原因,弹出的软键盘会遮住登陆注册按钮
2.评论时输入文字框居于软件盘上的效果时
2.软件盘细节
细节描述
1. 初次进入含有文本框的界面时,光标闪烁
2. 软键盘弹出,页面跳转,可能导致抖动
3. 软键盘将标题toolbar顶出去了
解决方式
让EditText的父亲布局抢占其焦点,可以有效解决进入界面输入框光标闪烁问题
<RelativeLayout
android:layout_gravity="center_horizontal"
android:layout_marginRight="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="@+id/account"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/bg_border_color_black"
>
.......
<EditText
android:id="@+id/etAccount"
android:layout_width="match_parent"
android:textColor="@color/white"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:textColorHint="@color/white"
android:hint="请输入您的手机号"
android:textSize="13sp"
android:background="@null"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/accountIcon"
tools:ignore="RtlCompat" />
</RelativeLayout>
延迟退出软键盘抗拒可能会出现的抖动
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 隐藏软键盘 imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
// 延迟个0.1秒是因为 可能推出太快导致软键盘刚刚被回收,前面的界面出现断层
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
规避底部的文本框不太接近弹出框的方法:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- *********** 核心,设置居于底部,paddingBottom 保持一定的距离**********-->
<EditText
android:layout_alignParentBottom="true"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_marginTop="300dp"
android:background="@drawable/bg_comment"
android:layout_marginLe