requestFocus() 与 requestFocusFromTouch() 方法的区别

来源:互联网 发布:linux 中文字体 编辑:程序博客网 时间:2024/06/06 00:05

android中设置控件获得焦点

标签: androidbutton

2011-12-01 17:15 55277人阅读 评论(3)收藏 举报

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android中,要使控件获得焦点,需要先setFocus,再requestFocus。

以Button为例:

                btn.setFocusable(true);

                btn.setFocusableInTouchMode(true);

                btn.requestFocus();

                btn.requestFocusFromTouch();

 

//获得失去焦点的监听器

btn.setOnFocusChangeListener(new OnFocusChangeListener() {

  

  @Override

  public void onFocusChange(View v, boolean hasFocus) {

   // TODO Auto-generated method stub

   if (hasFocus) {

    btn_box.setBackgroundResource(R.drawable.book_green);

   }else {

    btn_box.setBackgroundResource(R.drawable.book);

   }   

  }

 });

------------------------------------------------------------





requestFocus() 与 requestFocusFromTouch() 方法的区别

2015-05-04 23:26 3077人阅读 评论(0)收藏 举报

 分类:

Android(46) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

两个方法都是获取焦点:


requestFocus() 源码如下:


[java] view plaincopy


print?

  1. /** 
  2.      * Call this to try to give focus to a specific view or to one of its descendants 
  3.      * and give it hints about the direction and a specific rectangle that the focus 
  4.      * is coming from.  The rectangle can help give larger views a finer grained hint 
  5.      * about where focus is coming from, and therefore, where to show selection, or 
  6.      * forward focus change internally. 
  7.      * 
  8.      * @return Whether this view or one of its descendants actually took focus. 
  9.      */  
  10.     public boolean requestFocus(int direction, Rect previouslyFocusedRect) {  
  11.         // need to be focusable  
  12.         if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||  
  13.                 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {  
  14.             return false;  
  15.         }  
  16.   
  17.   
  18.         // need to be focusable in touch mode if in touch mode  
  19.         if (isInTouchMode() &&  
  20.             (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {  
  21.                return false;  
  22.         }  
  23.   
  24.   
  25.         // need to not have any parents blocking us  
  26.         if (hasAncestorThatBlocksDescendantFocus()) {  
  27.             return false;  
  28.         }  
  29.   
  30.   
  31.         handleFocusGainInternal(direction, previouslyFocusedRect);  
  32.         return true;  
  33.     }  




http://blog.csdn.net/wangbole/article/details/45488787

http://blog.csdn.net/wangbole/article/details/45488787


requestFocusFromTouch() 源码如下:


[java] view plaincopy


print?

  1. /** 
  2.     * Call this to try to give focus to a specific view or to one of its descendants. This is a 
  3.     * special variant of {@link #requestFocus() } that will allow views that are not focuable in 
  4.     * touch mode to request focus when they are touched. 
  5.     * 
  6.     * @return Whether this view or one of its descendants actually took focus. 
  7.     * 
  8.     * @see #isInTouchMode() 
  9.     * 
  10.     */  
  11.    public final boolean requestFocusFromTouch() {  
  12.        // Leave touch mode if we need to  
  13.        if (isInTouchMode()) {  
  14.            ViewRootImpl viewRoot = getViewRootImpl();  
  15.            if (viewRoot != null) {  
  16.                viewRoot.ensureTouchMode(false);  
  17.            }  
  18.        }  
  19.        return requestFocus(View.FOCUS_DOWN);  
  20.    }  



从代码中可以看出:

requestFocus() 方法在三种情况下获取焦点不能生效。

1)对应的View不支持Focus;

2) 对应的View支持Focus,但是不支持在Touch模式下的Focus;

3) 对应的View其祖先View 设置了FOCUS_BLOCK_DESCENDANTS 标志, 阻止其子View获取焦点。


而requestFocusFromTouch() 方法设计的目的就是解决requestFocus() 在第二种不能获得焦点的情况下,Touch模式下不支持焦点,也能够获得焦点使用的。

Baidu Button BEGIN







Baidu Button END 172.16.140.12 Baidu Button BEGIN Baidu Button END







0 0
原创粉丝点击