Android 多种完美解决输入法覆盖输入框的办法

    每次在输入框里输入的时候,输入法弹起来之后,就会覆盖输入框,如果在登录的时候覆盖,你会不会更气,在这里推荐几种解决的办法。

解决办法一

如果希望输入法弹出时,不自动挤压Activity, 可以在Manifest中写入一下属性:

<activity android:name=".MainActivity"
    android:windowSoftInputMode="adjustPan"
   /> 

解决办法二

在activity的onGreat里面添加一下方法:

setSoftInputMode这个方法是设置输入框不被遮挡

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

解决办法三

xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mRootView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/mLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#B2AFAF"
        android:orientation="horizontal" >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@mipmap/ic_launcher"
            android:gravity="left|center"
            android:hint="我不会被遮挡的"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:textColor="#000000"
            android:textSize="14sp" />
    </LinearLayout>
</RelativeLayout>

实现代码:这个是监听最外层和滚动来避免遮盖输入框,如果以上方法不能使用,我们就上代码解决。

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    // 最外层需要调整的Layout
    private RelativeLayout mRootView;
    // 滚动的Layout
    private LinearLayout mLine;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRootView = (RelativeLayout) findViewById(R.id.mRootView);
        mLine = (LinearLayout) findViewById(R.id.mLine);

        mRootView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //取得 rootView 可视区域
                mRootView.getWindowVisibleDisplayFrame(rect);
                //取得 rootView 不可视区域高度 (被其他View遮挡的区域高度)
                int rootInvisibleHeight = mRootView.getRootView().getHeight() - rect.bottom;
                //要是不可视区域高度大于100,则输入键盘就显示
                if (rootInvisibleHeight > 100) {
                    int[] location = new int[2];
                    //取得 scrollToInput 的坐标
                    mLine.getLocationInWindow(location);
                    //计算滚动高度(rootView),这样 (scrollToInput)在可视区域
                    int srollHeight = (location[1] + mLine.getHeight()) - rect.bottom;
                    mRootView.scrollTo(0, srollHeight);
                } else {
                    //隐藏软键盘
                    mRootView.scrollTo(0, 0);
                }
            }
        });
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值