当前位置:操作系统 > 安卓/Android >>

Android游戏开发系统控件-CheckBox

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 选中状态发生改变的那个按钮  
     

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,