0%

Android UI设计 CheckBox复选框用法

CheckBox复选框用法,先看效果图 选择点击按钮后,会在标题栏显示选中的项。 main.xml代码:

<CheckBox android:id="@+id/box1"
          android:text="我是BOX1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
/>
<CheckBox android:id="@+id/box2"
          android:text="我是BOX2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
/>
<Button android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按我"
/>

主程序代码:

package com.pocketdigi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        boxDemo();
        
    }
    CheckBox box1;
    CheckBox box2;
    private void boxDemo(){
        box1=(CheckBox)findViewById(R.id.box1);
        box2=(CheckBox)findViewById(R.id.box2);
        Button b1=(Button)findViewById(R.id.b1);
        b1.setOnClickListener(Dialog);
    }
    private Button.OnClickListener Dialog=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String s="";
            if(box1.isChecked()){
                s+="box1,";
            }
            if(box2.isChecked()){
                s+="box2";
            }
            setTitle(s);
        }
        
    };
}