單選按鈕RadioGroup與復(fù)選框CheckBox-創(chuàng)新互聯(lián)

在AndroidApp應(yīng)用中,單選按鈕和復(fù)選框也是經(jīng)常使用的,下面我們一起學(xué)習(xí)一下。我們需要學(xué)習(xí)Android中的基本控件:(1)單選按鈕RadioGroup、(2)復(fù)選框CheckBox。

成都創(chuàng)新互聯(lián)成都網(wǎng)站建設(shè)定制網(wǎng)站制作,是成都網(wǎng)站制作公司,為成都純水機提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站改版熱線:028-86922220

一、設(shè)計登錄窗口

打開“res/layout/activity_main.xml”文件。

 1、分別從工具欄向activity拖出1個單選按鈕列表RadioGroup(注意自動包含3個單選按鈕RadioButton)、2個復(fù)選框CheckBox、1個按鈕Button。這3個控件均來自Form Widgets。

2、打開activity_main.xml文件。

我們把自動生成的代碼修改成如下代碼,具體為:

(1)RatioGroup的id修改為gender,兩個RadioButton的id分別修改為male和female,其文本分別修改為男和女;

注意:第1個單選按鈕android:checked="true"表示此單選按鈕默認為選擇。

(2)兩個CheckBox的id修改為football和basketball,其文本分別修改為足球和藍球;

(3)Buttion的id修改為save,其文本修改為"保存"。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  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" >

  <RadioGroup
    android:id="@+id/gender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <RadioButton
      android:id="@+id/male"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:text="@string/male" />

    <RadioButton
      android:id="@+id/female"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/female" />
  </RadioGroup>

  <CheckBox
    android:id="@+id/football"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/gender"
    android:layout_below="@+id/gender"
    android:text="@string/football" />

  <CheckBox
    android:id="@+id/basketball"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/football"
    android:layout_below="@+id/football"
    android:text="@string/basketball" />

  <Button
    android:id="@+id/save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/basketball"
    android:layout_below="@+id/basketball"
    android:layout_marginTop="28dp"
    android:text="@string/save" />

</RelativeLayout>

二、單擊事件 

打開“src/com.genwoxue.RadioGroupCheckBox/MainActivity.java”文件。

然后輸入以下代碼:

package com.example.hw;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {
private RadioButton rbMale = null;
private RadioButton rbFemale = null;
private CheckBox cbFootBall = null;
private CheckBox cbBasketBall = null;
private Button btnSave = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rbMale = (RadioButton) super.findViewById(R.id.male);
rbFemale = (RadioButton) super.findViewById(R.id.female);
cbFootBall = (CheckBox) super.findViewById(R.id.football);
cbBasketBall = (CheckBox) super.findViewById(R.id.basketball);
btnSave = (Button) super.findViewById(R.id.save);//剛開始括號里的Button寫成CheckBox了,導(dǎo)致ClassCastException

   //而且模擬器出現(xiàn)Unfortunately,project名has stopped錯誤
btnSave.setOnClickListener(new SaveOnClickListener());
}

@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;
}
private class SaveOnClickListener implements OnClickListener{

public void onClick(View v) {
String sGender = "";
String sFav = "";
String sInfo = "";
if(rbMale.isChecked()){
sGender = rbMale.getText().toString();
}
if(rbFemale.isChecked()){
sGender = rbFemale.getText().toString();
}
if(cbFootBall.isChecked()){
sFav = sFav+cbFootBall.getText().toString();
}
if(cbBasketBall.isChecked()){
sFav = sFav+cbBasketBall.getText().toString();
}
   sInfo = "性別:"+sGender+"   愛好:"+sFav;
Toast.makeText(getApplicationContext(), sInfo, Toast.LENGTH_SHORT).show();
}

}
}

在以上代碼中,我們著重分析一下帶有綠色部分,其它是最簡單的基礎(chǔ)代碼,如果不明白,請參考上一章內(nèi)容。

1、第①部分

導(dǎo)入與RadioButton、CheckBox相關(guān)的2個包。

2、第②部分

聲明5個控件變量。

3、第③部分

與上一章類同。

(1)findViewById()方法完成5個控件的捕獲。

(2)“保存”按鈕添加單擊監(jiān)聽事件:btnSave.setOnClickListener(new SaveOnClickListener())。

4、第④部分

我們新建一個類SaveOnClickListener繼承接口OnClickListener用以實現(xiàn)單擊事件監(jiān)聽。

Toast.makeText(getApplicationContext(), sInfo,Toast.LENGTH_SHORT).show()用以顯示提示信息:性別與愛好。

注意:isChecked()方法用來判斷RadioButton和CheckBox控件是否被選中,如果選中返回true,否則返回flase。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站名稱:單選按鈕RadioGroup與復(fù)選框CheckBox-創(chuàng)新互聯(lián)
標(biāo)題來源:http://muchs.cn/article0/egoio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站維護、微信小程序、網(wǎng)站排名電子商務(wù)、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站