Android[初級教程]第三篇 RadioButton和CheckBox控制項

來源:互聯網
上載者:User

 這次我們講RadioButton和CheckBox控制項,首先我們講RadioButton控制項。

相信大家一定看過西遊記,裡面有妖精抓唐僧的情境,我們就用這兩個控制項來類比一下,RadionButton控制項呢是說每次妖精只能抓一個人,每次一個,抓幾個就得抓幾次,這可把妖精們忙壞了,呵呵

我們看一下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="fill_parent"android:layout_height="fill_parent"><RadioGroup android:id="@+id/radioGroup1"android:layout_width="wrap_content" android:layout_height="wrap_content"><RadioButton android:layout_height="wrap_content"android:layout_width="wrap_content" android:text="唐僧" android:id="@+id/tangseng"/><RadioButton android:id="@+id/wukong" android:text="孫悟空"android:layout_width="wrap_content" android:layout_height="wrap_content"/><RadioButton android:layout_height="wrap_content"android:layout_width="wrap_content" android:text="豬八戒" android:id="@+id/bajie"/><RadioButton android:layout_height="wrap_content"android:layout_width="wrap_content" android:text="沙和尚" android:id="@+id/shaseng"/></RadioGroup><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:text="按鈕" android:id="@+id/button"></Button><TextView android:layout_height="wrap_content"android:layout_width="fill_parent" android:text="@string/hello"android:id="@+id/text"></TextView></LinearLayout>

Activity中的java代碼如下:

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RadioButton;import android.widget.TextView;public class ButtonDemoActivity extends Activity implements OnClickListener{private TextView text = null;private RadioButton tangseng;private RadioButton wukong;private RadioButton bajie;private RadioButton shaseng;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 通過ID尋找到main.xml中的TextView控制項text = (TextView) findViewById(R.id.text);//唐僧單選框tangseng = (RadioButton)findViewById(R.id.tangseng);//悟空單選框wukong = (RadioButton)findViewById(R.id.wukong);//八戒單選框bajie = (RadioButton)findViewById(R.id.bajie);//沙僧單選框shaseng = (RadioButton)findViewById(R.id.shaseng);// 通過ID尋找到main.xml中的Button控制項Button button = (Button) findViewById(R.id.button);// 為Button控制項增加單擊監聽器button.setOnClickListener(this);}private void updateText(String string){// 將文本資訊設定給TextView控制項顯示出來text.setText(string);}@Overridepublic void onClick(View v){String str = "";//唐僧單選框被選中if(tangseng.isChecked()){str += "唐僧~";}//悟空單選框被選中if(wukong.isChecked()){str += "悟空~";}//八戒單選框被選中if(bajie.isChecked()){str += "八戒~";}//沙僧單選框被選中if(shaseng.isChecked()){str += "沙僧~";}//沒有人被選中if(str.equals("")){str += "沒有人";}str +="被妖精抓走了!";updateText(str);}}

 

每次只抓一個多麻煩啊,有妖精想了,我本領高強,我一次多抓幾個,這樣省事多了,這下,我們就用到了CheckBox控制項啦。

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="fill_parent"android:layout_height="fill_parent"><CheckBox android:text="唐僧" android:id="@+id/tangseng"android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox><CheckBox android:text="孫悟空" android:id="@+id/wukong"android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox><CheckBox android:text="豬八戒" android:id="@+id/bajie"android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox><CheckBox android:text="沙和尚" android:id="@+id/shaseng"android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:text="按鈕" android:id="@+id/button"></Button><TextView android:layout_height="wrap_content"android:layout_width="fill_parent" android:text="@string/hello"android:id="@+id/text"></TextView></LinearLayout>

Activity中的java代碼如下:

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.TextView;public class ButtonDemoActivity extends Activity implements OnClickListener{private TextView text = null;private CheckBox tangseng;private CheckBox wukong;private CheckBox bajie;private CheckBox shaseng;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 通過ID尋找到main.xml中的TextView控制項text = (TextView) findViewById(R.id.text);//唐僧複選框tangseng = (CheckBox)findViewById(R.id.tangseng);//悟空複選框wukong = (CheckBox)findViewById(R.id.wukong);//八戒複選框bajie = (CheckBox)findViewById(R.id.bajie);//沙僧複選框shaseng = (CheckBox)findViewById(R.id.shaseng);// 通過ID尋找到main.xml中的Button控制項Button button = (Button) findViewById(R.id.button);// 為Button控制項增加單擊監聽器button.setOnClickListener(this);}private void updateText(String string){// 將文本資訊設定給TextView控制項顯示出來text.setText(string);}@Overridepublic void onClick(View v){String str = "";//唐僧複選框被選中if(tangseng.isChecked()){str += "唐僧~";}//悟空複選框被選中if(wukong.isChecked()){str += "悟空~";}//八戒複選框被選中if(bajie.isChecked()){str += "八戒~";}//沙僧複選框被選中if(shaseng.isChecked()){str += "沙僧~";}//沒有人被選中if(str.equals("")){str += "沒有人";}str +="被妖精抓走了!";updateText(str);}}

哈哈,這下妖精們高興了,一次把四個都抓走都行了?什嗎?四個都抓走了,那誰救唐僧啊?放心,你看西遊記最後唐僧不也是沒被妖精吃掉嘛,高人自有人相救,呵呵,囉嗦了,這一篇就到這裡吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.