事件响应方法(点击等)

本例构建一个应用程序,其在AndroidManifest.xml描述文件中的内容如下所示:
<activity android:name="TestEvent1" android:label="TestEvent1">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
本例定义了一个Android中基本的活动。
本例的布局文件(layout)的代码片段如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="24sp"
android:text="@string/text1" />
<Button android:id="@+id/button1"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/red"/>
<Button android:id="@+id/button2"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/green"/>
</LinearLayout>

 

根据以上的布局文件中定义的两个按钮和一个文本框,这个布局文件被活动设置为View后,显示的内容就如上图所示,只是行为还没有实现。
行为将在源代码文件TestEvent1.java中实现,这部分的代码如下所示:
package com.android.basicapp;
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Color;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
public class TestEvent1 extends Activity {
private static final String TAG = "TestEvent1";
public TestEvent1() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testevent);
final TextView Text = (TextView) findViewById(R.id.text1); // 获得句柄
final Button Button1 = (Button) findViewById(R.id.button1);
final Button Button2 = (Button) findViewById(R.id.button2);
Button1.setOnClickListener(new OnClickListener() { // 实现行为功能
public void onClick(View v) {
Text.setBackgroundColor(Color.RED);
}
});
Button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Text.setBackgroundColor(Color.GREEN);
}
});
}
}
在创建的过程中,通过findViewById获得各个屏幕上面的控件(控件)的背景,这里使用的R.id.button1等和布局文件中各个元素的id是对应的。实际上,在布局文件中,各个控件即使不写android:id这一项也可以正常显示,但是如果需要在代码中进行控制,则必须设置这一项。
根据Button 控件的setOnClickListener()设置了其中的点击行为,这个方法的参数实际上是一个View.OnClickListener类型的接口,这个接口需要被实现才能够使用,因此在本例的设置中,实现了其中的onClick()函数。这样既可实现点击的时候实现相应的功能,在点击的函数中,将通过Text的句柄对其进行控制。
在Android的控件使用方面,这两个编程方面要点是:
?
使用findViewById()获取布局文件(XML)中控件的句柄;
?
使用setOnXXXListener()设置事件处理函数。
在获取句柄时需要转换成相应的控件类型,findViewById()函数的参数是一个整数,返回值是一个android.view.View类型。通过R.id.XXX找到布局文件中定义的ID,然后通过将基础类转换成其实际的类获得真正的句柄。注意:所转换类必须和布局文件中描述的控件一致。
SetOnXXXListener()等函数是android.view.View类的函数,各种控件(包括Button、EditText)都扩展这个类,同族的函数包括:
void setOnClickListener(View.OnClickListener l);
void setOnFocusChangeListener(View.OnFocusChangeListener l);
55
void setOnKeyListener(View.OnKeyListener l);

void setOnLongClickListener(View.OnLongClickListener l);
void setOnTouchListener(View.OnTouchListener l);
这些函数用于事件处理,它们由程序实现,通过设置这些内容也就设置了控件的行为。这些函数的参数都是所对应的android.view.View类中的方法。
Android中UI基本控制内容:使用findViewById()联系布局文件中控件和句柄,并通过OnClickListener()等定制句柄的行为。
6.1.2.第二种响应方法
除了上述的使用方法,在使用同样的布局文件和应用程序的情况下,实现同样的功能。本例中使用的是另外的一种方式实现。
本例使用的源代码文件如下所示:
package com.android.basicapp;
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Color;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
public class TestEvent2 extends Activity implements OnClickListener {
// 实现相关的接口
private static final String TAG = "TestEvent2";
private TextView mText;
private Button mButton1;
private Button mButton2;
public TestEvent2() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testevent);
mText = (TextView) findViewById(R.id.text1);
mButton1 = (Button) findViewById(R.id.button1);
mButton1.setOnClickListener(this); // 设置监听的类
mButton2 = (Button) findViewById(R.id.button2);
mButton2.setOnClickListener(this); // 设置监听的类
}
public void onClick(View v) {
Log.v(TAG, "onClick()");
switch(v.getId()){ // 区分不同的控件
case R.id.button1:
mText.setBackgroundColor(Color.RED);
break;
case R.id.button2:
mText.setBackgroundColor(Color.GREEN);
break;
default:
Log.v(TAG, "other");
break;
}
}
}
这个例子的主要变化是让活动实现(implements)了OnClickListener()这个进口,也就是需要实现其中的onClick()方法。然后通过setOnClickListener()将其设置到按钮中的参数就是this,表示了当前的活动。
通过这种方式的设置,如果程序中有多个控件需要设置,那么所设置的也都是一个函数。为了保证对不同控件

具有不同的处理,可以由onClick()函数的参数进行判断,参数是一个View类型,通过getId()获得它们的ID,使用switch…case分别进行处理。
在本例中,通过将需要将文本框(TextView)句柄保存为类的成员(mText),这样就可以在类的各个函数中都能获得这个句柄进行处理。这和上一种方法是有区别的,因为上一个例子实现的接口和获得的TextView在同一个函数中,因此不需要保存TextView的句柄。
6.1.3.第三种响应方法
本例介绍同样功能实现的第三种方法,区别也仅仅在于JAVA源代码中,实现的内容如下所示。
import android.view.View.OnClickListener;
import android.util.Log;
public class TestEvent3 extends Activity{
private static final String TAG = "TestEvent3";
private TextView mText;
private Button1_OnClickListener mListener1 = new Button1_OnClickListener();
private Button2_OnClickListener mListener2 = new Button2_OnClickListener();
public TestEvent3() {
}
class Button1_OnClickListener implements OnClickListener { // 接口的第一个实现
public void onClick(View v) {
mText.setBackgroundColor(Color.RED);
}
}
class Button2_OnClickListener implements OnClickListener { // 接口的第一个实现
public void onClick(View v) {
mText.setBackgroundColor(Color.GREEN);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testevent);
mText = (TextView) findViewById(R.id.text1);
final Button mButton1 = (Button) findViewById(R.id.button1);
final Button mButton2 = (Button) findViewById(R.id.button2);
mButton1.setOnClickListener(mListener1); // 设置监听者的类
mButton2.setOnClickListener(mListener2); // 设置监听者的类
}
}
本例通过定义实现活动类中的2个子类,来实现View.OnClickListener这个接口,这种方式是一种最为直接的方式,即为不同的控件单独实现它的相应类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值