android CheckBox使用和狀態獲得,androidcheckbox

來源:互聯網
上載者:User

android CheckBox使用和狀態獲得,androidcheckbox

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical">    <!-- 定義CheckBox控制項 ,代表籃球選項-->    <CheckBox        android:id="@+id/CbBasketball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="籃球" />    <!-- 定義CheckBox控制項 ,代表乒乓球選項-->    <CheckBox        android:id="@+id/CbPingpangball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="乒乓球" />    <!-- 定義CheckBox控制項 ,代表足球選項-->    <CheckBox        android:id="@+id/CbFootball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="足球" />    <!-- 定義TextView控制項,來顯示選中結果 -->    <TextView        android:id="@+id/TvResult"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/str" /></LinearLayout>
package com.example.yanlei.yl2;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;//匯入必備的包import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;import android.widget.CompoundButton.OnCheckedChangeListener;public class MainActivity extends AppCompatActivity {    private CheckBox CbBasketball;        //定義籃球的複選框對象    private CheckBox CbPingpangball;    //定義乒乓球的複選框對象    private CheckBox CbFootball;        //定義足球的複選框對象    private TextView TvResult;            //定義結果文本便簽對象    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //調用父類的onCreate方法        //通過setContentView方法設定當前頁面的布局檔案為activity_main        setContentView(R.layout.activity_main);        findView();            //擷取頁面中的控制項        setListener();        //設定控制項的監聽器    }    private void setListener() {        // TODO Auto-generated method stub        //設定所有CheckBox的狀態改變監聽器        CbBasketball.setOnCheckedChangeListener(myCheckChangelistener);        CbPingpangball.setOnCheckedChangeListener(myCheckChangelistener);        CbFootball.setOnCheckedChangeListener(myCheckChangelistener);    }    OnCheckedChangeListener myCheckChangelistener = new OnCheckedChangeListener() {        @Override        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {            // TODO Auto-generated method stub            //設定TextView的內容顯示CheckBox的選擇結果            setText();        }    };    private void findView() {        // TODO Auto-generated method stub        //通過findViewById得到對應的控制項對象        CbBasketball = (CheckBox)findViewById(R.id.CbBasketball);        CbPingpangball = (CheckBox)findViewById(R.id.CbPingpangball);        CbFootball = (CheckBox)findViewById(R.id.CbFootball);        TvResult = (TextView)findViewById(R.id.TvResult);    }    private void setText(){        String str;        TvResult.setText("");    //清空TextView的內容        //如果CbBasketball被選中,則加入TvResult內容顯示        if (CbBasketball.isChecked()) {            str = TvResult.getText().toString()+CbBasketball.getText().toString()+",";            TvResult.setText(str);        }        //如果CbPingpangball被選中,則加入TvResult內容顯示        if (CbPingpangball.isChecked()) {            str = TvResult.getText().toString()+CbPingpangball.getText().toString()+",";            TvResult.setText(str);        }        //如果CbFootball被選中,則加入TvResult內容顯示        if (CbFootball.isChecked()) {            str = TvResult.getText().toString()+CbFootball.getText().toString();            TvResult.setText(str);        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.