温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android项目中怎么显示与隐藏软键盘

发布时间:2020-11-21 17:11:13 来源:亿速云 阅读:131 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关Android项目中怎么显示与隐藏软键盘,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

代码如下:

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

看完上述内容,你们对Android项目中怎么显示与隐藏软键盘有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI