findFocus-获得拥有焦点的控件
所有的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
findFocus-获得拥有焦点的控件的更多相关文章
- js 设置焦点 判断控件是否获得焦点 判断哪个控件获得焦点
设置焦点 <html> <head> <title>设置焦点</title> <mce:script language ="javasc ...
- Android Activity中获取当前焦点的控件,自动化输入EditText
获取焦点的view对象 View view=getWindow().getDecorView().findFocus(); 如果是EditText if(view instanceof EditTex ...
- [原]创建三个输入文本框,当光标离开文本框的时候如果文本框为空,则将文本框背景色设置为红色,如果不为空则为白色。提示:焦点进入控件的事件是onfocus,焦点离开控件的事件是onblur
window.onload = function () { var txts = document.getElementsByTagName('input'); ...
- C#中方向键与回车键切换控件焦点
环境:界面上有TextBox,ComboBox等控件. 不建议把左右方向键都用来切换焦点,否则你在TextBox里面改变光标所在字符位置就不方便了. 方法一:笨方法,需为每个控件单独注册事件处理 以T ...
- C#关于控件的上下左右移动
C#怎么让控件上下左右移动?(转) http://wenwen.sogou.com/z/q231436494.htm 在winform中捕获上下左右键等控制键的按键事件(转) http://blog. ...
- winform学习之-----关于按键操作的一些小知识(如何获取焦点所在的当前控件)20160623
1.设置整个窗体keydown事件的时候,要设置keyPreview=true; 2.获取当前拥有焦点的控件: 关于这个问题,自己也是纠结死了,在网上搜了好多相关的问题答案,搜出的结果是: //API ...
- winfrom 获取焦点控件
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Win ...
- Delphi XE2 之 FireMonkey 入门(36) - 控件基础: TForm
Delphi XE2 之 FireMonkey 入门(36) - 控件基础: TForm 当我第一次读取 Form1.StyleLookup 并期待出现 "formstyle" 时 ...
- 【C#】让工具栏ToolStrip能触发焦点控件的Leave、Validating、DataError等事件以验证数据
----------------更新:2014-04-21--------------- 蒙doggo兄指教,得知有更好的方法可以代替蹩脚的0尺寸Button法,即调用窗体的验证方法Form.Vali ...
随机推荐
- Java容器源码解析之——LinkedList
我们直接从源码来分析LinkedList的结构: public class LinkedList<E> extends AbstractSequentialList<E> im ...
- BZOJ 1336&1337最小圆覆盖
思路: http://blog.csdn.net/commonc/article/details/52291822 (照着算法步骤写--) 已知三点共圆 求圆心的时候 就设一下圆心坐标(x,y) 解个 ...
- 网络流Dinic算法模板 POJ1273
这就是以后我的板子啦~~~ #include <queue> #include <cstdio> #include <cstring> #include <a ...
- 关于iOS声音识别的框架
你好,我现在的项目中需要用到"声纹识别"这方面的需求,以前没做过,请教了.有没有这方面的框架和工具? 关于iOS声音识别的框架 >> ios这个答案描述的挺清楚的:ht ...
- c# 值类型 之枚举
1声明枚举(enum)类型的变量 enum 变量名 { //标识符列表中,元素与元素之间用 , 逗号分隔: 标识符列表 } 枚举列表中的每个符号代表一个整数值,一个比他前面符号大的整数值,默认情况下, ...
- 阿里云安装mysql数据库出现2002错误解决办法
在安装数据库的时候出现了如下错误: 解决办法如下: 1.在bin目录下 输入:kill -s 9 9907 再输入:ps -ef|grep mysql 显示如下: 2.回到lampp目录下,重启数据库 ...
- Hyper-V 导入与导出虚拟机
虚拟机的导入与导出功能可以将虚拟机通过文件的方式进行转移,可以将虚拟机的文件复制到活动硬盘,然后带到其他的地点进行导入,这样方便了虚拟机的跨地域的转移.但是有一点要注意,所有要转移的虚拟机都必须处于停 ...
- cmd 操作命令
1)cd 操作文件目录的 cd path #进入path cd / #返回到当前盘符的根目录 cd .. #返回到上级目录 2)dir 显示当前目录 dir #显示当前目录下的文件夹 dir path ...
- NET Core微服务之路:实战SkyWalking+Exceptionless体验生产下追踪系统
原文:NET Core微服务之路:实战SkyWalking+Exceptionless体验生产下追踪系统 前言 当一个APM或一个日志中心实际部署在生产环境中时,是有点力不从心的. 比如如下场景分析的 ...
- Android studio树形
原创作品,允许转载,转载时请务必声明作者信息和本声明. http://www.cnblogs.com/zhu520/p/8349553.html 这个是上网找了好久才弄出来的,我把我上网找的总结也写 ...