android studio創建android項目(6)——CheckBox複選框和toast的混合使用

1、項目需求

  1. 本項目是一個安卓項目,啓動頁面有多個複選框和一個TextView
  2. 複選框分別顯示爲北京、上海、廣州;TextView的文字爲:已經勾選的城市;
  3. 點擊每個複選框時,toast提示覆選框的內容和選中狀態(true和false)
  4. 點擊文字時,toast提示選中的城市有哪些

2、項目分析

  1. checkbox的勾選監聽
    (1)循環取出checkbox,對每個checkbox設置勾選監聽器
  2. TextView的點擊監聽
    (1)TextView需要設置點擊監聽器,監聽器的作用是發一個toast
    (2)遍歷複選框列表,判斷複選框的選中狀態,如果選中,就將文本添加到結果字符串中
    (3)顯示toast
    (4)清除toast內容,還原到初始狀態

3、佈局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    tools:context="com.example.gui.checkbox1.MainActivity">
    <LinearLayout
        android:id="@+id/checklist"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="北京"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上海"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="廣州"/>

    </LinearLayout>
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="已經勾選的城市"/>

</LinearLayout>

4、Activity

package com.example.gui.checkbox1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //聲明checkbox所在的佈局
    LinearLayout checklist;
    //checkbox個數
    int count;
    //保存選中的checkbox
    StringBuffer stringBuffer=new StringBuffer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*1、監聽每個checkbox*/
        checklist=(LinearLayout)findViewById(R.id.checklist);
        count=checklist.getChildCount();
        for(int i=0;i<count;i++){
            CheckBox box=(CheckBox)checklist.getChildAt(i);
            box.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Toast.makeText(getApplicationContext(),buttonView.getText().toString()+isChecked,Toast.LENGTH_SHORT).show();
                }
            });
        }
        /*2、監聽textview*/

        TextView textView=(TextView)findViewById(R.id.textview);
        textView.setOnClickListener(new TextView.OnClickListener(){
            @Override
            public void onClick(View v) {
                stringBuffer.append("已經勾選的城市有:");
                for(int i=0;i<count;i++){
                    CheckBox box=(CheckBox)checklist.getChildAt(i);
                    if(box.isChecked()){
                        stringBuffer.append(box.getText().toString());
                    }
                }
                Toast.makeText(getApplicationContext(),stringBuffer,Toast.LENGTH_SHORT).show();
                //清除內容,還原到初始狀態
                stringBuffer=new StringBuffer();
            }
        });
    }
}

5、運行結果

apk下載鏈接:https://pan.baidu.com/s/1Ze5qDFiS97k4OOqF7v_nsQ
提取碼:l93q

運行結果:
1、啓動頁:
在這裏插入圖片描述
2、選中北京
在這裏插入圖片描述
3、取消選中北京
在這裏插入圖片描述
4、只選中上海、廣州,點擊提交按鈕,toast提示如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章