OnFocusChangeListener接口简介

来源:互联网 发布:有些什么直播软件 编辑:程序博客网 时间:2024/06/05 10:00

本篇文章主要是用来处理控件焦点发生改变的事件

那么具体的我们在下面的代码中简单介绍一下:

首先我们可以在浏览器中下载四张生肖图片,例如鼠牛虎兔等等,把图片放到drawable-mdpi中,在string.xml中加入如下代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">1</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="textView">请选择其中您喜爱的动物</string>


</resources>

然后在main.xml中加入一下代码:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.MainActivity" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:text="@string/textView" />


    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:src="@drawable/shu" />


    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_toRightOf="@+id/imageButton1"
        android:src="@drawable/niu" />


    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton1"
        android:layout_toLeftOf="@+id/imageButton2"
        android:src="@drawable/hu" />


    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageButton3"
        android:layout_toRightOf="@+id/imageButton3"
        android:src="@drawable/tu" />


    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton3"
        android:layout_below="@+id/imageButton3"
        android:layout_marginTop="16dp"
        android:textSize="30px" />


</RelativeLayout>

其中也可以在里面加入LinearLayout的布局文件,总之是看爱好了,

然后在MainActivity中键入以下代码:

package com.example;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;


public class MainActivity extends Activity implements OnFocusChangeListener {

TextView textView;
ImageButton[] imageButtons=new ImageButton[4];


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)this.findViewById(R.id.textView);
imageButtons[0]=(ImageButton)this.findViewById(R.id.imageButton1);
imageButtons[1]=(ImageButton)this.findViewById(R.id.imageButton2);
imageButtons[2]=(ImageButton)this.findViewById(R.id.imageButton3);
imageButtons[3]=(ImageButton)this.findViewById(R.id.imageButton4);
for(ImageButton imageButton:imageButtons){
imageButton.setOnFocusChangeListener(this);
}
}
public void onFocusChange(View v,boolean hasFocus){
if(v.getId()==R.id.imageButton1){
textView.setText("您选择了鼠");}
else if(v.getId()==R.id.imageButton2){
textView.setText("您选择了牛");}
else if(v.getId()==R.id.imageButton3){
textView.setText("您选择了虎");}
else if(v.getId()==R.id.imageButton4){
textView.setText("您选择了兔");}
else {
textView.setText("您没有选择任何动物");
}
}


@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);
}
}

就是这么简单的一些代码了。希望大家可以看懂。

0 0
原创粉丝点击