Android解决软键盘遮挡Button

今天来介绍软键盘遮挡住登录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。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值