上次我们介绍了两种常用的UI控件,分别是EditText和RadioButton,不清楚用法的可以查看我的上一次博客,链接为https://blog.csdn.net/chenpeixing361/article/details/89054256。这一次我们我们继续介绍两种常用的控件,分别是CheckBox和ImageView。
CheckBox
checkbox也就是复选框的意思,我们选择一项或者多项标签。这里我们主要讲解以下内容:①常用属性;②自定义样式;③监听事件。我们紧接着之前的博客内容进行拓展,在activity_main.xml中添加两个按钮,代码如下:
<Button
android:id="@+id/btn_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/check_box"
android:textAllCaps="false"/>
<Button
android:id="@+id/btn_image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/image_view"
android:textAllCaps="false"/>
我们这里把介绍ImageView的按钮也顺便添加进去,然后我们新建两个Activity类,类名分别是CheckBoxActivity和ImageViewActivity。我们在MainActivity中,设置两个界面跳转事件,代码如下:
Button btnCheckBox = findViewById(R.id.btn_checkbox);
btnCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳转到CheckBox界面
Intent intent = new Intent(MainActivity.this,CheckBoxActivity.class);
startActivity(intent);
}
});
Button btnImageView = findViewById(R.id.btn_image_view);
btnImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳转到ImageView界面
Intent intent = new Intent(MainActivity.this,ImageViewActivity.class);
startActivity(intent);
}
});
我们在创建CheckBoxActivity类时,会附带创建一个activity_check_box.xml文件,该文件即代表该界面的布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你会哪些移动开发:"
android:textColor="#000"
android:textSize="20sp"
android:layout_marginBottom="10dp"/>
<CheckBox
android:id="@+id/cb_1