CheckBox練習(android)

來源:互聯網
上載者:User

CheckBox練習

 

 

checkbox 和 radioButton 相比,不需要 RadioGroup ,每個 checkbox 是單獨的控制項,使用很簡單 isChecked 是擷取是否選擇中,選擇時有 CheckedChangeListener 監聽,具體請看代碼

 

java 代碼,該代碼簡化了在每個chekcbox 中寫 setOnCheckedChangeListener 監聽代碼

 

package zziss.android.checkboxtest;

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

public class CheckBoxTestActivity extends Activity {
    /** Called when the activity is first created. */
    private CheckBox cb_red;
    private CheckBox cb_blue;
    private CheckBox cb_gray;
    private CheckBox cb_green;
    
    private Button   btn_chose;
    private CheckListener listener; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        cb_red = (CheckBox)this.findViewById(R.id.cb_red);
        cb_blue = (CheckBox)this.findViewById(R.id.cb_blue);
        cb_gray = (CheckBox)this.findViewById(R.id.cb_gray);
        cb_green = (CheckBox)this.findViewById(R.id.cb_green);
        btn_chose = (Button)this.findViewById(R.id.btn);
        
        listener = new CheckListener();
        listener.iActivity = this;
        cb_red.setOnCheckedChangeListener(listener);
        cb_blue.setOnCheckedChangeListener(listener);
        cb_gray.setOnCheckedChangeListener(listener);
        cb_green.setOnCheckedChangeListener(listener);
        
        btn_chose.setOnClickListener(listener);
    }
    
    public void showInfo(String str)
    {
        Toast toast = Toast.makeText(this,str,Toast.LENGTH_SHORT);
        toast.show();
    }
    
    public void showCheckTexts()
    {
        String s = "";
        s += cb_red.isChecked()?cb_red.getText().toString()+";":"";
        s += cb_blue.isChecked()?cb_blue.getText().toString()+";":"";
        s += cb_gray.isChecked()?cb_gray.getText().toString()+";":"";
        s += cb_green.isChecked()?cb_green.getText().toString()+";":"";
        showInfo(s);
    }
}

class CheckListener  implements
android.widget.CompoundButton.OnCheckedChangeListener,
android.view.View.OnClickListener

{

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        iActivity.showInfo(buttonView.getText().toString());
    }
    public CheckBoxTestActivity iActivity;
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        iActivity.showCheckTexts();
    }
}

 

main.xml

 

<?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/info" />
    <CheckBox 
        android:id="@+id/cb_red"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb_red"
        />
    <CheckBox 
        android:id="@+id/cb_blue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb_blue"
        />
    <CheckBox 
        android:id="@+id/cb_gray"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb_gray"
        />
    <CheckBox 
        android:id="@+id/cb_green"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb_green"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_chose"
        />
</LinearLayout>

 

 

String.xml

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

    <string name="info">checkbox練習</string>
    <string name="app_name">CheckBoxTest</string>
    <string name="cb_red">紅色</string>
    <string name="cb_blue">藍色</string>
    <string name="cb_gray">灰色</string>
    <string name="cb_green">綠色</string>
    <string name="btn_chose">查看選擇</string>
</resources>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.