五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(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)的評論 (共 條)

分享到微博請遵守國家法律
梅州市| 灵宝市| 马鞍山市| 景德镇市| 新干县| 婺源县| 祁门县| 牡丹江市| 开鲁县| 留坝县| 邛崃市| 平江县| 上饶县| 舒兰市| 清原| 宾阳县| 永定县| 额尔古纳市| 清新县| 绍兴县| 疏勒县| 黄梅县| 满城县| 虞城县| 蒲江县| 抚远县| 西宁市| 顺昌县| 平安县| 含山县| 文化| 洛宁县| 黄大仙区| 泗水县| 靖远县| 涡阳县| 郓城县| 宣威市| 务川| 克什克腾旗| 沐川县|