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

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

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

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

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

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

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

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

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

這種并不能實(shí)時(shí)獲取到鍵盤的狀態(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;

/**
 * 
 * 軟鍵盤的監(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)軟鍵盤的狀態(tài)
new KeyBoardShowListener(Activity.this).setKeyboardListener(
    new KeyBoardShowListener.OnKeyboardVisibilityListener() {
      @Override
      public void onVisibilityChanged(boolean visible) {
        if (visible) {
          //軟鍵盤已彈出
          
        } else {
          //軟鍵盤未彈出
          
        }
      }
    }, Activity.this);

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

綁定軟鍵盤到EditText

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

去除軟鍵盤顯示:

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

始終不彈出軟件鍵盤

復(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)論

宝丰县| 临夏市| 元江| 平远县| 峡江县| 如皋市| 宜兰县| 邢台市| 牟定县| 七台河市| 桦南县| 瓦房店市| 黄大仙区| 鲁甸县| 台北县| 左权县| 威海市| 岢岚县| 辉南县| 翼城县| 顺平县| 宜兰市| 巩留县| 板桥市| 大理市| 泌阳县| 收藏| 来宾市| 延吉市| 同心县| 昭通市| 绥化市| 和田市| 柘城县| 通化市| 安顺市| 石楼县| 株洲市| 翼城县| 固镇县| 枣阳市|