`
wc0903
  • 浏览: 80789 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

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

阅读更多

本节展示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);

}

}

 

  • 大小: 25.4 KB
  • 大小: 45.8 KB
分享到:
评论

相关推荐

    Windows编程新手乐园 CHM

    第八章 本篇复习——控制台应用程序和长文件名 第二篇:字体和窗口控制 第九章 第八天课程——Windows 动画:Snake 游戏 第十章 第九天课程——字体基础知识 第十一章 第十天课程——窗口控制 第十二章 第十...

    《Visual C++实用教程》ppt课件(刘惊雷)

    第8章:利用CDC实现绘图程序.ppt 第9章:Windows应用程序引擎——消息映射.ppt 第10章:用户交互对象——菜单、工具栏和状态栏.ppt 第11章:对话框及常用控件.ppt 第12章:基于文档/视图结构的程序.ppt 第13章:...

    Android编程入门很简单.(清华出版.王勇).part2

    第8章Android应用程序组成 8.1深入理解Activity 8.1.1使用Intent连接Activity 8.1.2 Activity的生命周期 …… 第9章Android中的数据存储 第10章绚丽的多媒体技术 第11章Android网上冲浪 第12章Android地图服务 第4篇...

    Android编程入门很简单.(清华出版.王勇).part1

    第8章Android应用程序组成 8.1深入理解Activity 8.1.1使用Intent连接Activity 8.1.2 Activity的生命周期 …… 第9章Android中的数据存储 第10章绚丽的多媒体技术 第11章Android网上冲浪 第12章Android地图服务 第4篇...

    [MFC.Windows程序设计(第2版)修订版].(Programming.Windows.with.MFC,.2nd.Edition)

     第8章 对话框和属性表  第Ⅱ部分 文档/视图体系结构  第9章 文档、视图和单文档界面  第10章 滚动视图、HTML视图以及其他视图类型  第11章 多文档和多视图  第12章 工具栏、状态栏和组合栏  第13章 打印和...

    visual c++6.0技术内幕 带有NLC的文件查看器

     第8章 使用ActiveX控件  第9章 Internet Explorer 4通用控件  第10章 win32内存管理  第11章 位图  第12章 windows消息处理和多线程编程 第三部分 文档视图结构  第13章 菜单、键盘、加速键、多信息  第14章...

    android开发入门与实战(下)

    第8章 移动信息仓库——Android的数据存储操作 8.1 Android数据存储概述 8.2 轻轻地我保护——SharedPreferences存储 8.3 谁的文件,谁主宰——文件存储 8.4 打造自己的数据库存储——SQLite存储方式 8.4.1 Android...

    android开发入门与实战(上)

    第8章 移动信息仓库——Android的数据存储操作 8.1 Android数据存储概述 8.2 轻轻地我保护——SharedPreferences存储 8.3 谁的文件,谁主宰——文件存储 8.4 打造自己的数据库存储——SQLite存储方式 8.4.1 Android...

    Google Android开发入门与实战的代码

    第8章 移动信息仓库——Android的数据存储操作 136 8.1 Android数据存储概述 136 8.2 轻轻地我保护——SharedPreferences存储 136 8.3 谁的文件,谁主宰——文件存储 140 8.4 打造自己的数据库存储——...

    Visual C++.NET编程技术体验

    对话框高级应用 7.2.2 示例——实现无模式对话框 7.4.5 示例——使用模式属性表及向导属性表 7.5.4 示例——鼠标敏感文字 第8章 GDI+图形编程 本章示例通常含有多段被注释的演示代码,请读者查看...

    Visual C++.NET编程技术体验__实例源码

    对话框高级应用 7.2.2 示例——实现无模式对话框 7.4.5 示例——使用模式属性表及向导属性表 7.5.4 示例——鼠标敏感文字 第8章 GDI+图形编程 本章示例通常含有多段被注释的演示代码,请读者查看...

    《Google Android开发入门与实战》.pdf

    第8章 移动信息仓库——android的数据存储操作 136 8.1 android数据存储概述 136 8.2 轻轻地我保护——sharedpreferences存储 136 8.3 谁的文件,谁主宰——文件存储 140 8.4 打造自己的数据库存储——...

    Google.Android开发入门与实战

    第8章 移动信息仓库——Android的数据存储操作 8.1 Android数据存储概述 8.2 轻轻地我保护——SharedPreferences存储 8.3 谁的文件,谁主宰——文件存储 8.4 打造自己的数据库存储——SQLite存储方式 8.4.1 Android...

    计算机应用基础课件——Word排版知识.pptx

    计算机应用基础课件——Word排版知识全文共72页,当前为第8页。 3.3 文档基本操作 3.3.1 创建新文档 最常用的几种创建新文档的方式 3.3.2 打开文档 打开文档最快捷的方法就是在"资源管理 器"中双击需要的Word文档,...

    轻松搞定Extjs_原创

    第八章:Extjs组件结构 46 一、Extjs的组件结构远比我们想象的复杂 46 二、组件分类 47 三、组件的生命周期 48 四、组件渲染方法render 50 五、小结 52 第九章:按钮与日期选择器 53 一、开始组件学习之旅 53 二、被...

    精通JS脚本之ExtJS框架.part2.rar

    第8章 设计表格类布局 8.1 表格简介 8.1.1 表格的基本属性 8.1.2 制作第一个表格 8.2 表格常用功能详解 8.2.1 主要属性功能 8.2.2 自定义列宽度 8.2.3 执行按列排序 8.2.4 解决中文排序 8.2.5 格式化显示...

Global site tag (gtag.js) - Google Analytics