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>

  • 相关阅读:
    https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic net::ERR_CONNECTION_TIMED_OUT问题解决
    nginx 下使用 bootstrap 字体的问题
    php中函数preg_match或preg_match_all 第三个参数$match的解释
    thinkphp中 volist循环的 mod取值的问题
    mysql中sql注入的随笔
    修改mysql的字符集和默认存储引擎
    使用Marsedit在博客园写作
    Server Tomcat v7.0 Server at localhost failed to start.临时解决办法
    【转】Linux Mint 17.2 gedit中文乱码
    HashMap和HashSet
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302005.html
Copyright © 2011-2022 走看看