android监听软键盘的弹出与隐藏的示例代码
程序员文章站
2023-12-19 12:28:52
情境:布局文件中有scrollview,scrollview中有个editview,布局底部有一个控件(见下面布局代码),程序一启动editview就获取焦点,弹出软键盘,...
情境:布局文件中有scrollview,scrollview中有个editview,布局底部有一个控件(见下面布局代码),程序一启动editview就获取焦点,弹出软键盘,将这个底部的控件也顶上去了,感觉不太好,所以我就想监听下软键盘弹出,此时去隐藏底部控件,软键盘隐藏时则显示底部控件。
初始:
<?xml version="1.0" encoding="utf-8"?> <linearlayout android:id="@+id/activity_main" 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" android:fitssystemwindows="true" tools:context="com.test.myapplication.mainactivity"> <textview android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!"/> <linearlayout android:id="@+id/lin" android:background="#0000ff" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <scrollview android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <edittext android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout> </scrollview> </linearlayout> <textview android:layout_margin="10dp" android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!"/> </linearlayout>
android api提供了使得软键盘的弹出与隐藏的方式,比如
if(getwindow().getattributes().softinputmode==windowmanager.layoutparams.soft_input_state_unspecified) { //隐藏软键盘 getwindow().setsoftinputmode(windowmanager.layoutparams.soft_input_state_hidden); }
但是并未提供监听软键盘的弹出与隐藏的方法。
由于弹出与隐藏软键盘势必会引起layout布局的变化,监听布局的变化然后计算偏移,即可算出是否时显示或隐藏,有两种解决方案。
1、自定义view,修改onlayout()方法,比如
public class resizelayout extends linearlayout { private inputlistener mlistener; public interface inputlistener { void oninputlistener(boolean ishideinput); } public void setonresizelistener(inputlistener l) { mlistener = l; } public resizelayout(context context, attributeset attrs) { super(context, attrs); } private boolean mhasinit = false; private boolean mhaskeyboard = false; private int mheight; @override protected void onlayout(boolean changed, int l, int t, int r, int b) { // todo auto-generated method stub super.onlayout(changed, l, t, r, b); if (!mhasinit) { mhasinit = true; mheight = b; system.out.println("mheight= " + b); } else { mheight = mheight < b ? b : mheight; } if (mhasinit && mheight > b) { // mheight代表键盘的真实高度 ,b代表在窗口中的高度 mheight>b mhaskeyboard = true; mlistener.oninputlistener(false); } if (mhasinit && mhaskeyboard && mheight == b) { // mheight = b mhaskeyboard = false; mlistener.oninputlistener(true); } }
2、在activity中获取viewgroup的高度变化
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /*// 隐藏标题栏 requestwindowfeature(window.feature_no_title); // 隐藏状态栏 getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);*/ setcontentview(r.layout.activity_main); final linearlayout lin = (linearlayout) findviewbyid(r.id.lin); final textview txt = (textview) findviewbyid(r.id.txt); lin.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { rect rect = new rect(); lin.getwindowvisibledisplayframe(rect); int rootinvisibleheight = lin.getrootview().getheight() - rect.bottom; log.d(tag, "lin.getrootview().getheight()=" + lin.getrootview().getheight() + ",rect.bottom=" + rect.bottom + ",rootinvisibleheight=" + rootinvisibleheight); if (rootinvisibleheight <= 100) { //软键盘隐藏啦 txt.postdelayed(new runnable() { @override public void run() { txt.setvisibility(view.visible); } },100); } else { ////软键盘弹出啦 txt.setvisibility(view.gone); } } }); }
题外话:测试时发现通过设置全屏getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);也可以达到相同目的,但是全屏就违背了我的初衷。个人推荐第二种方法,因为遇到一个客户的设备在开启指纹识别的相册锁时,第一种方法不好使。
在查资料的过程中看到有些开发者希望软键盘弹出时把底部控件顶上去的情形,方法同上。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。