`
bk_lin
  • 浏览: 322636 次
社区版块
存档分类
最新评论

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

阅读更多

 

CheckBox 第八章(6)

在本节中,将为CheckBox View创建一个活动。创建活动的步骤和前面的章节一致。因此,将会为你提供三个主要活动文件——AndroidManifest.xml, 
checkbox.xml, 和 testCheckBox.java。这些文件在下面的部分提供。

AndroidManifest.xml

这个部分包含当前AndroidView的完整代码。如果你使用Eclipse,修改你活动的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> 
<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代码。使用在本章早些时候提到的方法,在项目中创建一个新的XML文件,把它命名为checkbox.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.java 
本部分涵盖了执行Checkbox活动所需的最后新文件。在项目中创建一个新的.java文件,并把它命名为testCheckBox.java。这个是活动的主文件并且包含可执行代码。在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 { 
180 Android: A Programmer’s Guide 
@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); 

}

AndroidViews.java 
创建这个活动的最后一步就是编辑AndroidViews.java文件。如果你要从AndroidViews主活动中呼叫testCheckBox活动,你必须增加代码到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; 
Chapter 8: Lists, Menus, and Other Views 181 
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选项。点击Change Layout和Change Text Color按钮。

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics