Android调查问卷的创建与提交

引言

         图片按钮、复选按钮、单选按钮、普通按钮、单击按钮弹出对话框的使用!

代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ImageButton
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/d"
    />
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="20dp"
          >
        <CheckBox
                android:id="@+id/Chinese"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Chinese"
        />
        <CheckBox
                android:id="@+id/English"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="English"
        />
        <Button
                android:id="@+id/checkboxChexk"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="复选框按钮检查"
        />
        <RadioButton
                android:id="@+id/female"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="female"
        />
        <RadioButton
                android:id="@+id/male"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="male"
        />
        <Button
                android:id="@+id/RadioButton"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="单选按钮检查"
        />

    </LinearLayout>
</LinearLayout>

MainActivity

package com.example.demo4;

import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private CheckBox  Chinese;
    private CheckBox English;
    private ImageButton img;
    private Button button1;
    private Button button2;
    private RadioButton rd1;
    private RadioButton rd2;
    private List<String> like = new ArrayList<String>();
    private void showNormalDialog(String s){
        //创建dialog构造器
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        //设置title
        normalDialog.setTitle("提示信息");
        //设置内容
        normalDialog.setMessage(s);
        //设置按钮
        normalDialog.setPositiveButton("关闭"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        //创建并显示
        normalDialog.create().show();
    }
    private String sex;

    //复选框单选按钮事件
    @Override
    public void onCheckedChanged(CompoundButton checkBox,boolean checked) {
        // TODO Auto-generated method stub
        switch (checkBox.getId()) {
            case R.id.English:
                if (checked) {// 选中吃
                    like.add("English");
                } else {
                    like.remove("English");
                }
                break;
            case R.id.Chinese:
                if (checked) {// 选中唱歌
                    like.add("Chinese");
                } else {
                    like.remove("Chinese");
                }
                break;
            default:
                break;
        }
        if(checked){
            disableOthers(checkBox.getId());
            sex=checkBox.getText().toString();
        }
    }
    private void disableOthers(int viewId) {
        if(R.id.male!=viewId&&rd2.isChecked()){
            rd2.setChecked(false);
        }
        if(R.id.female!=viewId&&rd1.isChecked()){
            rd1.setChecked(false);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Chinese=(CheckBox)findViewById(R.id.Chinese);
        English=(CheckBox)findViewById(R.id.English);
        // 绑定事件
        Chinese.setOnCheckedChangeListener(this);
        English.setOnCheckedChangeListener(this);

        img=(ImageButton)findViewById(R.id.image);
        img.setOnClickListener(new View.OnClickListener() {
            String s="您点击了图片按钮";
            @Override
            public void onClick(View v) {
               //
                showNormalDialog(s);

            }
        });
        button1=(Button)findViewById(R.id.checkboxChexk);
        button1.setOnClickListener(new View.OnClickListener() {
            String langle="";
            @Override
            public void onClick(View v) {
                if (like.size() == 0){
                  langle ="未选择";
                }else{
                     langle="";
                    for(String s:like){
                        langle+=s+" ";
                    }
                    langle+="被选择";
                }
                showNormalDialog(langle);
            }
        });
        rd1=(RadioButton)findViewById(R.id.female);
        rd2=(RadioButton)findViewById(R.id.male) ;
        rd1.setOnCheckedChangeListener(this);
        rd2.setOnCheckedChangeListener(this);
        button2=(Button)findViewById(R.id.RadioButton);
        button2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                showNormalDialog("性别是:"+sex);
            }
        });
    }

}

目录结构

在这里插入图片描述

运行结果

在这里插入图片描述

升级版

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ImageButton
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/d"
    />
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="20dp"
          >
        <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="精通的语言"
        />
        <CheckBox
                android:id="@+id/Chinese"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Chinese"
        />

        <CheckBox
                android:id="@+id/English"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="English"
        />
        <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="性别"
        />
        <RadioButton
                android:id="@+id/female"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="female"
        />
        <RadioButton
                android:id="@+id/male"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="male"
        />
        <Button
                android:id="@+id/Button"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="提交"
        />

    </LinearLayout>
</LinearLayout>

MainActivity

package com.example.demo4;

import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private CheckBox  Chinese;
    private CheckBox English;
    private ImageButton img;
    private Button button;
    private RadioButton rd1;
    private RadioButton rd2;
    private List<String> like = new ArrayList<String>();
    private void showNormalDialog(String s){
        //创建dialog构造器
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        //设置title
        normalDialog.setTitle("提示信息");
        //设置内容
        normalDialog.setMessage(s);
        //设置按钮
        normalDialog.setPositiveButton("关闭"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        //创建并显示
        normalDialog.create().show();
    }
    private String sex;

    //复选框单选按钮事件
    @Override
    public void onCheckedChanged(CompoundButton checkBox,boolean checked) {
        // TODO Auto-generated method stub
        switch (checkBox.getId()) {
            case R.id.English:
                if (checked) {// 选中吃
                    like.add("English");
                } else {
                    like.remove("English");
                }
                break;
            case R.id.Chinese:
                if (checked) {// 选中唱歌
                    like.add("Chinese");
                } else {
                    like.remove("Chinese");
                }
                break;
            default:
                break;
        }
        if(checked){
            disableOthers(checkBox.getId());
            sex=checkBox.getText().toString();
        }
    }
    private void disableOthers(int viewId) {
        if(R.id.male!=viewId&&rd2.isChecked()){
            rd2.setChecked(false);
        }
        if(R.id.female!=viewId&&rd1.isChecked()){
            rd1.setChecked(false);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Chinese=(CheckBox)findViewById(R.id.Chinese);
        English=(CheckBox)findViewById(R.id.English);
        // 绑定事件
        Chinese.setOnCheckedChangeListener(this);
        English.setOnCheckedChangeListener(this);

        img=(ImageButton)findViewById(R.id.image);
        img.setOnClickListener(new View.OnClickListener() {
            String s="您点击了图片按钮";
            @Override
            public void onClick(View v) {
               //
                showNormalDialog(s);

            }
        });
        rd1=(RadioButton)findViewById(R.id.female);
        rd2=(RadioButton)findViewById(R.id.male) ;
        rd1.setOnCheckedChangeListener(this);
        rd2.setOnCheckedChangeListener(this);
        button=(Button)findViewById(R.id.Button);
        button.setOnClickListener(new View.OnClickListener(){
            String langle="";
            @Override
            public void onClick(View v) {
                if (like.size() == 0){
                    langle ="未选择";
                }else{
                    langle="";
                    for(String s:like){
                        langle+=s+" ";
                    }
                    langle+="\n";
                }

                showNormalDialog("精通的语言:"+langle+"性别:"+sex);
            }
        });
    }

}

运行结果

在这里插入图片描述

在这里插入图片描述

参考链接

java中的List简单介绍
Android中复选框(CheckBox)的使用
Android系统对话框使用详解(最详细)
Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值