Home  >  Article  >  Backend Development  >  android RadioGroup checkBox button application

android RadioGroup checkBox button application

巴扎黑
巴扎黑Original
2016-12-20 15:04:461228browse

The first step is to create an android project.

The second step is to modify the generated res/layout/main.xml

view plaincopy to clipboardprint?

< ;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

> height="wrap_content"

android:text="checkbox 1 "/& gt; & & lt; checkbox

Android: ID ="@+ID/Checkbox1 "

Android: layout_width =" wrap_content "

Android: layout_height =" wrap_ Content "d Android: text =" check Box 2" />

android:id="@+id/radioGroup"

android:orientation="vertical"

android:layout_width="fill _parent"

android:layout_height="fill_parent ">

                                                                                                                                                                         < ="Beijing" />

                                ; Radiobutton android: ID = "@+ID/randion2"

Android: layout_width = "wrap_content" android: layout_heigh_Content "

Android: Text = "Tianjin"/& GT;

& LT; Radiobutton android: ID = "@+ID/randion3"

Android: layout_width = "wrap_content" android: layout_heigh = "wrap_content"

Android: text = "Shanghai"/& & lti; obutton android: ID = "@+ID/randion4 "

Android: layout_width =" wrap_content "android: layout_height =" wrap_content "

Android: text =" Chongqing "/& LT;/Radiogroup & GT; /& Lt;/linearlayout & gt;

third step , add the main code

view plaincopy to clipboardprint?

import android.app.Activity;

import android.os.Bundle;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.CompoundButton.OnCheckedChangeListener; ity implements OnCheckedChangeListener {

 /** Called when the activity is first created.  */

      

    RadioButton  r1 = null;  

    RadioButton  r2 = null;  

    RadioButton  r3 = null;  

    RadioButton  r4 = null;  

      

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

          

        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);   

        CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);  

        //获得单选按钮组  

        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);  

        //获得单选按钮  

        r1 = (RadioButton )findViewById(R.id.radion1);  

        r2 = (RadioButton )findViewById(R.id.radion2);  

        r3 = (RadioButton )findViewById(R.id.radion3);  

        r4 = (RadioButton )findViewById(R.id.radion4);  

          

        checkBox.setChecked(true);   

          

        r1.setClickable(true);  

        //监听多选按钮  

        checkBox.setOnCheckedChangeListener(this);  

        checkBox1.setOnCheckedChangeListener(this);  

        //监听单选按钮  

        radioGroup.setOnCheckedChangeListener(mChangeRadio);  

          

    }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn