Android 爱好调查页面

来源:互联网 发布:java语言与basic 编辑:程序博客网 时间:2024/05/18 03:03

使用工具 Android studio

首先贴一下

layout的代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"><TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="你喜欢的运动是什么?点击下方按钮"/>    <CheckBox        android:id="@+id/CbBasketball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="篮球" />    <CheckBox        android:id="@+id/CbPingpangball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="乒乓球" />    <CheckBox        android:id="@+id/Cblittleball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="小球"/>    <CheckBox        android:id="@+id/Cbcoolball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="冰球"/>    <CheckBox        android:id="@+id/CbFootball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="足球" />    <CheckBox        android:id="@+id/badminton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="羽毛球"/>    <TextView        android:id="@+id/TvResult"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/str" /></LinearLayout>

main部分代码

package com.sqlite.exam.agreepage;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private CheckBox CbBaskball;                        //定义篮球的复选框对象    private CheckBox CbPingpangball;                   //定义乒乓球的复选框对象    private CheckBox CbFootball;                        //定义足球的复选框对象    private CheckBox Cblittleball;                      //定义小球的复选框    private CheckBox Cbcoolball;                        //冰球    private CheckBox badminton;                         //羽毛球    private TextView TvResult;                          //定义结果文本便签对象    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findView();                            //获取页面中的控件        setListener();                                      //设置控件的监听器    }    private void setListener()    {        //设置所有CheckBox的状态改变监听器        CbBaskball.setOnCheckedChangeListener(myCheckChanglistener);        CbPingpangball.setOnCheckedChangeListener(myCheckChanglistener);        CbFootball.setOnCheckedChangeListener(myCheckChanglistener);        Cblittleball.setOnCheckedChangeListener(myCheckChanglistener);        Cbcoolball.setOnCheckedChangeListener(myCheckChanglistener);        badminton.setOnCheckedChangeListener(myCheckChanglistener);    }    CompoundButton.OnCheckedChangeListener myCheckChanglistener = new CompoundButton.OnCheckedChangeListener() {        @Override        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {            setText();        }    };    private void findView()    {        //通过findView得到对应的控件对象        CbBaskball= (CheckBox)findViewById(R.id.CbBasketball);        CbPingpangball =(CheckBox)findViewById(R.id.CbPingpangball);        CbFootball =(CheckBox)findViewById(R.id.CbFootball);        TvResult   =(TextView)findViewById(R.id.TvResult);        Cblittleball = (CheckBox)findViewById(R.id.Cblittleball);        Cbcoolball   = (CheckBox)findViewById(R.id.Cbcoolball);        badminton    = (CheckBox)findViewById(R.id.badminton);    }    private void setText()    {        String str;        TvResult.setText("");                                   //清空TextView的内容        //如果CbBasketball被选中,则加入TvResult内容显示        if(CbBaskball.isChecked()){            str = TvResult.getText().toString()+CbBaskball.getText().toString()+",";            TvResult.setText(str);        }       if (CbPingpangball.isChecked())       {           str = TvResult.getText().toString()+CbPingpangball.getText().toString()+",";           TvResult.setText(str);       }       if(CbFootball.isChecked())       {           str = TvResult.getText().toString()+CbFootball.getText().toString()+",";        TvResult.setText(str);       }       if(Cblittleball.isChecked())       {           str = TvResult.getText().toString()+Cblittleball.getText().toString()+",";            TvResult.setText(str);       }       if (Cbcoolball.isChecked())       {           str = TvResult.getText().toString()+Cbcoolball.getText().toString()+",";           TvResult.setText(str);       }       if(badminton.isChecked())       {           str = TvResult.getText().toString()+badminton.getText().toString();           TvResult.setText(str);       }    }}
string部分代码

<resources>    <string name="app_name">agreepage</string>    <string name="str"></string></resources>

最后完成代码范例。