Android项目中怎么显示与隐藏软键盘
今天就跟大家聊聊有关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项目中怎么显示与隐藏软键盘有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
当前名称:Android项目中怎么显示与隐藏软键盘
分享路径:http://scyingshan.cn/article/gcpies.html