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

**AndroidViews.java**

创建这个Activity的最后一步是编辑AndroidViews.java。如果你想要从主AndroidViews Activity中调用testCheckBox Activity，你必须给AndroidViews.java添加代码。比较一下下面的代码和你当前的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;

case 2:

showCheckBox()

return true;

case 3:

return true;

case 4:

return true;

case 5:

return 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);

}

public void showCheckBox() {

Intent checkbox = new Intent(this, testCheckBox.class);

startActivity(checkbox);

}

}

启动你的应用并从菜单中选择CheckBox选项。

下面的插图展示了Checkbox Activity的样子。

![](http://dl.iteye.com/upload/attachment/220321/958ae1f4-a5f2-3243-8b9d-6bbd59d9452b.jpg)

双击Change Layout和Change Test Color按钮。结果如下所示。

![](http://dl.iteye.com/upload/attachment/220323/473e65c9-8d99-3626-bd7b-1937ffb5caa4.jpg)

### EditText

在本节中，与上个类似，你会为EditText 视图创建一个Activity。创建Activity的步骤跟前面的章节中非常类似。因此会为你提供三个主要的Activity文件的全部代码——AndroidManifest.xml，edittext.xml和testEditText.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;activity android:name=".testEditText" android:label="TestEditText"&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;

**Edittext.xml**

本节展示editttext.xml的全部代码。根据章节前面的指导创建一个新的名为edittext.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;EditText android:id="@+id/testEditText"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

/&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;

**testEditText.java**

本节包含了实现Edittext Activity的最终文件。在项目中创建一个名为testEditText.java的新.java文件。该文件时Activity的主文件并且包含了可执行的代码。在testEditText.java中使用下面的代码来完善该Activity。

package android\_programmers\_guide.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.Button;

import android.graphics.Color;

public class testEditText extends Activity {

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.edittext);

final EditText edittext = (EditText)findViewById(R.id.testEditText);

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

changeButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption(edittext); }

});

final Button changeButton2 = (Button)

findViewById(R.id.textColorButton);

changeButton2.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption2(edittext);

}

});

}

public void changeOption(EditText edittext){

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

edittext.setHeight(30);

}

else{

edittext.setHeight(100);

}

}

public void changeOption2(EditText edittext){

edittext.setTextColor(Color.RED);

}

}

**AndroidViews.java**

创建这个Activity的最后一步是编辑AndroidViews.java。如果你想要从主AndroidViews Activity中调用testEdittext Activity，你必须给AndroidViews.java添加代码。比较一下下面的代码和你当前的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;

case 2:

showCheckBox();

return true;

case 3:

showEditText();

return true;

case 4:

showRadioGroup();

return true;

case 5:

showSpinner();

return 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);

}

public void showCheckBox(){

Intent checkbox = new Intent(this, testCheckBox.class);

startActivity(checkbox);

}

public void showEditText() {

Intent edittext = new Intent(this, testEditText.class);

startActivity(edittext);

}

}

启动你的应用，并从菜单中选择Edittext选项。

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

![](http://dl.iteye.com/upload/attachment/220325/8eba21e1-596e-39ae-813b-24fd04adf6c3.jpg)

双击Change Layout 和Change Test Color按钮。结果就如下插图所示。

 ![](http://dl.iteye.com/upload/attachment/220327/f0ab4a85-0a93-3def-9753-6eca2d5e04f2.jpg)

**RadioGroup**

在本节中，与上个类似，你会为RadioGroup 视图创建一个Activity。创建Activity的步骤跟前面的章节中非常类似。因此会为你提供三个主要的Activity文件的全部代码——AndroidManifest.xml，radiogroup.xml和testRadioGroup.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: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;activity android:name=".testEditText" android:label="TestEditText"&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=".testRadioGroup" android:label="Test

RadioGroup"&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;

**Radiogroup.xml**

本节展示radiogroup.xml的全部代码。根据章节前面的指导创建一个新的名为radiogroup.xml的XML文件。使用下面的代码构建你的文件。