Android开发从入门到精通(8) _5

按钮 第八章(5)

看看下面的代码。这段代码代表了四个文件,AndroidManifest.xml, Button.xml, testButton.java, 和 AndroidViews.java。增加代码到现存的AndroidViews活动中。

警告

如果你没有一开始就跟从本章,你执行代码时可能会遇到麻烦。要确保得到完整的项目,请从本章的开始开始阅读。

AndroidManifest.xml

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

<manifestxmlns:android=http://schemas.android.com/apk/res/android

package="android_programmers_guide.AndroidViews"

<applicationandroid:icon="@drawable/icon">

<activityandroid:name=".AndroidViews"

android:label="@string/app_name">

<intent-filter>

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

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

</intent-filter>

</activity>

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

<intent-filter>

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

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

</intent-filter>

</activity>

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

<intent-filter>

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

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

</intent-filter>

</activity>

</application>

</manifest>

Button.xml

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

<LinearLayoutxmlns:android=http://schemas.android.com/apk/res/android

android:orientation="vertical"

android:layout_width="fill_parent"

Chapter8:Lists,Menus,andOtherViews173

android:layout_height="fill_parent">

<Buttonandroid:id="@+id/testButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="ThisisthetestButton"/>

<Buttonandroid:id="@+id/layoutButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="ChangeLayout"/>

<Buttonandroid:id="@+id/textColorButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="ChangeTextColor"/>

</LinearLayout>

testButton.java

package android_programmers_guide.AndroidViews; 

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.Button;

importandroid.graphics.Color;

publicclasstestButtonextendsActivity{

@Override

publicvoidonCreate(Bundleicicle){

super.onCreate(icicle);

setContentView(R.layout.Button);

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

finalButtonchangeButton=(Button)findViewById(R.id.layoutButton);

changeButton.setOnClickListener(newButton.OnClickListener(){

publicvoidonClick(Viewv){

changeOption(Button);}

});

finalButtonchangeButton2=(Button)

findViewById(R.id.textColorButton);

changeButton2.setOnClickListener(newButton.OnClickListener(){

publicvoidonClick(Viewv){

changeOption2(Button);

}

});

}

publicvoidchangeOption(ButtonButton){

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

Button.setHeight(30);

}

174Android:AProgrammer’sGuide

Chapter8:Lists,Menus,andOtherViews175

else{

Button.setHeight(100);

}

}

publicvoidchangeOption2(ButtonButton){

Button.setTextColor(Color.RED);

}

}

AndroidViews.java

package android_programmers_guide.AndroidViews; 

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Menu;

importandroid.content.Intent;

publicclassAndroidViewsextendsActivity{

/**CalledwhentheActivityisfirstcreated.*/

@Override

publicvoidonCreate(Bundleicicle){

super.onCreate(icicle);

setContentView(R.layout.main);

}

@Override

publicbooleanonCreateOptionsMenu(Menumenu){

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

returntrue;

}

@Override

publicbooleanonOptionsItemSelected(Menu.Itemitem){

switch(item.getId()){

case0:

showAutoComplete();

returntrue;

case1:

showButton();

returntrue;

case2:

returntrue;

case3:

returntrue;

case4:

returntrue;

case5:

returntrue;

}

returntrue;

}

publicvoidshowButton(){

IntentshowButton=newIntent(this,testButton.class);

startActivity(showButton);

}

publicvoidshowAutoComplete(){

Intentautocomplete=newIntent(this,AutoComplete.class);

startActivity(autocomplete);

}

启动你的应用程序并且选择从菜单上选择按钮选项。试着点击Change Layout按钮。再一次,对文本来说,结果是一个较宽的显示区域,点击改变Text Color按钮并且文本变成红色。

更多信息请查看 http://www.javady.com/index.php/category/thread

相关推荐