美文网首页
EditText获取焦点,Android软键盘遮挡Button,

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

作者: majorty | 来源:发表于2018-09-19 23:16 被阅读0次
    • ①先上效果图,如果不是你想要的效果,下边的可以不看了^^


      qq_pic_merged_1537369244838.jpg
    • ②下面是xml文件
    <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>
    
    • ③核心java代码
    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();
        }
    
    • ④AndroidMainfest.xml文件中Activity配置
     <activity
                android:name=".activity.LoginActivity"
                android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
                android:screenOrientation="portrait"
                android:theme="@style/kq_loginTheme"
                android:windowSoftInputMode="adjustResize|stateHidden"/>
    
    • ⑤ style.xml文件(手机状态栏颜色请根据你自己的设置)
     <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>-->
    
    • ⑥end...如果你还没解决,一定仔细检查代码了

    相关文章

      网友评论

          本文标题:EditText获取焦点,Android软键盘遮挡Button,

          本文链接:https://www.haomeiwen.com/subject/fabwnftx.html