• 使用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>

  • 相关阅读:
    java序列化深拷贝【转】
    去除DedeCms 5.7后台版权广告链接的方法
    织梦DedeCMS首页调用单页文档内容的方法
    jquery插件lazyload.js延迟加载图片的使用方法
    把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方。Duplicate entry
    js 对象方法、类方法、原型方法的区别;私有属性、公有属性、公有静态属性的区别
    JS中的prototype
    用css3制作旋转加载动画的几种方法
    sencha touch list(列表)、 store(数据源)、model(模型)详解
    webkit webApp 开发技术要点总结
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302005.html
Copyright © 2020-2023  润新知