EditText获取焦点,Android软键盘遮挡Button,登录页面遇到的坑

  • ①先上效果图,如果不是你想要的效果,下边的可以不看了^^


    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...如果你还没解决,一定仔细检查代码了
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,566评论 2 59
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,218评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,937评论 0 17
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,511评论 1 45
  • 自出生起,我就只有一个身份,就是学生。全日制学生就和一份工作一样,有绩效测评,有上下班,有同僚。虽然很多人不赞同,...
    花花骚年阅读 2,701评论 1 1

友情链接更多精彩内容