欢迎光临散文网 会员登陆 & 注册

第五章 用户界面基础(CheckBox)

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

参考资料:

Android应用程序开发》ISBN 9787302283164

参考软件:

Android Studio、Eclipse+ADT、Android SDK、JDK

用户界面基础(接上节内容)

六、CheckBox

CheckBox复选按钮是一种有双状态按钮的特殊类型,可以选中或者不选中。可以现在布局文件中定义多选按钮,然后对每一个多选按钮进行事件监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.    //声明复选按钮

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.        //注册事件监听器

26.        cBox1.setOnCheckedChangeListener(listener);

27.        cBox2.setOnCheckedChangeListener(listener);

28.        cBox3.setOnCheckedChangeListener(listener);

29.

30.    }

31.    //响应事件

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">复选按钮测试</string>

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

6.      <stringname="basketball">篮球</string>

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

8.  </resources>

效果图:

 



第五章 用户界面基础(CheckBox)的评论 (共 条)

分享到微博请遵守国家法律