OnFocusChangeListener接口用来处理控件焦点发生改变的事件,如果注册了该接口,当某个控件失去焦点或者获得焦点的时候都会出发该接口中的回调方法,该接口对应的回调方法:public void onFocusChange(View v,Boolean hasFocus)
下面通过一个案例来说明:
(1)首先,编写string.xml文件,定义要用到的字符串资源
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sample_7_6</string>
<string name="textView">请选择下列动物的一种</string>
</resources>
<!-- 定义字符串资源 -->
(2)编写main.xml文件,编写完后一定要保存。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:text="@string/textView"/><!-- 添加TextView -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"><!-- 添加一个水平布局 -->
<ImageButton
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:src="@drawable/zou1"
/>
<ImageButton
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:src="@drawable/zou2"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"><!-- 添加一个水平布局 -->
>
<ImageButton
android:id="@+id/button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:src="@drawable/zou3"
/>
<ImageButton
android:id="@+id/button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:src="@drawable/zou4"
/>
</LinearLayout>
<TextView
android:id="@+id/myTextView"
android:textSize="30px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
`(3)编写.java文件
package dsa.daaw;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class Sample_7_6Activity extends Activity implements OnFocusChangeListener {
TextView myTextView;//声明TextView的引用
ImageButton[] imageButtons = new ImageButton[4];//声明数组按钮
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView) this.findViewById(R.id.myTextView);//得到myTextView的引用
imageButtons[0] = (ImageButton) this.findViewById(R.id.button01);
imageButtons[1] = (ImageButton) this.findViewById(R.id.button02);
imageButtons[2] = (ImageButton) this.findViewById(R.id.button03);
imageButtons[3] = (ImageButton) this.findViewById(R.id.button04);
for(ImageButton imageButton:imageButtons){
imageButton.setOnFocusChangeListener(this);//添加监听
}
}
@Override
public void onFocusChange(View v,boolean hasFocus){//实现接口中的方法
if(v.getId() == R.id.button01){
myTextView.setText("您选中了羊");
}else if(v.getId() == R.id.button02){
myTextView.setText("您选中了牛");
}else if(v.getId()==R.id.button03){
myTextView.setText("您选中了猪");
}else if(v.getId()==R.id.button04){
myTextView.setText("您选中了鼠");
}else{
myTextView.setText("您什么都没选");
}
}
}