在任何一个 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() {}@Overridepublic 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;importimportimportimportandroid.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() {}@Overridepublic 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{privateprivateprivateprivatestatic 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);}}@Overridepublic 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 这个接口,这种方式是一种最为直接的方
式,即为不同的控件单独实现它的相应类。

更多相关文章

  1. Android -- Layout布局文件里的android:layout_height等属性为什
  2. sdcardFS(android sdcard存储方案---基于wrapfs文件系统)
  3. 【Android Training - 04】保存数据 [ Lesson 2 - 保存文件]
  4. Android的支持库 && app/apk包进系统 && 预置so库进系统 && 预置
  5. Android mainfest文件 android属性 汇总
  6. [Android] 将Android工程做成jar包和资源文件
  7. Android 动态布局 动态生成 销毁控件 改变控件的位置等
  8. Android wps文件下载、预览
  9. Android(java)学习笔记135:Android中assets文件夹资源的访问

随机推荐

  1. Android(安卓)Studio 1.1 使用介绍及导入
  2. 进程间通过intent传递数据失败
  3. Android四方形输入框、密码框
  4. 获取手机屏幕大小(DisplayMetrics类取得
  5. Android(安卓)动画学习
  6. android busybox解决adbshell命令不全
  7. Android(安卓)Service 通知Activity更新
  8. android activity之间共享数据(applicatio
  9. Rxjava入门
  10. Google VR for Android(安卓)- Getting S