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