zoukankan      html  css  js  c++  java
  • 使用checkbox完成一个简单的调查表

    package com.example.checkbox_07;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    private TextView textView;
    private CheckBox checkBox_01;
    private CheckBox checkBox_02;
    private CheckBox checkBox_03;
    private CheckBox checkBox_04;
    private Button btnSubmit;

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

    textView = (TextView) findViewById(R.id.text);
    checkBox_01 = (CheckBox) findViewById(R.id.checkBox_01);
    checkBox_02 = (CheckBox) findViewById(R.id.checkBox_02);
    checkBox_03 = (CheckBox) findViewById(R.id.checkBox_03);
    checkBox_04 = (CheckBox) findViewById(R.id.checkBox_04);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    checkBox_01.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
    if(checkBox_01.isChecked()){
    displayToast("您選擇了"+checkBox_01.getText());
    }
    }
    });
    checkBox_02.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
    if(checkBox_02.isChecked()){
    displayToast("您選擇了"+checkBox_02.getText());
    }
    }
    });
    checkBox_03.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
    if(checkBox_03.isChecked()){
    displayToast("您選擇了"+checkBox_03.getText());
    }
    }
    });
    checkBox_04.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
    if(checkBox_04.isChecked()){
    displayToast("您選擇了"+checkBox_04.getText());
    }
    }
    });
    btnSubmit.setOnClickListener(new OnClickListener(){
    public void onClick(View view){
    int num=0;
    if(checkBox_01.isChecked()){
    num++;
    }
    if(checkBox_02.isChecked()){
    num++;
    }
    if(checkBox_03.isChecked()){
    num++;
    }
    if(checkBox_04.isChecked()){
    num++;
    }
    displayToast("您選擇了"+num+"項");
    }
    });
    }

    public void displayToast(String string){
    Toast toast = Toast.makeText(this, string, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM, 0, 220);
    toast.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

    XML文件:

    <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"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text" />
    <CheckBox
    android:id="@+id/checkBox_01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/checkBox_01"
    />
    <CheckBox
    android:id="@+id/checkBox_02"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/checkBox_02"
    />
    <CheckBox
    android:id="@+id/checkBox_03"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/checkBox_03"
    />
    <CheckBox
    android:id="@+id/checkBox_04"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/checkBox_04"
    />
    <Button
    android:id="@+id/btnSubmit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/submit"/>
    </LinearLayout>

  • 相关阅读:
    LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
    LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
    LeetCode 617. 合并二叉树(Merge Two Binary Trees)
    LeetCode 206. 反转链表(Reverse Linked List) 16
    LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
    LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
    LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302005.html
Copyright © 2011-2022 走看看