bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

單選按鈕RadioGroup與復選框CheckBox-創新互聯

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

成都創新互聯成都網站建設定制網站制作,是成都網站制作公司,為成都純水機提供網站建設服務,有成熟的網站定制合作流程,提供網站定制設計服務:原型圖制作、網站創意設計、前端HTML5制作、后臺程序開發等。成都網站改版熱線:028-86922220

一、設計登錄窗口

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

 1、分別從工具欄向activity拖出1個單選按鈕列表RadioGroup(注意自動包含3個單選按鈕RadioButton)、2個復選框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了,導致ClassCastException

   //而且模擬器出現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();
}

}
}

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

1、第①部分

導入與RadioButton、CheckBox相關的2個包。

2、第②部分

聲明5個控件變量。

3、第③部分

與上一章類同。

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

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

4、第④部分

我們新建一個類SaveOnClickListener繼承接口OnClickListener用以實現單擊事件監聽。

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

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

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

網站名稱:單選按鈕RadioGroup與復選框CheckBox-創新互聯
標題來源:http://vcdvsql.cn/article0/egoio.html

成都網站建設公司_創新互聯,為您提供動態網站網站維護微信小程序網站排名電子商務用戶體驗

廣告

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

成都做網站