EditText系列(1)-焦点问题处理
来源:互联网 发布:bim软件怎么安装 编辑:程序博客网 时间:2024/06/01 12:55
转载请注明出处:
http://blog.csdn.net/user11223344abc?viewmode=contents
出自【蛟-blog】
1.概述
本文采取的是监听键盘收起和打开来监听的。即:
键盘收起的时候认定EditText失焦。
键盘打开的时候认定EditText获得焦点。
友情提示:对着代码看可能快一些:
https://github.com/zj614android/zj-fatures.git
匹配的项目是:EditText_1_Focus
2.API
- 隐藏键盘
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
- 监听键盘收起打开(这个类不是系统原生类,在文章尾部附件有贴出,是自定义的一个类)
final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(getRootView());softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() { @Override public void onSoftKeyboardOpened(int keyboardHeightInPx) { Log.e("zj", "键盘open"); } @Override public void onSoftKeyboardClosed() { //取消输入框焦点 Log.e("zj", "键盘close"); }});
- 监听EditText焦点
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean isFoucs) { Toast.makeText(getApplicationContext(), "EditText焦点改变 " + isFoucs, Toast.LENGTH_SHORT).show(); if(isFocus){ }else{ } }});
- 让EditText失去焦点
editText.clearFocus()&&父布局需要设置如下2个属性:android:focusable="true" android:focusableInTouchMode="true"
3.实现思路(伪代码)
分为2种情况:
情况(一):基本情况键盘监听接口.监听(){ if(open){ }else{ edittext.clearFocus(); }edittext.setOnFocusChangeListener(){ if(hasFocus){ }else{ }}
情况(二):需要结合点击editView外部,让EditTextView失去焦点rootView = getRootView();rootView.setOnclick(){ ...关闭键盘...}键盘监听接口.监听(){ if(open){ }else{ edittext.clearFocus(); }}edittext.setOnFocusChangeListener(){ if(hasFocus){ }else{ }}
附件
package zj.edittext_1_foucs;import android.graphics.Rect;import android.view.View;import android.view.ViewTreeObserver;import java.util.LinkedList;import java.util.List;public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener { public interface SoftKeyboardStateListener { void onSoftKeyboardOpened(int keyboardHeightInPx); void onSoftKeyboardClosed(); } private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; public SoftKeyboardStateHelper(View activityRootView) { this(activityRootView, false); } public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); //r will be populated with the coordinates of your view that area still visible. activityRootView.getWindowVisibleDisplayFrame(r); final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < 100) { isSoftKeyboardOpened = false; notifyOnSoftKeyboardClosed(); } } public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { this.isSoftKeyboardOpened = isSoftKeyboardOpened; } public boolean isSoftKeyboardOpened() { return isSoftKeyboardOpened; } /** Default value is zero (0) @return last saved keyboard height in px */ public int getLastSoftKeyboardHeightInPx() { return lastSoftKeyboardHeightInPx; } public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.add(listener); } public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.remove(listener); } private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) { this.lastSoftKeyboardHeightInPx = keyboardHeightInPx; for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardOpened(keyboardHeightInPx); } } } private void notifyOnSoftKeyboardClosed() { for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardClosed(); } } }}
另外:一个链接提供了另一种思路:
http://www.jianshu.com/p/3d31d681f4bc
阅读全文
0 0
- EditText系列(1)-焦点问题处理
- EditText的焦点处理
- EditText焦点问题
- EditText焦点问题探讨
- ListView+EditText 焦点问题
- 关于EditText焦点问题
- EditText焦点问题
- EditText的焦点问题
- EditText焦点的问题。
- EditText焦点的问题
- EditText 的焦点问题
- 焦点问题EditText
- Edittext获取焦点问题
- ActivityGroup与EditText焦点问题
- Activity中EditText焦点问题
- android之EditText焦点问题
- ListView中EditText焦点问题
- ListView中EditText焦点问题
- js通过UA判断浏览器是pc端还是手机端详解
- JSP(2)——7个动作指令
- 百炼:4083:我爱北大
- 论文阅读笔记:Recurrent recommender networks
- android shell命令
- EditText系列(1)-焦点问题处理
- Android 全屏的方案
- 【微营销】第12天 微营销实战技巧之准备微博号和微信号的具体实操
- printf函数简解!
- HDU DP记录路径
- 动态规划之最长子串(LCS)
- ZeroTier-全网畅连
- CodeM美团B轮A题 黑白树 【贪心】
- SAR卫星影像分析6月23日修水暴雨受灾情况