android 软键盘的弹出问题总结
来源:互联网 发布:配音培训机构知乎 编辑:程序博客网 时间:2024/05/18 00:26
项目中用到软键盘的管理总结如下:
在开始进入页面时不弹出键盘
方法一:在包含EditText外层布局上添加
android:focusable="true"android:focusableInTouchMode="true"
抢在EditText获取焦点,即可
原理 view touch source:
public boolean onTouchEvent(MotionEvent event) { ... if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) { ... if (isFocusable() && isFocusableInTouchMode() && !isFocused()) { focusTaken = requestFocus(); } ... } ...}
即一个子view想要获取焦点必须满足clickable, focusable, focusableInTouchMode 属性为true,缺一不可.
方法二:在onResume中加入这行代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
软键盘出现把原来的布局底部拦布局给顶上去的方法解决办法:
android:windowSoftInputMode="adjustPan|stateHidden"
1,绑定软键盘到EditText:
edit.setFocusable(true);edit.setFocusableInTouchMode(true);edit.requestFocus();InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(edit, 0);
2,点击空白区域去除EditText软键盘显示:
重写Activity dispatchTouch
@Overridepublic boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { View v = getCurrentFocus(); if ( v instanceof EditText) { Rect outRect = new Rect(); v.getGlobalVisibleRect(outRect); if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) { v.clearFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } } return super.dispatchTouchEvent( event );}
EditText始终不弹出软件键盘
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 ); }
1,在设置软键盘弹出属性界面加载后,软键盘不能弹出,不能弹出软键盘的主要原因是Android程序未将屏幕绘制完成,所以延迟一定时间,弹出软键盘。
方法一:
private Handler hander=new Handler(){public void handleMessage(android.os.Message msg) {edit.setFocusable(true);edit.setFocusableInTouchMode(true);edit.requestFocus();InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(edit, 0);};};
@Overridepublic void onWindowFocusChanged(boolean hasWindowFocus) {if(visible){hander.sendEmptyMessageDelayed(0, 1000);}}
方法二:
Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {InputMethodManager m = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);}}, 300);
<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
该Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间
软键盘打开,关闭监控:来源girhub:
import android.app.Activity;import android.os.Build;import android.view.View;import android.view.ViewTreeObserver;import android.view.Window;import android.view.WindowManager;import java.lang.ref.WeakReference;/** * Created by froger_mcs on 21/03/16. */public class KeyboardWatcher { private WeakReference<Activity> activityRef; private WeakReference<View> rootViewRef; private WeakReference<OnKeyboardToggleListener> onKeyboardToggleListenerRef; private ViewTreeObserver.OnGlobalLayoutListener viewTreeObserverListener; public KeyboardWatcher(Activity activity) { activityRef = new WeakReference<>(activity); initialize(); } public void setListener(OnKeyboardToggleListener onKeyboardToggleListener) { onKeyboardToggleListenerRef = new WeakReference<>(onKeyboardToggleListener); } public void destroy() { if (rootViewRef.get() != null) if (Build.VERSION.SDK_INT >= 16) { rootViewRef.get().getViewTreeObserver().removeOnGlobalLayoutListener(viewTreeObserverListener); } else { rootViewRef.get().getViewTreeObserver().removeGlobalOnLayoutListener(viewTreeObserverListener); } } private void initialize() { if (hasAdjustResizeInputMode()) { viewTreeObserverListener = new GlobalLayoutListener(); rootViewRef = new WeakReference<>(activityRef.get().findViewById(Window.ID_ANDROID_CONTENT)); rootViewRef.get().getViewTreeObserver().addOnGlobalLayoutListener(viewTreeObserverListener); } else { throw new IllegalArgumentException(String.format("Activity %s should have windowSoftInputMode=\"adjustResize\"" + "to make KeyboardWatcher working. You can set it in AndroidManifest.xml", activityRef.get().getClass().getSimpleName())); } } private boolean hasAdjustResizeInputMode() { return (activityRef.get().getWindow().getAttributes().softInputMode & WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) != 0; } private class GlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener { int initialValue; boolean hasSentInitialAction; boolean isKeyboardShown; @Override public void onGlobalLayout() { if (initialValue == 0) { initialValue = rootViewRef.get().getHeight(); } else { if (initialValue > rootViewRef.get().getHeight()) { if (onKeyboardToggleListenerRef.get() != null) { if (!hasSentInitialAction || !isKeyboardShown) { isKeyboardShown = true; onKeyboardToggleListenerRef.get().onKeyboardShown(initialValue - rootViewRef.get().getHeight()); } } } else { if (!hasSentInitialAction || isKeyboardShown) { isKeyboardShown = false; rootViewRef.get().post(new Runnable() { @Override public void run() { if (onKeyboardToggleListenerRef.get() != null) { onKeyboardToggleListenerRef.get().onKeyboardClosed(); } } }); } } hasSentInitialAction = true; } } } public interface OnKeyboardToggleListener { void onKeyboardShown(int keyboardSize); void onKeyboardClosed(); }}
使用监控:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize" />
keyboardWatcher = new KeyboardWatcher(this);
keyboardWatcher.setListener(this);
- android 软键盘的弹出问题总结
- 关于Android软键盘弹出的问题
- Android软键盘弹出问题的解决方法
- Android 弹出软键盘的问题
- Android Dialog弹出时的软键盘弹出问题
- Android软键盘弹出,界面整体上移的问题
- Android软键盘弹出,界面整体上移的问题
- android关于EditText弹出软键盘的问题
- Android中软键盘弹出时关于布局的问题
- Android软键盘弹出,界面整体上移的问题
- android 一进入view就弹出软键盘的问题
- Android中软键盘弹出时关于布局的问题
- Android—软键盘弹出时关于布局的问题
- android弹出软键盘时,editText被遮挡的问题
- Android软键盘弹出,界面整体上移的问题
- Android软键盘弹出,界面整体上移的问题
- Android软键盘弹出,界面整体上移的问题
- Android软键盘弹出,界面整体上移的问题
- ADODB连接数据库,通过msado15组件实现,VC2005
- 搭建phoneGap开发环境事项
- 多域名ThinkPHP利用MEMCACHE方式共享SESSION数据
- 根据已知字符串遍历整个数据库查找它所在的表和列位置以及出现的次数
- mongoDB简单使用
- android 软键盘的弹出问题总结
- HDU 1056 HangOver
- 《Android系统学习》第十章:Android消息处理、消息循环和消息队列
- Shell编程与vim控制命令
- HDU 1106 排序
- vi实用命令
- makefile
- 详解yii用户登录体系
- ThinkPHP使用总结