安卓之软键盘监听与切换软键盘状态和重新获取EditText焦点

最近在工作中的时候遇到了关于使用ScrollView作为外层点击内部EditText弹出软键盘滑动页面的问题,百度了好久各种教程,但是对于我的页面来说没有一个有用的,自己花了1天时间终于搞定了,今天在这里分享一下我的解决方案。(这篇文章是我当时写在博客园上,此次将其搬家,已经过去一年了代码中可能存在问题,下期修改出完整版,欢迎大家评论)

下面先贴出布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.croshe.loans.activity.AddContactsActivity">
    <ScrollView
        android:id="@+id/scrollContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadeScrollbars="true"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/backGround"
            android:fitsSystemWindows="true">

            <LinearLayout
                android:id="@+id/llItem"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="visible">

                <TextView
                    android:id="@+id/lld_add_two_contacts"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/handAdd"
                    android:textSize="20sp" />

                <LinearLayout
                    android:id="@+id/ll_add_contacts_add"
                    android:layout_width="200dp"
                    android:layout_height="50dp"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/hand_add_contacts"
                    android:gravity="center"
                    android:layout_marginBottom="20dp">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:scaleType="centerCrop"
                        android:src="@mipmap/icon_add_contacts" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:text="添加"
                        android:textColor="@color/white"
                        android:textSize="18sp" />

                </LinearLayout>

                <com.croshe.android.base.views.list.CrosheRecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                </com.croshe.android.base.views.list.CrosheRecyclerView>

                <include
                    android:id="@+id/llAdds"
                    layout="@layout/hand_add_contacts"
                    android:visibility="gone" />


                <Button
                    android:id="@+id/btn_confirmAuth"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="10dp"
                    android:visibility="gone"
                    android:background="@drawable/login_button"
                    android:hint="提交认证"
                    android:textColorHint="@color/white"
                    android:textSize="18sp" />
            </LinearLayout>

        </LinearLayout>

    </ScrollView>
</LinearLayout>

接下来在你的AndroidManifast.xml中设置给此Activity添加android:windowSoftInputMode=“adjustResize”(网上有的教程说这样做就可以滑动了,但是我的还是不能滑动,这个属性的意思是给软键盘留出位置,但是可能是我用了框架的原因,所以我的页面并不会滑动)

由于我的页面还不能滑动所以接下来将是最终版解决方案:
百度了一下软键盘监听类,非常好用
package com.croshe.loans.util;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;

/**
 * 软键盘监听类
 * Created by *** on 2018/5/5 14:04.
 */

  

      public class SoftKeyBoardListener {
            private View rootView;//activity的根视图
            int rootViewVisibleHeight;//纪录根视图的显示高度
            private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;

    public SoftKeyBoardListener(Activity activity) {
        //获取activity的根视图
        rootView = activity.getWindow().getDecorView();

        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获取当前根视图在屏幕上显示的大小
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);

                int visibleHeight = r.height();

                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
                if (rootViewVisibleHeight == visibleHeight) {
                    return;
                }

                //根视图显示高度变小超过200,可以看作软键盘显示了
                if (rootViewVisibleHeight - visibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度变大超过200,可以看作软键盘隐藏了
                if (visibleHeight - rootViewVisibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

            }
        });
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }

        public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
            SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
            softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
        }
    }

在你的Activity的onCreate方法中监听就可以了
监听方法如下

/**
 * 监听软键盘弹出隐藏
 */
SoftKeyBoardListener.setListener(AddContactsActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
    //软键盘弹出时执行的方法
    @Override
    public void keyBoardShow(int height) {
    //此方法为我需要隐藏页面中视图方法可忽略
        viewGone();
     //设置软键盘切换状态,防止出错
        mhideSoftInput = true;
        //滑动ScrollView至底部
        mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
    //定义变量nunEditText判断用户点击了那个EditText,因为滑动ScrollView后会失去焦点,所以需要重新聚焦
        if(numEditText!=0){
            switch (numEditText){
                case 1:
            //聚焦方法
                    etName.requestFocus();
                    break;
                case 2:
            //聚焦方法
                    etPhone.requestFocus();
                    break;
                case 3:
            //聚焦方法
                    etRelationShip.requestFocus();
                    break;
            }
        }
    }
    //软键盘隐藏时执行的方法
    @Override
    public void keyBoardHide(int height) {

        viewVisble();
     //定义该变量为全局变量用于控制软键盘弹出隐藏的判断
        mhideSoftInput = false;
    }
});

//软键盘状态切换方法

/**
 * 切换软键盘状态
 */
public void hideSoftInput(){
    if(mhideSoftInput) {
        mhideSoftInput = false;
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            // 如果开启
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                    InputMethodManager.HIDE_NOT_ALWAYS);
            // 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
        }
    }else{
        return;
    }
}

这里有几个地方需要注意:
1>如果不需要切换请去除mhideSoftInput这个成员变量相关的所有代码例如切换软键盘状态代码和软键盘弹出隐藏时设置mhideSoftInput=true等
2>需要切换软键盘的话则需要定义成员变量mhideSoftInput
如还有问题,欢迎大家评论告知,一起探讨更好的解决方案。
萌新第一次发博客,心情有点小激动-

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值