
输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示:

输入密码时输入框被系统键盘遮挡了,大大降低了用户操作体验,这就是开发中非常常见的软键盘遮挡的问题,该如何解决?
二、简单解决方案
方法一
在你的activity中的oncreate中setContentView之前写上这个代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);方法二
android:windowSoftInputMode="stateVisible|adjustResize"这样会让屏幕整体上移。如果加上的 是 android:windowSoftInputMode="adjustPan"这样键盘就会覆盖屏幕。
<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >在这设置的值(除"stateUnspecified"和"adjustUnspecified"以外)将覆盖在主题中设置的值
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:orientation="vertical"></LinearLayout></ScrollView>但这些方法虽然比较简单,但往往都有一定的局限性不是很灵活,有时达不到预期效果,大家可以试试或许也能解决你的问题,下面就教大家一种具有代码可控性的一种方法:
<com.jereh.overidelinearlayout.LinearLayoutView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:id="@+id/login_root_layout"android:layout_height="match_parent"android:orientation="vertical"><!—这里模仿一个登录界面--><LinearLayoutandroid:id="@+id/login_layout_logo"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="10"android:background="#ff0000"android:orientation="vertical" ><ImageViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:src="@drawable/login_logo"android:scaleType="fitXY"/></LinearLayout><!—输入框和密码框--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_marginTop="20dp"android:layout_weight="3"android:orientation="vertical" ><EditTextandroid:id="@+id/editText1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_alignParentBottom="true"android:layout_gravity="center_vertical"android:hint="用户名"android:ems="10" ><requestFocus /></EditText><EditTextandroid:id="@+id/editText1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_alignParentBottom="true"android:layout_gravity="center_vertical"android:hint="密码"android:ems="10" ><requestFocus /></EditText></LinearLayout></com.jereh.overidelinearlayout.LinearLayoutView>可以看出关键地方在于LinearLayoutView这个自定义组件
protected void onSizeChanged(int w,final int h, int oldw,final int oldh) {super.onSizeChanged(w, h, oldw, oldh);uiHandler.post(new Runnable() {public void run() {if (oldh - h > SOFTKEYPAD_MIN_HEIGHT){ // 软键盘关闭keyBordStateListener.stateChange(KEYBORAD_SHOW);//回调方法显示部分区域}else { // 软键盘弹出 if(keyBordStateListener != null){keyBordStateListener.stateChange(KEYBORAD_HIDE);// 回调方法隐藏部分区域}}}});} ②提供KeyBordStateListener 接口采用回调机制调用接口的实现方法。public interface KeyBordStateListener{ public void stateChange(int state);}//定义接口private KeyBordStateListener keyBordStateListener;public void setKeyBordStateListener(KeyBordStateListener keyBordStateListener) {this.keyBordStateListener = keyBordStateListener;}LinearLayoutView组件的完整代码:public class LinearLayoutView extends LinearLayout{public static final int KEYBORAD_HIDE = 0;public static final int KEYBORAD_SHOW = 1;private static final int SOFTKEYPAD_MIN_HEIGHT = 50;private Handler uiHandler = new Handler();public LinearLayoutView(Context context) {super(context);}public LinearLayoutView(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onSizeChanged(int w,final int h, int oldw,final int oldh) {// TODO Auto-generated method stubsuper.onSizeChanged(w, h, oldw, oldh);uiHandler.post(new Runnable() {@Overridepublic void run() {if (oldh - h > SOFTKEYPAD_MIN_HEIGHT){ keyBordStateListener.stateChange(KEYBORAD_SHOW);}else { if(keyBordStateListener != null){keyBordStateListener.stateChange(KEYBORAD_HIDE);}}}});}private KeyBordStateListener keyBordStateListener;public void 、setKeyBordStateListener(KeyBordStateListener keyBordStateListener) {this.keyBordStateListener = keyBordStateListener;}public interface KeyBordStateListener{ public void stateChange(int state);}3.主界面MainActivitypublic class MainActivity extends Activity implements KeyBordStateListener {private LinearLayoutView resizeLayout;private LinearLayout logoLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获得可根据软键盘的弹出/关闭而隐藏和显示某些区域的LinearLayoutView组件 resizeLayout = (LinearLayoutView) findViewById(R.id.login_root_layout);//获得要控制隐藏和显示的区域logoLayout = (LinearLayout) findViewById(R.id.login_layout_logo);resizeLayout.setKeyBordStateListener(this);//设置回调方法}//实现接口中的方法,该方法在resizeLayout的onSizeChanged方法中调用@Overridepublic void stateChange(int state) {// TODO Auto-generated method stubswitch (state) {case LinearLayoutView.KEYBORAD_HIDE:logoLayout.setVisibility(View.VISIBLE);break;case LinearLayoutView.KEYBORAD_SHOW:logoLayout.setVisibility(View.GONE);break;}}四、实现效果
键盘关闭:

以上所述是小编给大家介绍的Android软键盘遮挡的四种完美解决方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!