网上搜了很多方案,结果都不靠谱,以下是比较常见的不靠谱方案:
1、在EditText前面放置一个隐藏的线性布局:
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"
/>
2、在manifest中对activity进行配置
<activity ... android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />
3、在EditText的父级控件中设置
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
下面是比较靠谱的方案:
在EditText面前,增加一个大小的0的EditText,目的是让它先抢走焦点,因为大小为0,所以用户也看不见,可以完美解决问题
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="0dp"
android:layout_height="0dp" />
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>