第八章 列表、菜单以及其它视图——继

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

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

<RadioGroup android:id="@+id/testRadioGroup"

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

<RadioButton

android:text="Radio 1"

android:id="@+id/radio1"

/>

<RadioButton

android:text="Radio 2"

android:id="@+id/radio2" />

</RadioGroup>

<Button android:id="@+id/enableButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Set isEnabled"/>

<Button android:id="@+id/backgroundColorButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Change Background Color"/>

</LinearLayout>

 

testRadioGroup.java

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

package android_programmers_guide.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.RadioGroup;

import android.widget.Button;

import android.graphics.Color;

public class testRadioGroup extends Activity {

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.radiogroup);

final RadioGroup radiogroup = (RadioGroup)

findViewById(R.id.testRadioGroup);

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

changeButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption(radiogroup); }

});

final Button changeButton2 = (Button)

findViewById(R.id.backgroundColorButton);

changeButton2.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption2(radiogroup);

}

});

}

public void changeOption(RadioGroup radiogroup){

if (radiogroup.isEnabled()){

radiogroup.setEnabled(false);

}

else{

radiogroup.setEnabled(true);

} }

public void changeOption2(RadioGroup radiogroup){

radiogroup.setBackgroundColor(Color.RED);

}

}

AndroidViews.java

创建这个Activity的最后一步是编辑AndroidViews.java。如果你想要从主AndroidViews Activity中调用testRadioGroup 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);

}

public void showRadioGroup(){

Intent radiogroup = new Intent(this, testRadioGroup.class);

startActivity(radiogroup);

}

public void showSpinner(){

}

}

}

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

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

双击Set isEnabled Change BackGroup Color按钮。结果就如下插图所示。注意RadioGroupSet isEnabled按钮能禁用组,而Change BackGroup Color按钮能够改变组的背景色。

Spinner

在本节中,与上个类似,你会为Spinner 视图创建一个Activity。创建Spinner视图和其他编程语言中的ComboBox相似。创建Activity的步骤跟前面的章节中非常类似。因此会为你提供三个主要的Activity文件的全部代码——AndroidManifest.xmlspinner.xmltestSpinner.java。下面的会为你提供这些代码。

AndroidManifest.xml

本节包含了当前AndroidViewAndroidManifest.xml的全部代码。如果你在Eclipse中学习,将你的ActivityAndroidManifest.xml文件更改为如下这样:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=http://schemas.android.com/apk/res/android

package="android_programmers_guide.AndroidViews">

<application android:icon="@drawable/icon">

<activity android:name=".AndroidViews"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".AutoComplete" android:label="AutoComplete">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity android:name=".testButton" android:label="TestButton">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity android:name=".testCheckBox" android:label="TestCheckBox">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity android:name=".testEditText" android:label="TestEditText">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity android:name=".testRadioGroup" android:label="Test

RadioGroup">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity android:name=".testSpinner" android:label="Test Spinner">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

 

Spinner.xml

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

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

<Spinner android:id="@+id/testSpinner"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

<Button android:id="@+id/enableButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Set isEnabled"/>

<Button android:id="@+id/backgroundColorButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Change Background Color"/>

</LinearLayout>

testSpinner.java

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

package android_programmers_guide.AndroidViews;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Button;

import android.graphics.Color;

simple_spinner_item, Months);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

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

changeButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption(spinner); }

});

final Button changeButton2 = (Button)

findViewById(R.id.backgroundColorButton);

changeButton2.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v){

changeOption2(spinner);

}

});

}

static final String[] Months = new String[]{

"January","February","March","April","May","June","July","August",

"September","October","November","December"

};

public void changeOption(Spinner spinner){

if (spinner.isEnabled()){

spinner.setEnabled(false);

}

else{

spinner.setEnabled(true);

}

}

public void changeOption2(Spinner spinner){

spinner.setBackgroundColor(Color.RED);

}

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值