软键盘遮挡登录按钮的处理方法

原文链接:http://blog.csdn.net/mr_liu_gege/article/details/53169359
拾人牙慧,感谢博主分享。试验了一下,确实有效果,收藏学习。

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:id="@+id/parent_ll">

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:ems="10" >
    </EditText>

    <EditText
        android:id="@+id/userpwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Button" />

</LinearLayout>
public class MainActivity extends Activity {
	private Button btn;
	private LinearLayout parent_ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_login);
        /*ScrollView mScrollView = (ScrollView)findViewById(R.id.scrollContent);  
        mScrollView.setVerticalScrollBarEnabled(false);  
        mScrollView.setHorizontalScrollBarEnabled(false);*/ 
        parent_ll=(LinearLayout) findViewById(R.id.parent_ll);
        btn=(Button) findViewById(R.id.btn);
        addLayoutListener(parent_ll, btn);
    }
    
    /**
     *  1、获取parentView在窗体的可视区域
     *  2、获取parentView在窗体的不可视区域高度
     *  3、判断不可视区域高度
     *      1、大于100:键盘显示  获取childView的窗体坐标
     *                           算出parentView需要滚动的高度,使childView显示。
     *      2、小于100:键盘隐藏
     *
     * @param parentView 根布局
     * @param childView 需要显示的最下方View
     */
    public void addLayoutListener(final View parentView, final View childView) {
    	parentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                parentView.getWindowVisibleDisplayFrame(rect);
                int mainInvisibleHeight = parentView.getRootView().getHeight() - rect.bottom;
                if (mainInvisibleHeight > 100) {
                    int[] location = new int[2];
                    childView.getLocationInWindow(location);
                    int srollHeight = (location[1] + childView.getHeight()) - rect.bottom;
                    parentView.scrollTo(0, srollHeight);
                } else {
                	parentView.scrollTo(0, 0);
                }
            }
        });
    }
}
addLayoutListener() 这个方法是关键。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值