android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼

情境:布局文件中有ScrollView,ScrollView中有個EditView,布局底部有一個控件(見下面布局代碼),程序一啟動EditView就獲取焦點,彈出軟鍵盤,將這個底部的控件也頂上去了,感覺不太好,所以我就想監(jiān)聽下軟鍵盤彈出,此時去隱藏底部控件,軟鍵盤隱藏時則顯示底部控件。

10余年的晉源網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整晉源建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“晉源網(wǎng)站設(shè)計”,“晉源網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

初始:

android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼

android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼      android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼

<?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);
    }

但是并未提供監(jiān)聽軟鍵盤的彈出與隱藏的方法。

由于彈出與隱藏軟鍵盤勢必會引起layout布局的變化,監(jiān)聽布局的變化然后計算偏移,即可算出是否時顯示或隱藏,有兩種解決方案。

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);
    /*// 隱藏標(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) {
        //軟鍵盤隱藏啦
          txt.postDelayed(new Runnable() {
            @Override
            public void run() {
              txt.setVisibility(View.VISIBLE);
            }
          },100);
        } else {
          ////軟鍵盤彈出啦
          txt.setVisibility(View.GONE);
        }
      }
    });
  }

題外話:測試時發(fā)現(xiàn)通過設(shè)置全屏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);也可以達到相同目的,但是全屏就違背了我的初衷。個人推薦第二種方法,因為遇到一個客戶的設(shè)備在開啟指紋識別的相冊鎖時,第一種方法不好使。

在查資料的過程中看到有些開發(fā)者希望軟鍵盤彈出時把底部控件頂上去的情形,方法同上。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站標(biāo)題:android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼
URL分享:http://www.muchs.cn/article28/ihejcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、小程序開發(fā)微信小程序、外貿(mào)建站、企業(yè)建站、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)