Android几行代码解决键盘遮挡问题

简介: 1.scrollTo、scrollBy实现键盘不遮挡; 2.列表中的Edittext,利用假键盘实现键盘不遮挡

GitHub

APK

将libray模块复制到项目中,或者直接在build.gradle中依赖:

allprojects {
        repositories {
            
            maven { url 'https://jitpack.io' }
        }
    }
dependencies {
               compile 'com.github.AnJiaoDe:Keyboard:V1.0.0'

    }

注意:如果sync报错,是因为和com.android.tools.build:gradle 3.0有关,
可以改将compile改为implementation 或者api

1.scrollTo、scrollBy实现键盘不遮挡

1.gif

使用方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"

        android:layout_height="44dp"
        android:background="@color/colorPrimary">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="提现"
            android:textColor="#ffffff" />
    </RelativeLayout>


    <LinearLayout
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E9F4FB"
        android:orientation="vertical">



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="38dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"

            android:gravity="center_vertical"
            android:orientation="horizontal">

            <View
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="可提余额:"
                    android:textColor="#333333"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_yue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2000.00"
                    android:textColor="#666666"
                    android:textSize="14sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="元"
                    android:textColor="#333333"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="手续费(0.6%):"
                    android:textColor="#333333"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_shouxufei"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0.00"
                    android:textColor="#666666"
                    android:textSize="14sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="元"
                    android:textColor="#333333"
                    android:textSize="14sp" />
            </LinearLayout>


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="提现方式:"
                    android:textColor="#333333"
                    android:textSize="15sp" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:text="支付宝"
                    android:textColor="#666666"
                    android:textSize="14sp" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:text="银行卡"
                    android:textColor="#666666"
                    android:textSize="14sp" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="20dp"


                android:background="@color/line" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="11dp"
                android:gravity="center"
                android:text="申请提现金额"
                android:textColor="#333333"
                android:textSize="15sp" />


            <LinearLayout
                android:id="@+id/layout_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="23dp"
                android:layout_marginRight="23dp"
                android:layout_marginTop="13dp"
                android:orientation="vertical">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"/>
                <EditText
                    android:id="@+id/et_jine"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="30dp"
                    android:background="@null"
                    android:gravity="center"
                    android:hint="请输入提现金额"
                    android:inputType="numberDecimal|number"
                    android:maxLength="20"
                    android:minHeight="44dp"
                    android:textColorHint="#999999"
                    android:textSize="14sp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="30dp"
                    android:background="#A7ABB1" />

            </LinearLayout>


            <Button
                android:id="@+id/tv_submit"
                android:layout_width="match_parent"
                android:layout_height="49dp"
                android:layout_marginLeft="38dp"
                android:layout_marginRight="38dp"
                android:layout_marginTop="33dp"
                android:gravity="center"
                android:text="确定"
                android:textColor="@color/white"
                android:textSize="16sp" />
        </LinearLayout>


    </LinearLayout>

</LinearLayout>
public class EditTextActivity extends KeyboardActivity {
    private View layout_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);

        layout_content=findViewById(R.id.layout_content);
        //设置监听器
        setOnCYGlobalLayoutListener(new OnCYGlobalLayoutListener() {
            //键盘刚显示
            @Override
            public void onKeyboardShowedNow(Rect rect) {
                //rect 可视区域
                //screenHight-rect.bottom就是键盘高度
                layout_content.scrollTo(0,screenHight-rect.bottom);

            }
            //键盘刚隐藏,可以对输入的内容进行修改


            @Override
            public void onKeyboardHideNow() {
                layout_content.scrollTo(0,0);


            }
        });
    }
}

2.列表中的Edittext,利用假键盘实现键盘不遮挡

2.gif

使用方法:

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


    <RelativeLayout
        android:background="@color/colorPrimary"

        android:layout_width="match_parent"
        android:layout_height="44dp">


        <TextView
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="match_parent"
            android:text="修改价格"
            android:textColor="#ffffff" />
    </RelativeLayout>


    <com.cy.cyrvadapter.recyclerview.VerticalRecyclerView
        android:id="@+id/vrv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <View
        android:id="@+id/view_keyboard"
        android:layout_width="match_parent"
        android:layout_height="0dp" />


</LinearLayout>
public class RVEdittextActivity extends KeyboardMockActivity {
    private RVAdapter<String> rvAdapter;

    private View view_keybord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rvedittext);

        List<String> list = new ArrayList<>();
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        list.add("dojij");
        rvAdapter = new RVAdapter<String>(list) {
            @Override
            public void bindDataToView(RVViewHolder holder, int position, String bean, boolean isSelected) {

            }

            @Override
            public int getItemLayoutID(int position, String bean) {
                return R.layout.item_price_modify;
            }

            @Override
            public void onItemClick(int position, String bean) {

            }
        };
        //万能适配器:https://github.com/AnJiaoDe/RecyclerViewAdapter
        ((VerticalRecyclerView) findViewById(R.id.vrv)).setAdapter(rvAdapter);
        //设置监听器
        setOnCYGlobalLayoutListener(view_keybord, new OnCYGlobalLayoutListener() {
            //键盘刚显示
            @Override
            public void onKeyboardShowedNow(Rect rect) {

            }

            //键盘刚隐藏,可以对输入的内容进行修改
            @Override
            public void onKeyboardHideNow() {


            }

            //假键盘高度
            @Override
            public int getViewKeyboardHeight(Rect rect) {
                return screenHight-rect.bottom;//screenHight-rect.bottom就是键盘高度
            }
        });
    }
}

源码:

/**
 * Created by cy on 2018/9/6.,用在普通界面中的edittext
 */

public abstract class KeyboardActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener {


    public int screenHight = 0;
    private OnCYGlobalLayoutListener onCYGlobalLayoutListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        screenHight = ScreenUtils.getScreenHeight(getApplicationContext());
    }

    @Override
    protected void onResume() {
        super.onResume();
        addGlobal();
    }

    @Override
    protected void onStop() {
        super.onStop();
        removeGlobal();
    }

    private void addGlobal() {
        if (onCYGlobalLayoutListener!=null)
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(this);

    }

    private void removeGlobal() {
        getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);

    }

    @Override
    public void onGlobalLayout() {

//        CYLogUtils.log("布局变化", "---------------------------------------");
        Rect r = new Rect();
        //获取当前界面可视部分
        getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
        //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。
//        int heightDifference = screenHeight - (r.bottom - r.top);
        boolean isKeyboardShowing = r.bottom < ScreenUtils.getScreenHeight(getApplicationContext());

        if (isKeyboardShowing) {
//            Toast.makeText(this, "键盘显示", Toast.LENGTH_SHORT).show();
            removeGlobal();

            onCYGlobalLayoutListener.onKeyboardShowedNow(r);
            //会导致布局不断刷新,无限制回调当前方法,所以做延迟回调处理

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addGlobal();
                }
            }, 100);
        } else {
//            Toast.makeText(this, "键盘隐藏", Toast.LENGTH_SHORT).show();

            removeGlobal();
            onCYGlobalLayoutListener.onKeyboardHideNow();

            //会导致布局不断刷新,无限制回调当前方法,所以做延迟回调处理

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addGlobal();
                }
            }, 100);



        }

    }

    public int getScreenHight() {
        return screenHight;
    }

    /**
     * @param onCYGlobalLayoutListener
     */
    public void setOnCYGlobalLayoutListener( OnCYGlobalLayoutListener onCYGlobalLayoutListener) {
        this.onCYGlobalLayoutListener = onCYGlobalLayoutListener;

        addGlobal();
    }

    public interface OnCYGlobalLayoutListener {
        public void onKeyboardShowedNow(Rect rect);//键盘刚显示

        public void onKeyboardHideNow();//键盘刚隐藏

    }
}
/**
 * Created by cy on 2018/9/6.,假键盘,用在列表中的edittext
 */

public abstract class KeyboardMockActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener {

    private View view_keyboard;

    public int screenHight = 0;
    private OnCYGlobalLayoutListener onCYGlobalLayoutListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        screenHight = ScreenUtils.getScreenHeight(getApplicationContext());
    }

    @Override
    protected void onResume() {
        super.onResume();
        addGlobal();
    }

    @Override
    protected void onStop() {
        super.onStop();
        removeGlobal();
    }

    private void addGlobal() {
        if (view_keyboard == null) return;
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(this);

    }

    private void removeGlobal() {
        getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);

    }

    @Override
    public void onGlobalLayout() {

        if (view_keyboard == null) {
            removeGlobal();
            return;
        }
//        CYLogUtils.log("布局变化", "---------------------------------------");
        Rect r = new Rect();
        //获取当前界面可视部分
        getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
        //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。
//        int heightDifference = screenHeight - (r.bottom - r.top);
        boolean isKeyboardShowing = r.bottom < ScreenUtils.getScreenHeight(getApplicationContext());

        if (isKeyboardShowing) {
            if (view_keyboard.getVisibility() == View.VISIBLE) return;
//            Toast.makeText(this, "键盘显示", Toast.LENGTH_SHORT).show();
            removeGlobal();


            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view_keyboard.getLayoutParams();

            layoutParams.height = onCYGlobalLayoutListener.getViewKeyboardHeight(r);

            view_keyboard.setVisibility(View.VISIBLE);


            onCYGlobalLayoutListener.onKeyboardShowedNow(r);

            //会导致布局不断刷新,无限制回调当前方法,所以做延迟回调处理

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addGlobal();
                }
            }, 100);
        } else {
//            Toast.makeText(this, "键盘隐藏", Toast.LENGTH_SHORT).show();
            if (view_keyboard.getVisibility() == View.GONE) return;

            removeGlobal();
            view_keyboard.setVisibility(View.GONE);

            onCYGlobalLayoutListener.onKeyboardHideNow();
            //会导致布局不断刷新,无限制回调当前方法,所以做延迟回调处理

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addGlobal();
                }
            }, 100);


        }

    }

    public int getScreenHight() {
        return screenHight;
    }

    /**
     * @param view_keyboard            假键盘布局,使用时必须自行设置其高度,以满足 需要
     * @param onCYGlobalLayoutListener
     */
    public void setOnCYGlobalLayoutListener(View view_keyboard, OnCYGlobalLayoutListener onCYGlobalLayoutListener) {
        this.view_keyboard = view_keyboard;
        this.onCYGlobalLayoutListener = onCYGlobalLayoutListener;

        addGlobal();
    }

    public interface OnCYGlobalLayoutListener {
        public void onKeyboardShowedNow(Rect rect);//键盘刚显示

        public void onKeyboardHideNow();//键盘刚隐藏

        public int getViewKeyboardHeight(Rect rect);//返回假键盘的高度,rect 可视区域
    }
}

GitHub

关注专题Android开发常用开源库
简书

微信公众号
这里写图片描述

QQ群
这里写图片描述

目录
相关文章
|
6天前
|
存储 Java Android开发
🔥Android开发大神揭秘:从菜鸟到高手,你的代码为何总是慢人一步?💻
在Android开发中,每位开发者都渴望应用响应迅速、体验流畅。然而,代码执行缓慢却是常见问题。本文将跟随一位大神的脚步,剖析三大典型案例:主线程阻塞导致卡顿、内存泄漏引发性能下降及不合理布局引起的渲染问题,并提供优化方案。通过学习这些技巧,你将能够显著提升应用性能,从新手蜕变为高手。
15 2
|
30天前
|
JSON JavaScript 前端开发
Android调用Vue中的JavaScript代码
Android调用Vue中的JavaScript代码
17 3
|
1月前
|
安全 Java 网络安全
Android远程连接和登录FTPS服务代码(commons.net库)
很多文章都介绍了FTPClient如何连接ftp服务器,但却很少有人说如何连接一台开了SSL认证的ftp服务器,现在代码来了。
74 2
|
2月前
|
存储 Java Android开发
🔥Android开发大神揭秘:从菜鸟到高手,你的代码为何总是慢人一步?💻
【7月更文挑战第28天】在Android开发中,每位开发者都追求极致的用户体验。然而,“代码执行慢”的问题时常困扰着开发者。通过案例分析,我们可探索从新手到高手的成长路径。
34 3
|
1月前
|
Java Android开发
Android项目架构设计问题之要提升代码的可读性和管理性如何解决
Android项目架构设计问题之要提升代码的可读性和管理性如何解决
26 0
|
2月前
|
API Android开发
Android 监听Notification 被清除实例代码
Android 监听Notification 被清除实例代码
|
3月前
|
JavaScript 前端开发 Android开发
kotlin安卓在Jetpack Compose 框架下使用webview , 网页中的JavaScript代码如何与native交互
在Jetpack Compose中使用Kotlin创建Webview组件,设置JavaScript交互:`@Composable`函数`ComposableWebView`加载网页并启用JavaScript。通过`addJavascriptInterface`添加`WebAppInterface`类,允许JavaScript调用Android方法如播放音频。当页面加载完成时,执行`onWebViewReady`回调。
|
2月前
|
Web App开发 JavaScript 前端开发
Android端使用WebView注入一段js代码实现js调用android
Android端使用WebView注入一段js代码实现js调用android
46 0
|
4月前
|
移动开发 监控 Android开发
构建高效Android应用:从内存优化到电池寿命代码之美:从功能实现到艺术创作
【5月更文挑战第28天】 在移动开发领域,特别是针对Android系统,性能优化始终是关键议题之一。本文深入探讨了如何通过细致的内存管理和电池使用策略,提升Android应用的运行效率和用户体验。文章不仅涵盖了现代Android设备上常见的内存泄漏问题,还提出了有效的解决方案,包括代码级优化和使用工具进行诊断。同时,文中也详细阐述了如何通过减少不必要的后台服务、合理管理设备唤醒锁以及优化网络调用等手段延长应用的电池续航时间。这些方法和技术旨在帮助开发者构建更加健壮、高效的Android应用程序。
|
3月前
|
存储 算法 Java
Android 进阶——代码插桩必知必会&ASM7字节码操作
Android 进阶——代码插桩必知必会&ASM7字节码操作
173 0