EditText获取焦点,Android软键盘遮挡Button,

2018-09-19  本文已影响0人  majorty
<LinearLayout android:id="@+id/login_layout"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/kq_white"
              android:clipToPadding="false"
              android:fitsSystemWindows="true"
              android:orientation="vertical">

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/sv_content_ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

           //此处是你编写的登录布局文件,正常写都可以,没什么特殊的


            <Button
                android:id="@+id/login_btn"
                style="?borderlessButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="@dimen/kq_dp45"
                android:layout_below="@id/pw_layout"
                android:layout_marginLeft="@dimen/kq_dp20"
                android:layout_marginRight="@dimen/kq_dp20"
                android:layout_marginTop="@dimen/kq_dp45"
                android:background="@drawable/kq_select_button_common_bg"
                android:gravity="center"
                android:text="@string/kq_loginInfo"
                android:textColor="@color/kq_white"
                android:textSize="16sp"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
public void initView(){
 layout = findViewById(R.id.login_layout);
         //键盘显示,触摸键盘以外隐藏键盘
        findViewById(R.id.sv_content_ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                return false;
            }
        });
        layout.getViewTreeObserver().addOnGlobalLayoutListener(gLayoutListener);
}
 ViewTreeObserver.OnGlobalLayoutListener gLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

            View focus = layout.findFocus();
            ScrollView sv = ((ScrollView) findViewById(R.id.scroll));
            sv.fullScroll(ScrollView.FOCUS_DOWN);

            if (focus != null && focus instanceof EditText) {//保证滑动之后 焦点依然未变
                focus.requestFocus();
            }
        }
    };

    @Override
    public void onDestroy() {

        layout.getViewTreeObserver().removeOnGlobalLayoutListener(gLayoutListener);
        gLayoutListener = null;
        super.onDestroy();
    }
 <activity
            android:name=".activity.LoginActivity"
            android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
            android:screenOrientation="portrait"
            android:theme="@style/kq_loginTheme"
            android:windowSoftInputMode="adjustResize|stateHidden"/>
 <style name="kq_loginTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <!--手机状态栏颜色-->
        <item name="colorPrimaryDark">@color/kq_login_blue</item>
        <item name="windowActionBar">false</item>
        <item name="android:color">@color/kq_white</item>
        <item name="android:windowDisablePreview">true</item>
    </style>
<!--此处注意千万不能有该属性  <item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>-->
上一篇下一篇

猜你喜欢

热点阅读