android 單選(RadioGroup )複選(CheckBox)按鈕的應用

來源:互聯網
上載者:User
第一步,還是建立android項目。

第二步,修改已經產生的res/layout/main.xml

view plaincopy to clipboardprint?

<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<CheckBox

android:id="@+id/checkBox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="複選框1" />

<CheckBox

android:id="@+id/checkBox1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="複選框2" />

<RadioGroup

android:id="@+id/radioGroup"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<RadioButton android:id="@+id/radion1"

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

android:text="北京" />

<RadioButton android:id="@+id/radion2"

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

android:text="天津" />

<RadioButton android:id="@+id/radion3"

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

android:text="上海" />

<RadioButton android:id="@+id/radion4"

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

android:text="重慶" />

</RadioGroup>

</LinearLayout>

第三步,添加主要代碼

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;

public class checkBox extends Activity 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);

}

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.