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

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

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

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

看demo

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context="com.example.testcode.MainActivity" > <TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="10dp"
android:clickable="true"
android:focusable="true"
android:textSize="30sp"
android:focusableInTouchMode="true"
android:background="@drawable/select"
android:textColor="#ffffff" /> <LinearLayout
android:id="@+id/aaa"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:orientation="horizontal" > <Button
android:id="@+id/bt_1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:background="@drawable/select"
android:focusableInTouchMode="true"
android:textColor="#ffffff" /> <Button
android:id="@+id/bt_2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:background="@drawable/select"
android:focusableInTouchMode="true"
android:textColor="#ffffff" /> <Button
android:id="@+id/bt_3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:background="@drawable/select"
android:focusableInTouchMode="true"
android:textColor="#ffffff" /> <Button
android:id="@+id/bt_4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:background="@drawable/select"
android:focusableInTouchMode="true"
android:textColor="#ffffff" />
</LinearLayout> <Button
android:id="@+id/bt"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/select" /> </LinearLayout>

activity

package com.example.testcode;
import com.example.testcode.R.id; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener; import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView textView;
private Button button;
private LinearLayout relativeLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv);
button = (Button)findViewById(R.id.bt);
relativeLayout = (LinearLayout)findViewById(R.id.aaa);
textView.setText("当前焦点所在view");
button.setText("显示当前focus"); button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
View view = relativeLayout.findFocus();
textView.setText(""+view);
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} }

我们看下它的效果

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

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

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

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

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

  /**
* Find the nearest view in the specified direction that can take focus.
* This does not actually give focus to that view.
*
* @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
*
* @return The nearest focusable in the specified direction, or null if none
* can be found.
*/
public View focusSearch(@FocusRealDirection int direction) {
if (mParent != null) {
return mParent.focusSearch(this, direction);
} else {
return null;
}
}

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

<Button
android:id="@+id/bt_3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:background="@drawable/select"
android:focusableInTouchMode="true"
android:nextFocusRight="@+id/tv"
android:textColor="#ffffff" />

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

最新文章

  1. WPF 自定义Grid双击事件
  2. 检测IE浏览器方法
  3. Leetcode 376. Wiggle Subsequence
  4. ThinkPHP的四种URL模式 URL_MODEL
  5. VBA 表操作1
  6. shell script练习
  7. HighCharts之2D柱状图
  8. C# Window Form播放音乐的4种方式
  9. [转]Android进程与线程基本知识
  10. javascript第八课匿名函数的使用
  11. 我也来谈javascript高级编程之:javascript函数编译过程
  12. BigDecimal工具类处理精度计算
  13. Python @property 详解
  14. UVA 536 Tree Recovery 建树+不建树
  15. [转载]mysqlcreate新建用户host使用%,本地无法连接原因及解决方法
  16. C#:使用ListView动态添加数据一直闪烁的解决办法
  17. Android源码中final关键字的用法及final,finally,finalize的区别
  18. 宇宙最帅叉叉——第五周博客 for 测试与发布(Alpha版本)
  19. 冲刺ing-5
  20. js中的encodeURIComponent()函数

热门文章

  1. spring mvc给参数起别名
  2. 配置ssh免密码登录的原理
  3. AIX 6.1 Oracle 10G 数据库GoldenGate实施
  4. JS由Number与new Number的区别引发的思考
  5. 用Electron开发企业网盘(二)--分片下载
  6. bzoj1935 [Shoi2007]园丁的烦恼
  7. vue-router 实现无效路由(404)的友好提示
  8. win下通过pip安装TensorFlow
  9. 编译bash实现history的syslog日志记录
  10. 《SAS编程与数据挖掘商业案例》学习笔记之十八