最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android軟鍵盤(pán)狀態(tài)彈出與消失的示例

 更新時(shí)間:2017年02月10日 08:40:41   作者:hello125521  
這篇文章主要介紹了本篇文章主要介紹了Android軟鍵盤(pán)狀態(tài)彈出與消失的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

最近遇到了關(guān)于軟鍵盤(pán)的問(wèn)題,需要獲取到軟鍵盤(pán)的狀態(tài),是否在顯示 ,記錄一下,方便以后查閱。網(wǎng)上常見(jiàn)的判定狀態(tài)方法

復(fù)制代碼 代碼如下:

getWindow().getAttributes().softInputMode== WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED 

來(lái)判斷軟鍵盤(pán)是否打開(kāi),若相等則為打開(kāi)。試了之后,發(fā)現(xiàn)這個(gè)只對(duì)手機(jī)自帶的鍵盤(pán)有作用,對(duì)安裝的第三方的輸入法沒(méi)有效果。

還有介紹使用InputMethodManager 來(lái)獲取鍵盤(pán)狀態(tài),代碼如下

 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 boolean isOpen=imm.isActive();//isOpen若返回true,則表示輸入法打開(kāi)

這種并不能實(shí)時(shí)獲取到鍵盤(pán)的狀態(tài),對(duì)我依然沒(méi)有效果。

后來(lái)找到的解決方法,監(jiān)聽(tīng)屏幕的變化,代碼如下:

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

/**
 * 
 * 軟鍵盤(pán)的監(jiān)聽(tīng)
 */

public class KeyBoardShowListener {
  private Context ctx;

  public KeyBoardShowListener(Context ctx) {
    this.ctx = ctx;
  }
  OnKeyboardVisibilityListener keyboardListener;

  public OnKeyboardVisibilityListener getKeyboardListener() {
    return keyboardListener;
  }

  public interface OnKeyboardVisibilityListener {


    void onVisibilityChanged(boolean visible);
  }

  public void setKeyboardListener(final OnKeyboardVisibilityListener listener, Activity activity) {
    final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);

    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

      private boolean wasOpened;

      private final int DefaultKeyboardDP = 100;

      // From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
      private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);

      private final Rect r = new Rect();

      @Override
      public void onGlobalLayout() {
        // Convert the dp to pixels.
        int estimatedKeyboardHeight = (int) TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());

        // Conclude whether the keyboard is shown or not.
        activityRootView.getWindowVisibleDisplayFrame(r);
        int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        boolean isShown = heightDiff >= estimatedKeyboardHeight;

        if (isShown == wasOpened) {
          Log.e("Keyboard state", "Ignoring global layout change...");
          return;
        }

        wasOpened = isShown;
        listener.onVisibilityChanged(isShown);
      }
    });
  }
}

用法如下:

//監(jiān)聽(tīng)軟鍵盤(pán)的狀態(tài)
new KeyBoardShowListener(Activity.this).setKeyboardListener(
    new KeyBoardShowListener.OnKeyboardVisibilityListener() {
      @Override
      public void onVisibilityChanged(boolean visible) {
        if (visible) {
          //軟鍵盤(pán)已彈出
          
        } else {
          //軟鍵盤(pán)未彈出
          
        }
      }
    }, Activity.this);

以下是可能會(huì)遇到的一些情況:

綁定軟鍵盤(pán)到EditText

edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edit, 0);

去除軟鍵盤(pán)顯示:

editMsgView.setText("");
editMsgView.clearFocus();
//close InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);

始終不彈出軟件鍵盤(pán)

復(fù)制代碼 代碼如下:

EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);

也可以:

InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()){ //這里可以判斷也可以不判斷
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0 );
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

临泽县| 锡林郭勒盟| 江安县| 墨玉县| 淮安市| 墨竹工卡县| 永寿县| 刚察县| 诸城市| 綦江县| 南丰县| 贡山| 响水县| 陇西县| 新郑市| 吴忠市| 武定县| 乐东| 来宾市| 穆棱市| 托克托县| 秭归县| 马尔康县| 基隆市| 中方县| 云浮市| 山西省| 长春市| 长泰县| 濮阳市| 南宁市| 临西县| 永寿县| 怀宁县| 五寨县| 惠州市| 宜黄县| 神池县| 赣州市| 得荣县| 宣武区|