6.单选按钮和复选框的应用实例

来源:互联网 发布:如何打开23端口 编辑:程序博客网 时间:2024/05/22 06:39

1.目标效果

  该工程在csdn中的下载链接

2.页面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    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:background="@drawable/bg1">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/sex" />        <RadioGroup         android:id="@+id/sex"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        >            <RadioButton            android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="男"           android:checked="true"           android:id="@+id/man" />    <RadioButton         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="女"        android:id="@+id/woman"/>    </RadioGroup>    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/interest"/>        <!-- 以下是三个复选框按钮 -->    <CheckBox         android:id="@+id/android"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="Android编程"/>        <CheckBox         android:id="@+id/java"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="JAVA编程"/>    <CheckBox         android:id="@+id/html"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="HTML网站设计"/>    <Button         android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="确定"/>    <TextView         android:id="@+id/show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="点击确定,显示您的选择性别"        android:textSize="20dp"        android:layout_marginTop="12dp"        android:singleLine="false"/>         <TextView         android:id="@+id/show1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="点击确定,显示您的兴趣爱好"        android:textSize="20dp"        android:layout_marginTop="12dp"        android:singleLine="false"/></LinearLayout>


3.事件响应

package com.example.sixdemo;import android.os.Bundle;import android.app.Activity;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.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class MainActivity extends Activity {RadioGroup sex=null;RadioButton radio=null;CheckBox android=null;CheckBox java=null;CheckBox html=null;Button button=null;TextView show=null;TextView show1=null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                //获取当前页面的那些控件        sex=(RadioGroup) findViewById(R.id.sex);        android=(CheckBox) findViewById(R.id.android);        java=(CheckBox) findViewById(R.id.java);        html=(CheckBox) findViewById(R.id.html);        button=(Button) findViewById(R.id.button);                //为单选按钮获取事件监听        sex.setOnCheckedChangeListener(new OnCheckedChangeListener()        {@Overridepublic void onCheckedChanged(RadioGroup arg0, int arg1) {//arg1表示在单选按钮组中被选择的那个单选按钮radio=(RadioButton) findViewById(arg1);}                });                //为三个复选框分别设置事件监听,为了减少代码的冗余,这样编写方便        android.setOnCheckedChangeListener(new checkBoxListener());        java.setOnCheckedChangeListener(new checkBoxListener());        html.setOnCheckedChangeListener(new checkBoxListener());            //为按钮添加动作事件,将选取的选项输出到文本框中        button.setOnClickListener(new OnClickListener()        {        public void onClick(View arg0)         {        show=(TextView) findViewById(R.id.show);        if(radio!=null)        {        show.setText("您的性别是:"+radio.getText().toString());        }        else        {        show.setText("您尚未选择性别!");        }        //写到这里了!!!        String check="";        show1=(TextView) findViewById(R.id.show1);        if(android.isChecked())        check=check+android.getText().toString()+" ";        if(java.isChecked())        check=check+java.getText().toString()+" ";        if(html.isChecked())        check=check+html.getText().toString()+" ";                if(check.equals(""))        show1.setText("您尚未选择任何爱好!");        else        show1.setText("您选择的爱好是:"+check);        }        });    }    @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;    }    }class checkBoxListener implements OnCheckedChangeListener, android.widget.CompoundButton.OnCheckedChangeListener {public void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stub}@Overridepublic void onCheckedChanged(RadioGroup arg0, int arg1) {// TODO Auto-generated method stub}}


4.运行效果


原创粉丝点击