在开发中,常常会碰到这种情况,打开一个activity后,第一个文本框自动获得焦点,同时会弹出软键盘输入框,这样很影响用户体验,现在来看解决方法。
我们先来看看为什么会出现上述情况,原因很简单,文本框默认是会获得焦点的,获得焦点之后当然会继续弹出输入框,等待输入,针对此原因,我们可以有以下两种方案:
1、不让文本框获得焦点;
2、获得焦点不弹出输入框;
来看第一种方法,我们可以抢占文本框的焦点,如在其父窗体中加入:
01 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
02 | xmlns:tools="http://schemas.android.com/tools" |
03 | android:layout_width="match_parent" |
04 | android:layout_height="match_parent" |
05 | android:orientation="vertical" |
06 | android:focusable="true" |
07 | android:focusableInTouchMode="true" |
08 | tools:context=".MainActivity" > |
11 | android:id="@+id/etMsg" |
12 | android:layout_width="wrap_content" |
13 | android:layout_height="wrap_content" |
来看第二种方法,在activity中加入:
1 | android:windowSoftInputMode = "stateHidden" |