Young87

当前位置:首页 >个人收藏

android分页中的checkbox如何控制,复选框  |  Android 开发者  |  Android Developers

复选框可让用户从一系列选项中选择一个或多个选项。通常,您应以垂直列表形式显示每个复选框选项。

d6728b28d108bb29836c7903d731bd97.png

要创建各个复选框选项,请在布局中创建一个

关键类如下:

响应点击事件

当用户选中复选框时,

要为复选框定义点击事件处理脚本,请将 元素。此属性的值必须是为了响应点击事件而调用的方法的名称。随后,托管布局的

例如,以下是采用列表形式的几个

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/meat"

android:onClick="onCheckboxClicked"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/cheese"

android:onClick="onCheckboxClicked"/>

在托管此布局的

Kotlin

fun onCheckboxClicked(view: View) {

if (view is CheckBox) {

val checked: Boolean = view.isChecked

when (view.id) {

R.id.checkbox_meat -> {

if (checked) {

// Put some meat on the sandwich

} else {

// Remove the meat

}

}

R.id.checkbox_cheese -> {

if (checked) {

// Cheese me

} else {

// I'm lactose intolerant

}

}

// TODO: Veggie sandwich

}

}

}Java

public void onCheckboxClicked(View view) {

// Is the view now checked?

boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked

switch(view.getId()) {

case R.id.checkbox_meat:

if (checked)

// Put some meat on the sandwich

else

// Remove the meat

break;

case R.id.checkbox_cheese:

if (checked)

// Cheese me

else

// I'm lactose intolerant

break;

// TODO: Veggie sandwich

}

}

您在

公开

返回 void

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: Python自动给抖音小姐姐视频点赞

下一篇: linux ptrace,Linux ptrace 简介

精华推荐