android監(jiān)聽(tīng)軟鍵盤(pán)的彈出與隱藏的示例代碼
情境:布局文件中有ScrollView,ScrollView中有個(gè)EditView,布局底部有一個(gè)控件(見(jiàn)下面布局代碼),程序一啟動(dòng)EditView就獲取焦點(diǎn),彈出軟鍵盤(pán),將這個(gè)底部的控件也頂上去了,感覺(jué)不太好,所以我就想監(jiān)聽(tīng)下軟鍵盤(pán)彈出,此時(shí)去隱藏底部控件,軟鍵盤(pán)隱藏時(shí)則顯示底部控件。
初始:
<?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提供了使得軟鍵盤(pán)的彈出與隱藏的方式,比如
if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { //隱藏軟鍵盤(pán) getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); }
但是并未提供監(jiān)聽(tīng)軟鍵盤(pán)的彈出與隱藏的方法。
由于彈出與隱藏軟鍵盤(pán)勢(shì)必會(huì)引起layout布局的變化,監(jiān)聽(tīng)布局的變化然后計(jì)算偏移,即可算出是否時(shí)顯示或隱藏,有兩種解決方案。
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代表鍵盤(pán)的真實(shí)高度 ,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); /*// 隱藏標(biāo)題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 隱藏狀態(tài)欄 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) { //軟鍵盤(pán)隱藏啦 txt.postDelayed(new Runnable() { @Override public void run() { txt.setVisibility(View.VISIBLE); } },100); } else { ////軟鍵盤(pán)彈出啦 txt.setVisibility(View.GONE); } } }); }
題外話(huà):測(cè)試時(shí)發(fā)現(xiàn)通過(guò)設(shè)置全屏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);也可以達(dá)到相同目的,但是全屏就違背了我的初衷。個(gè)人推薦第二種方法,因?yàn)橛龅揭粋€(gè)客戶(hù)的設(shè)備在開(kāi)啟指紋識(shí)別的相冊(cè)鎖時(shí),第一種方法不好使。
在查資料的過(guò)程中看到有些開(kāi)發(fā)者希望軟鍵盤(pán)彈出時(shí)把底部控件頂上去的情形,方法同上。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中監(jiān)聽(tīng)軟鍵盤(pán)顯示狀態(tài)實(shí)現(xiàn)代碼
- Android監(jiān)聽(tīng)軟鍵盤(pán)彈出與隱藏的兩種方法
- Android項(xiàng)目類(lèi)似淘寶 電商 搜索功能,監(jiān)聽(tīng)軟鍵盤(pán)搜索事件,延遲自動(dòng)搜索,以及時(shí)間排序的搜索歷史記錄的實(shí)現(xiàn)
- Android App實(shí)現(xiàn)監(jiān)聽(tīng)軟鍵盤(pán)按鍵的三種方式
- Android 監(jiān)聽(tīng)軟鍵盤(pán)狀態(tài)的實(shí)例詳解
- Android監(jiān)聽(tīng)鍵盤(pán)狀態(tài)獲取鍵盤(pán)高度的實(shí)現(xiàn)方法
相關(guān)文章
Android Studio中導(dǎo)入module的方法(簡(jiǎn)單版)
這篇文章主要介紹了AndroidStudio中導(dǎo)入module的方法,本文是一篇簡(jiǎn)易版的教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01Android開(kāi)發(fā)實(shí)現(xiàn)圖片的上傳下載
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圖片的上傳下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android中WebView無(wú)法后退和js注入漏洞的解決方案
這篇文章主要介紹了Android中WebView無(wú)法后退和js注入漏洞解決方案,其中js注入主要針對(duì)安卓4.2及以下版本中WebView的漏洞,需要的朋友可以參考下2016-02-02Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Android O實(shí)現(xiàn)Framework層CENTER鍵長(zhǎng)按功能方法
這篇文章主要為大家介紹了Android O實(shí)現(xiàn)Framework層CENTER鍵長(zhǎng)按功能方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android App支付系列(一):微信支付接入詳細(xì)指南(附官方支付demo)
這篇文章主要介紹了Android App支付系列(一):微信支付接入詳細(xì)指南(附官方支付demo) ,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11android事件總線(xiàn)EventBus3.0使用方法詳解
這篇文章主要為大家詳細(xì)介紹了android事件總線(xiàn)EventBus3.0使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11安卓(Android)動(dòng)態(tài)創(chuàng)建多個(gè)按鈕并添加監(jiān)聽(tīng)事件
本文主要介紹Android動(dòng)態(tài)創(chuàng)建多個(gè)按鈕并給每個(gè)按鍵添加監(jiān)聽(tīng)事件,在做Android項(xiàng)目會(huì)經(jīng)常遇到的,希望對(duì)需要用到的同學(xué)有所幫助2016-07-07