CheckBox 是 Android 中常用的一个控件,主要是用来收集一些真、假的判断信息。
处理CheckBox的事件
在示例中,我们首先建立一个 Activity, 然后在屏幕上放置一个 CheckBox 控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CheckBoxActivity">
<CheckBox android:id="@+id/chkIsAdmin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" android:layout_marginTop="32dp" android:layout_marginEnd="32dp" android:layout_marginRight="32dp" android:text="isAdmin" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
屏幕建立好以后,在对应的 Activity 代码中加入事件处理方法。处理用户与 CheckBox 的交互,可以使用两个事件,onClick 和 onCheckedChanged。 我们先演示 onClick, 代码如下:
1 2 3
| @OnClick(R.id.chkIsAdmin) void onClick() { Log.d(TAG, "[onClick] is Admin = " + chkIsAdmin.isChecked()); }
|
在这里,我们使用 ButterKnife 来简化代码(如果不熟悉 butterknife 的用法,可以参考 在Android中使用注解绑定控件和事件 )
如果使用 onCheckedChanged 事件,则代码变为:
1 2 3 4
| @OnCheckedChanged({R.id.chkIsAdmin, R.id.chkIsPartner}) void onCheckedChanged(CompoundButton buttonView, boolean checked) {
Log.d(TAG, "[onCheckedChanged] isAdmin = " + checked); }
|
处理多个CheckBox
如果在一个界面上有多个 CheckBox, 就需要在事件中判断是那个 CheckBox 控件被按下了。示例如下:
首先在上面的界面中再加入一个 CheckBox,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CheckBoxActivity">
<CheckBox android:id="@+id/chkIsAdmin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" android:layout_marginTop="32dp" android:layout_marginEnd="32dp" android:layout_marginRight="32dp" android:text="isAdmin" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<CheckBox android:id="@+id/chkIsPartner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="104dp" android:text="is Partner" app:layout_constraintEnd_toEndOf="@+id/chkIsAdmin" app:layout_constraintHorizontal_bias="0.583" app:layout_constraintStart_toStartOf="@+id/chkIsAdmin" app:layout_constraintTop_toBottomOf="@+id/chkIsAdmin" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
修改 onCheckedChanged 事件的代码如下:
1 2 3 4 5 6 7 8 9 10 11
| @OnCheckedChanged({R.id.chkIsAdmin, R.id.chkIsPartner}) void onCheckedChanged(CompoundButton buttonView, boolean checked) {
switch(buttonView.getId()) { case R.id.chkIsAdmin: Log.d(TAG, "[onCheckedChanged] isAdmin = " + checked); break; case R.id.chkIsPartner: Log.d(TAG, "[onCheckedChanged] isPartner = " + checked); break; } }
|
可以看到,@OnCheckedChanged 注解中加入了多个 id 作为参数,可以使用 getId 方法获取控件的 id, 以此来区别是那个控件被按下。
实际上 onClick 事件也有一个参数
1 2 3
| @OnClick(R.id.chkIsAdmin) void onClick(View view) { Log.d(TAG, "[onClick] is Admin = " + chkIsAdmin.isChecked()); }
|
通过 view 变量也可以识别那个按钮被按下
最后,给出完整的类实现代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class CheckBoxActivity extends AppCompatActivity {
private static final String TAG = CheckBoxActivity.class.getSimpleName();
@BindView(R.id.chkIsAdmin) CheckBox chkIsAdmin;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_check_box);
ButterKnife.bind(this); }
@OnClick(R.id.chkIsAdmin) void onClick(View view) { Log.d(TAG, "[onClick] is Admin = " + chkIsAdmin.isChecked()); }
@OnCheckedChanged({R.id.chkIsAdmin, R.id.chkIsPartner}) void onCheckedChanged(CompoundButton buttonView, boolean checked) {
switch(buttonView.getId()) { case R.id.chkIsAdmin: Log.d(TAG, "[onCheckedChanged] isAdmin = " + checked); break; case R.id.chkIsPartner: Log.d(TAG, "[onCheckedChanged] isPartner = " + checked); break; } } }
|