想要监听一个控件的焦点变化情况,发现了一个

view.setOnFocusChangeListener(new OnFocusChangeListener() {
......
}

现在写一个小demo,学习一下这个方法的使用。看如下代码

布局xml

 <RelativeLayout
android:id="@+id/abc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/bt"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:gravity="center"
android:background="@drawable/select"
android:focusableInTouchMode="true" /> <ImageButton
android:id="@+id/bt1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/bt"
android:focusableInTouchMode="true"
android:src="@drawable/select" /> <ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/bt"
android:focusableInTouchMode="true"
android:src="@drawable/select" />
</RelativeLayout>

代码

package com.example.alert;

import android.app.Activity;
import android.os.Bundle; import android.util.Log;
import android.view.ActionMode;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.ImageButton; public class HeadSetActivity extends Activity {
private ImageButton imageButton;
private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.bt1);
button = (Button) findViewById(R.id.bt);
imageButton.setOnFocusChangeListener(new OnFocusChangeListener() { @Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
button.setText("focus==="+arg1);
}
});
} }

我们看下运行结果

总结如下

1.这个方法的使用非常简单,也就是当view的焦点状态发生变化的时候,这个事件就会触发。参数arg1代表的是当前控件的焦点状态

2.xml中有一个属性

android:focusableInTouchMode="true"

它代表的是当点击这个控件的时候,这个控件是否获得焦点

最新文章

  1. Repeater控件使用中的一些小问题
  2. Asp.net mvc 有关序列化的问题
  3. 数据仓库专题(23):总线矩阵的另类应用-Drill Down into a More Detailed Bus Matrix
  4. Android环境搭建的步骤
  5. DevExpress LookUpEdit 下拉框基本操作
  6. 高效并发JUC锁-砖石
  7. 7.01-beautiful_soup3
  8. 《笔记》Python itertools的groupby分组数据处理
  9. 【splunk】启动停止
  10. python - 6. Defining Functions
  11. 【论文笔记】Learning Convolutional Neural Networks for Graphs
  12. Mysql正则匹配某列是否含有手机号
  13. Invalid HTTP_HOST header: &#39;xxx.xx.xxx.xxx:8000&#39;. You may need to add &#39;xxx.xx&#39; to ALLOWED_HOSTS
  14. 009-hostname与/etc/hosts区别
  15. python第二十一课——str中的常用函数(重要)
  16. Win10版本号区分
  17. 这些HTML、CSS知识点,面试和平时开发都需要 No10-No11(知识点:表格操作、代码编写规则)
  18. nvidia显卡驱动
  19. 洗牌问题 FZU - 1062 (传说中的思路题,hhh)
  20. Python ImportError: No module named &#39;requests&#39;解决方法

热门文章

  1. HBase框架基础(四)
  2. mysql读写分离的解决方案
  3. 织梦CMS调用文章第一张图片(非缩略图)终极方法
  4. 洛谷3388 【模板】割点 tarjan算法
  5. Systemd曝3漏洞,大部分Linux将受到攻击
  6. users---显示当前登录系统的所有用户的用户列表
  7. 一个Web报表项目的性能分析和优化实践(二):MySQL数据库连接不够用(TooManyConnections)问题的一次分析和解决案例
  8. linux学习之多高并发服务器篇(一)
  9. 【 D3.js 入门系列 --- 2.1 】 关于怎样选择,插入,删除元素
  10. CentOS 配置防火墙操作实例(启、停、开、闭port)