六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 35|回复: 0

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

[复制链接]

升级  15.33%

21

主题

21

主题

21

主题

秀才

Rank: 2

积分
73
 楼主| 发表于 2013-1-15 02:39:29 | 显示全部楼层 |阅读模式
这里是你的完整的AndroidManifest.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>

</application>

</manifest>

处理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,如下:

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

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

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

下面的章节会为你提供在工程中实现剩余五个视图的代码。
 
Button
 
看一下下面的代码。这个代码包含了四个文件,AndroidManifest.xmlButton.xmltestButton.javaAndroidViews.java。将文件中的这些代码添加到在已存在的AndroidViews Activity中。
AndroidManifest.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>
</application>
</manifest>
Button.xml

<?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">
<Button android:id="@+id/testButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the test Button"/>
<Button android:id="@+id/layoutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Layout"/>
<Button android:id="@+id/textColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Color"/>
</LinearLayout>
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的样子。

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

点击Change Text Color Button,文本就会变红,如下:

 
CheckBox
本节中你会为CheckBox视图创建一个Activity。创建Activity的步骤和前面的章节中的相同。因此,会为你提供三个主Activity文件的全部代码——AndroidManifest.xmlcheckbox.xmltestCheckBox.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>
</application>
</manifest>
 
Checkbox.xml
本节展示了checkbox.xml的全部代码。使用本章前面的指导在你的项目中创建一个名为checkbox.xml的新XML文件。使用下面的代码构建你的文件。
<?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"
> 
<CheckBox android:id="@+id/testCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the test CheckBox"/>
<Button android:id="@+id/layoutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Layout"/>
<Button android:id="@+id/textColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Color"/>
</LinearLayout>
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);
}
}
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表