Android edittext 键盘 弹出问题总结
2017-03-16 15:40
405 查看
项目中用到软键盘的管理总结如下:
一、在开始进入页面时不弹出键盘
方法一:在包含EditText外层布局上添加
抢在EditText获取焦点,即可
原理 view touch source:
即一个子view想要获取焦点必须满足clickable, focusable, focusableInTouchMode 属性为true,缺一不可.
方法二:在onResume中加入这行代码
二、软键盘出现把原来的布局底部拦布局给顶上去的方法解决办法:
二·二、软键盘出现 覆盖原来的布局:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar"
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan" >
1,绑定软键盘到EditText:
2,点击空白区域去除EditText软键盘显示:
重写Activity dispatchTouch
仨、EditText始终不弹出软件键盘
EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);
还可以
1,在设置软键盘弹出属性界面加载后,软键盘不能弹出,不能弹出软键盘的主要原因是Android程序未将屏幕绘制完成,所以延迟一定时间,弹出软键盘。
方法一:
方法二:
清单文件:
< activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
该Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间
三、软键盘打开,关闭监控:来源girhub:
使用监控:
一、在开始进入页面时不弹出键盘
方法一:在包含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"
二·二、软键盘出现 覆盖原来的布局:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar"
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan" >
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
@Override public 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); }; }; @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if(visible){ hander.sendEmptyMessageDelayed(0, 1000); } }
@Override public void onWindowFocusChanged(boolean hasWindowFocus) { if(visible){ hander.sendEmptyMessageDelayed(0, 1000); } }
方法二:
Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public 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(); } }
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 EditText不弹出输入法总结,焦点问题的总结
- Android EditText不弹出输入法焦点问题的总结
- Android EditText不弹出输入法焦点问题的总结
- Android EditText不弹出输入法焦点问题的总结
- Android EditText不弹出输入法焦点问题的总结
- Android EditText不弹出输入法总结,焦点问题的总结
- Android EditText不弹出输入法总结,焦点问题的总结
- Android EditText不弹出输入法总结,焦点问题的总结
- Android EditText不弹出输入法焦点问题的总结
- Android EditText不弹出输入法总结,焦点问题的总结
- Android EditText不弹出输入法焦点问题的总结
- Android EditText不弹出输入法焦点问题的总结
- Android N(7.0) 在ListView里显示EditText时软键盘弹出时会自动切换到全键盘的问题?
- Android的EditText自动获取焦点并弹出输入法问题
- android edittext不弹出软键盘
- android强制隐藏键盘(当edittext获得焦点时不弹出键盘)
- android EditText 设置弹出数字输入法键盘
- Android EditText默认不弹出软件键盘
- 【Android开发那点破事】Android EditText不弹出输入法焦点问题
- [Android]去除EditText自动获得的焦点,解决软键盘弹出问题