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

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

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

2018-11-04 14:41 作者:swiss126  | 我要投稿

參考資料:

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

參考軟件:

Android Studio、Eclipse+ADT、Android SDK、JDK

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

六、CheckBox

CheckBox復(fù)選按鈕是一種有雙狀態(tài)按鈕的特殊類型,可以選中或者不選中。可以現(xiàn)在布局文件中定義多選按鈕,然后對每一個多選按鈕進行事件監(jiān)setOnCheckedChangeListener,通過isChecked來判斷選項是否被選中

?下面是具體的例子:

?MainActivity.java

2.

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

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

5.??import?android.widget.CheckBox;

6.??import?android.widget.CompoundButton;

7.??import?android.widget.Toast;

8.??import?android.widget.CompoundButton.OnCheckedChangeListener;

9.

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

11.????//聲明復(fù)選按鈕

12.????private?CheckBox?cBox1;

13.????private?CheckBox?cBox2;

14.????private?CheckBox?cBox3;

15.

16.????@Override

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

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

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

20.????????//通過findViewById獲得CheckBox對象

21.????????cBox1=(CheckBox)findViewById(R.id.checkbox1);

22.????????cBox2=(CheckBox)findViewById(R.id.checkbox2);

23.????????cBox3=(CheckBox)findViewById(R.id.checkbox3);

24.

25.????????//注冊事件監(jiān)聽器

26.????????cBox1.setOnCheckedChangeListener(listener);

27.????????cBox2.setOnCheckedChangeListener(listener);

28.????????cBox3.setOnCheckedChangeListener(listener);

29.

30.????}

31.????//響應(yīng)事件

32.????private?OnCheckedChangeListener?listener?=?new?OnCheckedChangeListener(){

33.????????@Override

34.????????public?void?onCheckedChanged(CompoundButton?buttonView,?boolean?isChecked)

35.????????{

36.????????????//cBox1被選中

37.????????????if?(buttonView.getId()==R.id.checkbox1){

38.????????????????if?(isChecked){

39.????????????????????Toast.makeText(MainActivity.this,?"你喜歡足球",?Toast.LENGTH_LONG).show();

40.????????????????}

41.????????????}

42.????????????//cBox2被選中

43.????????????else?if?(buttonView.getId()==R.id.checkbox2){

44.????????????????if?(isChecked){

45.????????????????????Toast.makeText(MainActivity.this,?"你喜歡籃球",?Toast.LENGTH_LONG).show();

46.????????????????}

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

48.????????????//cBox3被選中

49.????????????else?if?(buttonView.getId()==R.id.checkbox3){

50.????????????????if?(isChecked){

51.????????????????????Toast.makeText(MainActivity.this,?"你喜歡排球",?Toast.LENGTH_LONG).show();

52.????????????????}

53.????????????}

54.????????}

55.????};

56.}

main.xml

2.??<LinearLayoutxmlns: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/hello"

11.????????android:textSize="20sp"

12.????????android:textStyle="bold"

13.????????android:textColor="#FFFFFF"

14.????????/>

15.????<CheckBox

16.????????android:id="@+id/checkbox1"

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

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

19.????????android:text="@string/football"

20.????????android:textSize="16sp"

21.????????/>

22.????<CheckBox

23.????????android:id="@+id/checkbox2"

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

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

26.????????android:text="@string/basketball"

27.????????android:textSize="16sp"

28.????????/>

29.????<CheckBox

30.????????android:id="@+id/checkbox3"

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

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

33.????????android:text="@string/volleyball"

34.????????android:textSize="16sp"

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

36.</LinearLayout>

strings.xml

2.??<resources>

3.??????<stringname="hello">你喜歡的運動是</string>

4.??????<stringname="app_name">復(fù)選按鈕測試</string>

5.??????<stringname="football">足球</string>

6.??????<stringname="basketball">籃球</string>

7.??????<stringname="volleyball">排球</string>

8.??</resources>

效果圖:

?



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

分享到微博請遵守國家法律
阆中市| 永年县| 建昌县| 湖州市| 开化县| 建宁县| 永康市| 闵行区| 麻城市| 六枝特区| 香格里拉县| 青龙| 共和县| 合水县| 昌宁县| 英山县| 沾益县| 黄冈市| 陕西省| 道真| 宣汉县| 印江| 天峨县| 大英县| 宜良县| 龙南县| 乳山市| 襄樊市| 通城县| 聊城市| 浦城县| 罗源县| 如皋市| 双城市| 阿荣旗| 长沙市| 岳阳县| 日照市| 长兴县| 朝阳市| 靖安县|