Android遊戲開發系統控制項-CheckBox
CheckBox是Android系統最普通的UI控制項,繼承了Button按鈕
下面通過一個執行個體來學習
功能:實現複選框的功能
建立項目“CheckBoxProject”
運行項目效果:
代碼實現:
=>>main.xml
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cb1"
android:id="@+id/cb1"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cb2"
android:id="@+id/cb2"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cb3"
android:id="@+id/cb3"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cb1"
android:id="@+id/cb1"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cb2"
android:id="@+id/cb2"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cb3"
android:id="@+id/cb3"
/>
</LinearLayout>
=>>string.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">CheckBoxProject!</string>
<string name="app_name">CheckBox</string>
<string name="cb1">CheckBox1</string>
<string name="cb2">CheckBox2</string>
<string name="cb3">CheckBox3</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">CheckBoxProject!</string>
<string name="app_name">CheckBox</string>
<string name="cb1">CheckBox1</string>
<string name="cb2">CheckBox2</string>
<string name="cb3">CheckBox3</string>
</resources>
=>>CheckBoxProject.java
[java]
package com.checkBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class CheckBoxActivity extends Activity implements OnCheckedChangeListener{
private CheckBox cb1,cb2,cb3;//建立3個CheckBox對象
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//執行個體化3個CheckBox
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}
//重寫監聽器的抽象函數
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//buttonView 選中狀態發生改變的那個按鈕
//isChecked 表示按鈕新的狀態(true/false)
if(cb1 == buttonView || cb2 == buttonView || cb3 == buttonView ){
if(isChecked){
//顯示一個提示資訊
toastDisplay(buttonView.getText() + "選中");
}else{
toastDisplay(buttonView.getText() + "取消選中");
}
}
}
public void toastDisplay(String str){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
package com.checkBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class CheckBoxActivity extends Activity implements OnCheckedChangeListener{
private CheckBox cb1,cb2,cb3;//建立3個CheckBox對象
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//執行個體化3個CheckBox
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}
//重寫監聽器的抽象函數
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//buttonView 選中狀態發生改變的那個按鈕
//isChecked 表示按鈕新的狀態(true/false)
if(cb1 == buttonView || cb2 == buttonView || cb3 == buttonView ){
if(isChecked){
//顯示一個提示資訊
toastDisplay(buttonView.getText() + "選中");
}else{
toastDisplay(buttonView.getText() + "取消選中");
}
}
}
public void toastDisplay(String str){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
對CheckBox進行監聽,步驟如下:
步驟1:使用OnCheckChangeListener介面,這裡的介面匯入的是:
“android.widget.CompoundButton.OnCheckChangeListener”;
步驟2:重寫監聽器的抽象函數“onCheckedChanged()”
步驟3:將每個CheckBox組件綁定監聽器。
通過重寫的onCheckedChanged(CompoundButton buttonView,boolean isChecked)函數一個參數來確定哪個CheckBox狀態發生改變;根據第二個參數來確定改變的CheckeBox的具體狀態值,true為勾選,false為未勾選。
CheckBoxActivity類中還定義了toastDisplay()函數,其實是為了使用Android的一種提示資訊的方式:Toast:主要用於提示資訊,使用起來很方便;先建立Toast對象,然後調用makeText()方法得到一個Toast執行個體對象。
makeText(Context context,CharSequence text,int duration)
第一參數是內容物件;第二個參數顯示的常值內容;第三個參數顯示提示訊息的期間;其值有兩個常量:LENGTH_SHORT(短暫持續)和LENGTH_LONG(略長持續)。
最後,使用Toast對象調用show()方法即可。
作者:wwj