国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android軟鍵盤狀態彈出與消失的示例

2019-12-12 03:46:12
字體:
來源:轉載
供稿:網友

最近遇到了關于軟鍵盤的問題,需要獲取到軟鍵盤的狀態,是否在顯示 ,記錄一下,方便以后查閱。網上常見的判定狀態方法

復制代碼 代碼如下:

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

來判斷軟鍵盤是否打開,若相等則為打開。試了之后,發現這個只對手機自帶的鍵盤有作用,對安裝的第三方的輸入法沒有效果。

還有介紹使用InputMethodManager 來獲取鍵盤狀態,代碼如下

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

這種并不能實時獲取到鍵盤的狀態,對我依然沒有效果。

后來找到的解決方法,監聽屏幕的變化,代碼如下:

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;/** *  * 軟鍵盤的監聽 */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);      }    });  }}

用法如下:

//監聽軟鍵盤的狀態new KeyBoardShowListener(Activity.this).setKeyboardListener(    new KeyBoardShowListener.OnKeyboardVisibilityListener() {      @Override      public void onVisibilityChanged(boolean visible) {        if (visible) {          //軟鍵盤已彈出                  } else {          //軟鍵盤未彈出                  }      }    }, Activity.this);

以下是可能會遇到的一些情況:

綁定軟鍵盤到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 InputMethodManagerInputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);

始終不彈出軟件鍵盤

復制代碼 代碼如下:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐源县| 许昌市| 赤峰市| 崇信县| 油尖旺区| 若尔盖县| 图们市| 阳曲县| 蒲城县| 新宁县| 杭锦后旗| 灵丘县| 柘荣县| 宣威市| 固阳县| 乌拉特前旗| 黄陵县| 龙胜| 临城县| 界首市| 陈巴尔虎旗| 博乐市| 东山县| 淮滨县| 兴宁市| 蓝山县| 松溪县| 大足县| 苏尼特右旗| 黄陵县| 浮山县| 偃师市| 于田县| 五大连池市| 鲁甸县| 拜城县| 武冈市| 吉林省| 玉屏| 宣威市| 芦溪县|