<span style="font-size:24px;">1.描述文件中的内容如下所示: </span>
<span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.helloactivity.HelloActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest></span>
<span style="font-size:24px;"></pre><pre name="code" class="html" style="font-size: 18px;">里面主要是activity:</span>
<span style="font-size:24px;"><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html" style="font-size: 18px;"> <activity
android:name="com.example.helloactivity.HelloActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity></span>
</pre><pre name="code" class="html"><span style="font-size:24px;">2.布局文件代码片段如下所示:</span>
<span style="font-size:24px;"><pre name="code" class="html" style="font-size: 18px;"><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>
</span>
3.行为将在源代码文件实现:
<span style="font-size:24px;"><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">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);
}
});
}
} </span>
<span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">通过 findViewById 获得各个屏幕上面的控件(控件)的背景,这里使用的 R.id.button1 等和</span><span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">布局文件中各个元素的 id 是对应的。</span>
<span style="font-size:24px;">使用方面,这两个编程方面要点是:
使用 findViewById()获取布局文件(XML)中控件的句柄;
使用 setOnXXXListener()设置事件处理函数。
</span>