溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android項(xiàng)目中怎么顯示與隱藏軟鍵盤

發(fā)布時間:2020-11-21 17:11:13 來源:億速云 閱讀:132 作者:Leah 欄目:移動開發(fā)

今天就跟大家聊聊有關(guān)Android項(xiàng)目中怎么顯示與隱藏軟鍵盤,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

代碼如下:

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)聽
 */

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

看完上述內(nèi)容,你們對Android項(xiàng)目中怎么顯示與隱藏軟鍵盤有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI