今天来介绍软键盘遮挡住登录Button的方法,在登录和注册的时候界面最下方往往会有一个Button,但是用户在输入框中输入的时候软键盘很可能就会把一部分的输入框和Button遮挡了,在网上查了相关的资料解决的办法有很多种,但是都有不尽人意的地方(就我查到的相关解决方案),最后自己总结出了一个我觉得还可以的方案,现在分享给大家。直接上代码,里面主要部分都已经注释了。
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden" >
android:windowSoftInputMode=”stateHidden”这个主要是在进入activity阻止自动弹出软键盘。
layout.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="输入框1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框4" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框5" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框6" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框7" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框8" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框9" />
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入框10" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>
</ScrollView>
布局中主要的是ScrollView,这样当软键盘显示的时候可以通过ScrollView来滑动界面。
MainActivity
public class MainActivity extends Activity implements OnTouchListener {
private ScrollView scrollView;
private EditText editText;
private Handler handler;
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
editText = (EditText) findViewById(R.id.edittext);
scrollView = (ScrollView) findViewById(R.id.scroll);
layout = (LinearLayout) findViewById(R.id.layout);
handler = new Handler();
editText.setOnTouchListener(this);
layout.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.edittext:
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//让scrollview划到最下方
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}, 100);
break;
case R.id.layout:
//点击空白处软键盘隐藏
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
return imm.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), 0);
default:
break;
}
return false;
}
}
在用户点击最下方的edittext的时候ScrollView就会划到最下方,这样就能把Button完全显示出来了。OK到此为止,已经完全解决了软键盘遮挡Button。