---
title: "第八章\t列表、菜单以及其它视图——下"
author: "wc0903"
date: "2010-03-19T08:42:32Z"
tags: ["Android", "Eclipse", "OS", "XML"]
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2010-03-19T08:48:53Z"
id: 619532
url: "https://wc0903.iteye.com/blog/619532"
---

这里是你的完整的AndroidManifest.xml项目文件：

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;manifest xmlns:android=http://schemas.android.com/apk/res/android

package="android\_programmers\_guide.AndroidViews

&lt;application android:icon="@drawable/icon"&gt;

&lt;activity android:name=".AndroidViews"

android:label="@string/app\_name"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER" /&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;activity android:name=".AutoComplete" android:label="AutoComplete"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER"

/&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;/application&gt;

&lt;/manifest&gt;

**处理Intent Call**

完成AndroidManifest.xml后，为AndroidViews.java添加下面的功能：

public void showAutoComplete(){

Intent autocomplete = new Intent(this, AutoComplete.class);

startActivity(autocomplete);

}

当从你的select/case生命中调用时，这个功能会打开你的autocomplete Action。编辑选择声明中的case()来让它调用新的功能:

case 0:

showAutoComplete();

return true;

在Android模拟器中运行应用。当主Activity启动时，点击Menu Button，你会看到如图8-1所示的菜单。点击AutoComplete菜单项。

点击AutoComplete按钮菜单项会激活你的autocomplete Activity，如下：

![](http://dl.iteye.com/upload/attachment/220306/e30a5dd2-44de-38c7-b341-d0fdc0a5ca6a.jpg)

要测试AutoCompleteTextView，就输入单词January。在你敲入少量字母后，你会January出现在文本框下面，如下图所示：

![](http://dl.iteye.com/upload/attachment/220308/ab7cf31c-8376-3716-8fe6-e676509b730c.jpg)

然后，点击Change Layout Button，结果应该是一个扩展后的文本输入框，与如下插入类似：

![](http://dl.iteye.com/upload/attachment/220310/3a834cf5-907d-3dcf-8660-e0ccb0f3cae3.jpg)

现在点击Change Text Color Button并输入一些文字，如下所示：

![](http://dl.iteye.com/upload/attachment/220312/f435b01b-52e2-3b27-bf0d-74c091d24064.jpg)

下面的章节会为你提供在工程中实现剩余五个视图的代码。

 

**Button**

 

看一下下面的代码。这个代码包含了四个文件，AndroidManifest.xml，Button.xml，testButton.java和AndroidViews.java。将文件中的这些代码添加到在已存在的AndroidViews Activity中。

**AndroidManifest.xml**

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;manifest xmlns:android=http://schemas.android.com/apk/res/android

package="android\_programmers\_guide.AndroidViews"

&lt;application android:icon="@drawable/icon"&gt;

&lt;activity android:name=".AndroidViews"

android:label="@string/app\_name"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER" /&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;activity android:name=".AutoComplete" android:label="AutoComplete"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER"/&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;activity android:name=".testButton" android:label="TestButton"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER"/&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;/application&gt;

&lt;/manifest&gt;

**Button.xml**

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

android:orientation="vertical"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"&gt;

&lt;Button android:id="@+id/testButton"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="This is the test Button"/&gt;

&lt;Button android:id="@+id/layoutButton"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="Change Layout"/&gt;

&lt;Button android:id="@+id/textColorButton"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="Change Text Color"/&gt;

&lt;/LinearLayout&gt;

**testButton.java**

package android\_programmers\_guide.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.graphics.Color;

public class testButton extends Activity {

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.Button);

final Button Button = (Button) findViewById(R.id.testButton);

final Button changeButton = (Button)findViewById(R.id.layoutButton);

changeButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption(Button); }

});

final Button changeButton2 = (Button)

findViewById(R.id.textColorButton);

changeButton2.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption2(Button);

}

});

}

public void changeOption(Button Button){

if (Button.getHeight()==100){

Button.setHeight(30);

}

else{

Button.setHeight(100);

}

}

public void changeOption2(Button Button){

Button.setTextColor(Color.RED);

}

}

**AndroidViews.java**

package android\_programmers\_guide.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.content.Intent;

public class AndroidViews extends Activity {

/\** Called when the Activity is first created. \*/

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

super.onCreateOptionsMenu(menu);

menu.add(0, 0, "AutoComplete");

menu.add(0, 1, "Button");

menu.add(0, 2, "CheckBox");

menu.add(0, 3, "EditText");

menu.add(0, 4, "RadioGroup");

menu.add(0, 5, "Spinner");

return true;

}

@Override

public boolean onOptionsItemSelected(Menu.Item item){

switch (item.getId()) {

case 0:

showAutoComplete();

return true;

case 1:

showButton();

return true;

true;

}

return true;

}

public void showButton() {

Intent showButton = new Intent(this, testButton.class);

startActivity(showButton);

}

public void showAutoComplete(){

Intent autocomplete = new Intent(this, AutoComplete.class);

startActivity(autocomplete);

}

}

启动你的应用并从菜单（如前面的图8-1所示）中选择Buttong选项。

下面的插图显示了Button Activity的样子。

![](http://dl.iteye.com/upload/attachment/220315/3d05cb2d-2086-3316-b740-db4fc0b29259.jpg)

尝试点击Change Layout Button。再次，结果会是一个更宽的文本显示区域，如下所示：

![](http://dl.iteye.com/upload/attachment/220317/f5bd235f-39af-3199-a2a7-91f99fe641fa.jpg)

点击Change Text Color Button，文本就会变红，如下：

![](http://dl.iteye.com/upload/attachment/220319/65b4780e-03b6-3f77-82e3-5d5d6aca2904.jpg)

 

**CheckBox**

本节中你会为CheckBox视图创建一个Activity。创建Activity的步骤和前面的章节中的相同。因此，会为你提供三个主Activity文件的全部代码——AndroidManifest.xml，checkbox.xml和testCheckBox.java。这些文件会在下几节提供给你。

 

**AndroidManifest.xml**

本节包含了当前AndroidView的AndroidManifest.xml的全部代码。如果你在Eclipse中学习，将你的Activity的AndroidManifest.xml文件更改为如下这样：

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;manifest xmlns:android=http://schemas.android.com/apk/res/android

package="android\_programmers\_guide.AndroidViews"&gt;

&lt;application android:icon="@drawable/icon"&gt;

&lt;activity android:name=".AndroidViews"

android:label="@string/app\_name"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER" /&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;activity android:name=".AutoComplete" android:label="AutoComplete"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER" /&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;activity android:name=".testButton" android:label="TestButton"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER"/&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;activity android:name=".testCheckBox" android:label="TestCheckBox"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER"/&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;/application&gt;

&lt;/manifest&gt;

 

**Checkbox.xml**

本节展示了checkbox.xml的全部代码。使用本章前面的指导在你的项目中创建一个名为checkbox.xml的新XML文件。使用下面的代码构建你的文件。

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

android:orientation="vertical"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

&gt; 

&lt;CheckBox android:id="@+id/testCheckBox"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="This is the test CheckBox"/&gt;

&lt;Button android:id="@+id/layoutButton"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="Change Layout"/&gt;

&lt;Button android:id="@+id/textColorButton"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="Change Text Color"/&gt;

&lt;/LinearLayout&gt;

**testCheckBox.xml**

本节包含了实现你的Checkbox Action所需的最终文件。在你的项目中创建一个名为testCheckBox.java的.java文件。该文件是这个Activity的主要文件并包含了可执行的代码。在testCheckBox.java中使用下面的代码。

package android\_programmers\_guide.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.CheckBox;

import android.widget.Button;

import android.graphics.Color;

public class testCheckBox extends Activity {

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.checkbox);

final CheckBox checkbox = (CheckBox)findViewById(R.id.testCheckBox);

final Button changeButton = (Button)findViewById(R.id.layoutButton);

changeButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption(checkbox); }

});

final Button changeButton2 = (Button)

findViewById(R.id.textColorButton);

changeButton2.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption2(checkbox);

}

});

}

public void changeOption(CheckBox checkbox){

if (checkbox.getHeight()==100){

checkbox.setHeight(30);

}

else{

checkbox.setHeight(100);

}

}

public void changeOption2(CheckBox checkbox){

checkbox.setTextColor(Color.RED);

}

}