Android UI控件和基本事件的响应

在任何一个 GUI 系统中,控制界面上的控件(通常称为控件)都是一个基本的内容。对于 Android 应用程序,控件称为 View。

在 Android 中,在处理 UI 中的各种元素的时候,两个程序中的要点为:

  • 得到布局文件(XML)中的控件句柄
  • 设置控件的行为

Android 中3种基本的程序控制方法。例子效果是通过 2 个按钮来控制一个文本框的背景颜色。

布局文件(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>

1.事件响应方法

根据以上的布局文件中定义的两个按钮和一个文本框,这个布局文件被活动设置为 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 的句柄对其进行控制。

2.第二种响应方法

除了上述的使用方法,在使用同样的布局文件和应用程序的情况下,实现同样的功能。本例中使用的是另外的
一种方式实现。
本例使用的源代码文件如下所示:

package com.android.basicapp;
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Color;
import
import
import
import
android.widget.Button;
android.widget.TextView;
android.view.View;
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 的句柄。

3.第三种响应方法

本例介绍同样功能实现的第三种方法,区别也仅仅在于 JAVA 源代码中,实现的内容如下所示。

import android.view.View.OnClickListener;
import android.util.Log;
public class TestEvent3 extends Activity{
private
private
private
private
static final String TAG = "TestEvent3";
TextView mText;
Button1_OnClickListener mListener1 = new Button1_OnClickListener();
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 这个接口,这种方式是一种最为直接的方
式,即为不同的控件单独实现它的相应类。

转载于:https://my.oschina.net/u/178456/blog/210822

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值