所有的view控件有一个findFocus方法,这个方法如下

  1. /**
  2. * Find the view in the hierarchy rooted at this view that currently has
  3. * focus.
  4. *
  5. * @return The view that currently has focus, or null if no focused view can
  6. * be found.
  7. */
  8. public View findFocus() {
  9. return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
  10. }

大概意思就是,获得当前试图下,拥有焦点的控件

我们可以验证下它的具体使用

看demo

xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:padding="20dp"
  7. tools:context="com.example.testcode.MainActivity" >
  8.  
  9. <TextView
  10. android:id="@+id/tv"
  11. android:layout_width="match_parent"
  12. android:layout_height="200dp"
  13. android:layout_marginBottom="10dp"
  14. android:clickable="true"
  15. android:focusable="true"
  16. android:textSize="30sp"
  17. android:focusableInTouchMode="true"
  18. android:background="@drawable/select"
  19. android:textColor="#ffffff" />
  20.  
  21. <LinearLayout
  22. android:id="@+id/aaa"
  23. android:layout_width="match_parent"
  24. android:layout_height="0dp"
  25. android:layout_marginBottom="10dp"
  26. android:layout_weight="1"
  27. android:orientation="horizontal" >
  28.  
  29. <Button
  30. android:id="@+id/bt_1"
  31. android:layout_width="60dp"
  32. android:layout_height="60dp"
  33. android:layout_marginLeft="5dp"
  34. android:background="@drawable/select"
  35. android:focusableInTouchMode="true"
  36. android:textColor="#ffffff" />
  37.  
  38. <Button
  39. android:id="@+id/bt_2"
  40. android:layout_width="60dp"
  41. android:layout_height="60dp"
  42. android:layout_marginLeft="5dp"
  43. android:background="@drawable/select"
  44. android:focusableInTouchMode="true"
  45. android:textColor="#ffffff" />
  46.  
  47. <Button
  48. android:id="@+id/bt_3"
  49. android:layout_width="60dp"
  50. android:layout_height="60dp"
  51. android:layout_marginLeft="5dp"
  52. android:background="@drawable/select"
  53. android:focusableInTouchMode="true"
  54. android:textColor="#ffffff" />
  55.  
  56. <Button
  57. android:id="@+id/bt_4"
  58. android:layout_width="60dp"
  59. android:layout_height="60dp"
  60. android:layout_marginLeft="5dp"
  61. android:background="@drawable/select"
  62. android:focusableInTouchMode="true"
  63. android:textColor="#ffffff" />
  64. </LinearLayout>
  65.  
  66. <Button
  67. android:id="@+id/bt"
  68. android:layout_width="100dp"
  69. android:layout_height="100dp"
  70. android:background="@drawable/select" />
  71.  
  72. </LinearLayout>

activity

  1. package com.example.testcode;
  2. import com.example.testcode.R.id;
  3.  
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11.  
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15.  
  16. public class MainActivity extends Activity {
  17. private TextView textView;
  18. private Button button;
  19. private LinearLayout relativeLayout;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. textView = (TextView) findViewById(R.id.tv);
  26. button = (Button)findViewById(R.id.bt);
  27. relativeLayout = (LinearLayout)findViewById(R.id.aaa);
  28. textView.setText("当前焦点所在view");
  29. button.setText("显示当前focus");
  30.  
  31. button.setOnClickListener(new OnClickListener() {
  32.  
  33. @Override
  34. public void onClick(View arg0) {
  35. // TODO Auto-generated method stub
  36. View view = relativeLayout.findFocus();
  37. textView.setText(""+view);
  38. }
  39. });
  40.  
  41. }
  42.  
  43. @Override
  44. public boolean onCreateOptionsMenu(Menu menu) {
  45. // Inflate the menu; this adds items to the action bar if it is present.
  46. getMenuInflater().inflate(R.menu.main, menu);
  47. return true;
  48. }
  49.  
  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. // Handle action bar item clicks here. The action bar will
  53. // automatically handle clicks on the Home/Up button, so long
  54. // as you specify a parent activity in AndroidManifest.xml.
  55. int id = item.getItemId();
  56. if (id == R.id.action_settings) {
  57. return true;
  58. }
  59. return super.onOptionsItemSelected(item);
  60. }
  61.  
  62. }

我们看下它的效果

从上面的结果,我们可以得出结论

1.这个方法其实加在viewGroup上比较有作用,它得到的是当前拥有焦点的子控件view

2.如果焦点不在这个控件内的话,返回的是一个null

3.如果你把这个方法加在ListView上的话,返回的是它的item

另外,我们还发现了另外一个跟focus相关的方法,如下

  1. /**
  2. * Find the nearest view in the specified direction that can take focus.
  3. * This does not actually give focus to that view.
  4. *
  5. * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
  6. *
  7. * @return The nearest focusable in the specified direction, or null if none
  8. * can be found.
  9. */
  10. public View focusSearch(@FocusRealDirection int direction) {
  11. if (mParent != null) {
  12. return mParent.focusSearch(this, direction);
  13. } else {
  14. return null;
  15. }
  16. }

这个方法,其实是代码获得某个控件获得焦点以后,下一个焦点移动到的控件。如果我们没有对这个控件的焦点进行操作,它就遵循android本身的焦点顺序。如果我们进行了操作,例如

  1. <Button
  2. android:id="@+id/bt_3"
  3. android:layout_width="60dp"
  4. android:layout_height="60dp"
  5. android:layout_marginLeft="5dp"
  6. android:background="@drawable/select"
  7. android:focusableInTouchMode="true"
  8. android:nextFocusRight="@+id/tv"
  9. android:textColor="#ffffff" />

增加了nextfocus 属性,我们得到的其实就是这个里面焦点跳转view

findFocus-获得拥有焦点的控件的更多相关文章

  1. js 设置焦点 判断控件是否获得焦点 判断哪个控件获得焦点

    设置焦点 <html> <head> <title>设置焦点</title> <mce:script language ="javasc ...

  2. Android Activity中获取当前焦点的控件,自动化输入EditText

    获取焦点的view对象 View view=getWindow().getDecorView().findFocus(); 如果是EditText if(view instanceof EditTex ...

  3. [原]创建三个输入文本框,当光标离开文本框的时候如果文本框为空,则将文本框背景色设置为红色,如果不为空则为白色。提示:焦点进入控件的事件是onfocus,焦点离开控件的事件是onblur

    window.onload = function () {             var txts = document.getElementsByTagName('input');         ...

  4. C#中方向键与回车键切换控件焦点

    环境:界面上有TextBox,ComboBox等控件. 不建议把左右方向键都用来切换焦点,否则你在TextBox里面改变光标所在字符位置就不方便了. 方法一:笨方法,需为每个控件单独注册事件处理 以T ...

  5. C#关于控件的上下左右移动

    C#怎么让控件上下左右移动?(转) http://wenwen.sogou.com/z/q231436494.htm 在winform中捕获上下左右键等控制键的按键事件(转) http://blog. ...

  6. winform学习之-----关于按键操作的一些小知识(如何获取焦点所在的当前控件)20160623

    1.设置整个窗体keydown事件的时候,要设置keyPreview=true; 2.获取当前拥有焦点的控件: 关于这个问题,自己也是纠结死了,在网上搜了好多相关的问题答案,搜出的结果是: //API ...

  7. winfrom 获取焦点控件

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Win ...

  8. Delphi XE2 之 FireMonkey 入门(36) - 控件基础: TForm

    Delphi XE2 之 FireMonkey 入门(36) - 控件基础: TForm 当我第一次读取 Form1.StyleLookup 并期待出现 "formstyle" 时 ...

  9. 【C#】让工具栏ToolStrip能触发焦点控件的Leave、Validating、DataError等事件以验证数据

    ----------------更新:2014-04-21--------------- 蒙doggo兄指教,得知有更好的方法可以代替蹩脚的0尺寸Button法,即调用窗体的验证方法Form.Vali ...

随机推荐

  1. [JZOJ3383] [NOIP2013模拟] 太鼓达人 解题报告(数位欧拉)

    来源:XLk 摘录 HDU2894 Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队 ...

  2. MVC获取当前Controller/Action名称

    1.视图中获取: var actionName=ViewContext.RouteData.Values["action"].ToString().ToLower(); var c ...

  3. C语言基础-第六章

    数组和字符串 1.一维数组 数组当中最简单的数据 声明: 类型说明符 数组名[常量表达式] int a[3];说明a的长度为3,那么给a赋值的语句是:a={1,2,3}; 2.多维数组 2.1 二维数 ...

  4. VS10的一个问题

    今天遇到一个问题,LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏.转一下网上的解决办法http://bbs.csdn.net/topics/390 ...

  5. Input Team

    The Chromium Input team (aka input-dev) is a web platform team focused on making touch (P1) and othe ...

  6. 最大优先队列 A - 奇怪的玩意

    我们的化学生物学家发明了一种新的叫stripies非常神奇的生命.该stripies是透明的无定形变形虫似的生物,生活在果冻状的营养培养基平板菌落.大部分的时间stripies在移动.当他们两个碰撞, ...

  7. 洛谷 P1501 [国家集训队]Tree II Link-Cut-Tree

    Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...

  8. Linux 文件系统权限

    文件权限管理 文件系统上的权限是指文件和目录的权限,权限主要针对三类对象(访问者)定义   owner   group   other  属主    属组    其它 每个文件对每类访问者都定义了三种 ...

  9. PHP安全性防范方式

    SQL注入 SQL注入是一种恶意攻击,用户利用在表单字段输入SQL语句的方式来影响正常的SQL执行. 防范方式 使用mysql_real_escape_string(),或者addslashes()过 ...

  10. Mysql 锁表 for update (引擎/事务)

    因为之前用过oracle,知道利用select * for update 可以锁表.所以很自然就想到在mysql中能不能适应for update来锁表呢. 学习参考如下 由于InnoDB预设是Row- ...