最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

第五章 用戶界面基礎(chǔ)(RadioButton)

2018-11-03 21:51 作者:swiss126  | 我要投稿

參考資料:

Android應(yīng)用程序開發(fā)》ISBN 9787302283164

參考軟件:

Android Studio、Eclipse+ADT、Android SDK、JDK

用戶界面基礎(chǔ)(接上節(jié)內(nèi)容)

五、RadioButton

?RadioButton(單選按鈕)在Android開發(fā)中應(yīng)用的非常廣泛,比如一些選擇項(xiàng)的時候,會用到單選按鈕。它是一種單個圓形單選框雙狀態(tài)的按鈕,可以選擇或不選擇。在RadioButton沒有被選中時,用戶能夠按下或點(diǎn)擊來選中它。但是,與復(fù)選框相反,用戶一旦選中就不能夠取消選中。

???實(shí)現(xiàn)RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用.RadioGroup是單選組合框,可以容納多個RadioButton的容器.在沒有RadioGroup的情況下,RadioButton可以全部都選中;當(dāng)多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個。并用setOnCheckedChangeListener來對單選按鈕進(jìn)行監(jiān)聽

下面的具體的例子:

MainActivity.java

1.??package?com.android.radiobutton; ?

2.???

3.??import?android.app.Activity; ?

4.??import?android.os.Bundle; ?

5.??import?android.widget.RadioGroup; ?

6.??import?android.widget.Toast; ?

7.???

8.??public?class?MainActivity?extends?Activity?{ ?

9.?????? ?

10.????//聲明RadioGroup??

11.????RadioGroup?raGroup1,raGroup2;?

12.????@Override?

13.????public?void?onCreate(Bundle?savedInstanceState)?{?

14.????????super.onCreate(savedInstanceState); ?

15.????????setContentView(R.layout.main);?

16.?????????

17.????????//通過findViewById獲得RadioGroup對象?

18.????????raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);??????

19.????????//添加事件監(jiān)聽器?

20.????????raGroup1.setOnCheckedChangeListener(new?RadioGroup.OnCheckedChangeListener()?{?

21.?????????????

22.????????????@Override?

23.????????????public?void?onCheckedChanged(RadioGroup?group,?int?checkedId)?{ ?

24.????????????????//?TODO?Auto-generated?method?stub?

25.????????????????if(checkedId==R.id.radioBtn1){???????????????????

26.????????????????????Toast.makeText(MainActivity.this,?"你來自廣東省",?Toast.LENGTH_LONG).show(); ?

27.????????????????}?

28.????????????????else?if(checkedId==R.id.radioBtn2){ ?

29.????????????????????Toast.makeText(MainActivity.this,?"你來自廣西省",?Toast.LENGTH_LONG).show(); ?

30.????????????????}?

31.????????????????else{ ?

32.????????????????????Toast.makeText(MainActivity.this,?"你來自湖南省",?Toast.LENGTH_LONG).show(); ?

33.????????????????}?

34.????????????}?

35.????????});?

36.????????raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);??????

37.????????raGroup2.setOnCheckedChangeListener(new?RadioGroup.OnCheckedChangeListener()?{?

38.?????????????

39.????????????@Override?

40.????????????public?void?onCheckedChanged(RadioGroup?group,?int?checkedId)?{ ?

41.????????????????//?TODO?Auto-generated?method?stub?

42.????????????????if(checkedId==R.id.radioBtn4){???????????????????

43.????????????????????Toast.makeText(MainActivity.this,?"你的性別是男",?Toast.LENGTH_LONG).show(); ?

44.????????????????}?

45.????????????????else?{ ?

46.????????????????????Toast.makeText(MainActivity.this,?"你的性別是女",?Toast.LENGTH_LONG).show(); ?

47.????????????????}?

48.????????????}?

49.????????});????????

50.????} ?

51.}?

main.xml

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

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

3.??????android:orientation="vertical"?

4.??????android:layout_width="fill_parent"?

5.??????android:layout_height="fill_parent"?

6.??????>?

7.??????<TextView?? ?

8.??????????android:layout_width="fill_parent"? ?

9.??????????android:layout_height="wrap_content"? ?

10.????????android:text="@string/hello1"?

11.????????/>?

12.????????<RadioGroup?

13.????????????android:id="@+id/radioGroup1"?

14.????????????android:layout_width="wrap_content"?

15.????????????android:layout_height="wrap_content"?

16.????????????android:orientation="vertical"?

17.????????????>?

18.????????????<RadioButton?

19.????????????????android:id="@+id/radioBtn1"?

20.????????????????android:layout_width="wrap_content"?

21.????????????????android:layout_height="wrap_content"?

22.????????????????android:text="@string/radioBtn1"?

23.???????????????/>?

24.????????????<RadioButton?

25.????????????????android:id="@+id/radioBtn2"?

26.????????????????android:layout_width="wrap_content"?

27.????????????????android:layout_height="wrap_content"?

28.????????????????android:text="@string/radioBtn2"?

29.???????????????/>?

30.????????????<RadioButton?

31.????????????????android:id="@+id/radioBtn3"?

32.????????????????android:layout_width="wrap_content"?

33.????????????????android:layout_height="wrap_content"?

34.????????????????android:text="@string/radioBtn3"?

35.???????????????/>?

36.????????</RadioGroup>?

37.????<!--?在兩個RadioGroup之間畫條橫線?-->?

38.????<View? ?

39.????????android:layout_width="match_parent"? ?

40.????????android:layout_height="1dp"?

41.????????android:background="#ffffff"?

42.????????/>?

43.????<TextView?? ?

44.????????android:layout_width="fill_parent"? ?

45.????????android:layout_height="wrap_content"? ?

46.????????android:text="@string/hello2"?

47.????????/>?

48.????????<RadioGroup?

49.????????????android:id="@+id/radioGroup2"?

50.????????????android:layout_width="wrap_content"?

51.????????????android:layout_height="wrap_content"?

52.????????????android:orientation="vertical"?? ?

53.????????????>?

54.????????????<RadioButton?

55.????????????????android:id="@+id/radioBtn4"?

56.????????????????android:layout_width="wrap_content"?

57.????????????????android:layout_height="wrap_content"?

58.????????????????android:text="@string/radioBtn4"?

59.????????????????android:textColor="#ffffff"?

60.???????????????/>?

61.????????????<RadioButton?

62.????????????????android:id="@+id/radioBtn5"?

63.????????????????android:layout_width="wrap_content"?

64.????????????????android:layout_height="wrap_content"?

65.????????????????android:text="@string/radioBtn5"?

66.???????????????/>?

67.????????</RadioGroup>?

68.</LinearLayout>?

strings.xml

1.??<?xmlversion="1.0"encoding="utf-8"?>

2.??<resources>

3.??????<stringname="hello1">你來自哪個省</string>

4.??????<stringname="hello2">你的性別是</string>

5.??????<stringname="app_name">單選按鈕測試</string>

6.??????<stringname="radioBtn1">廣東</string>

7.??????<stringname="radioBtn2">廣西</string>

8.??????<stringname="radioBtn3">湖南</string>

9.??????<stringname="radioBtn4"></string>

10.????<stringname="radioBtn5"></string>

11.</resources>

效果圖:

RadioButton的另一種效果:

要實(shí)現(xiàn)上面的效果,只要在main.xml布局文件中的<RadioButton/>加入android:button="@null"????????????????????
android:drawableRight="@android:drawable/btn_radio"即可


第五章 用戶界面基礎(chǔ)(RadioButton)的評論 (共 條)

分享到微博請遵守國家法律
阿拉善盟| 扬中市| 谷城县| 青冈县| 图木舒克市| 高密市| 青铜峡市| 连平县| 依安县| 宣化县| 寿光市| 措勤县| 岫岩| 齐河县| 翁源县| 绍兴市| 邹城市| 康乐县| 凤山县| 贡觉县| 威宁| 长顺县| 前郭尔| 绥滨县| 潮安县| 从江县| 龙门县| 博爱县| 兴义市| 新巴尔虎左旗| 泾川县| 弥渡县| 湘西| 鸡东县| 北安市| 垫江县| 鄢陵县| 年辖:市辖区| 郑州市| 永嘉县| 应城市|