如何获取复选框导致的Andr​​oid [英] How to get checkboxes result in Android

查看:112
本文介绍了如何获取复选框导致的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建编程基础上,没有复选框。数据库中的任务

i created checkboxes programmatically based on the no. of tasks in the database

例如:3个任务,3复选框

for example: 3 tasks , 3 checkboxes

布局

没有。任务:3

这将创建这样的:

任务1 []

任务2 []

任务3 []

[提交]

例如:5个任务,5复选框

for example: 5 tasks , 5 checkboxes

布局

没有。任务:5

这将创建这样的:

任务1 []

任务2 []

任务3 []

任务4 []

任务5 []

[提交]

我要的是,当用户检查并提交,我想知道哪些任务被选中。如果在任务3和任务4检查,然后再提交一样,显示的信息是任务3和任务4进行检查。

What i want is that when the user checks and submit, i want to know which tasks is checked. Like if task 3, and task 4 are checked and then submitted, the display message is task 3 and task 4 are checked.

任务1,任务2和任务3检查,然后再提交,显示的信息是任务1,任务2和任务3进行检查。那怎么办?

OR task 1, task 2 and task 3 are checked and then submitted, the display message is task 1, task 2 and task 3 are checked. How to do that?

更新:
OnClick方法

UPDATED: Onclick method

cb.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                      if(cb.isChecked()){
                            list.add(task.getTaskId());
                            i++;
                         }

                    }

}); 

该数组为空。我不知道为什么。我把这个情况cb.isChecked(),这样只有检查的项目包括在数组中。

The array is empty. I dont know why. I put this condition cb.isChecked() so that only checked items is included in array.

推荐答案

如果复选框具有不同的ID,您可以使用该ID来确定所选择之一。否则,你可以给复选框的标记,使用 setTag()方法。点击之后就可以调用 checkbox.getTag(),并检查它是哪一个。

If the checkboxes have different ids you can use the id to determine the selected one. Otherwise you can give the checkbox a tag, using setTag() method. After the click you can call checkbox.getTag() and check which one it is.

在评论更新:

如果您通过$ C $创建它们c可以给他们生成的ID。添加资源ID这样每个复选框:

If you are creating them by code you can give them generated ids. Add a resource id for each checkbox like this:

<item type="id" name="checkbox_task_1" />

或者其他方法,给他们的标签。所以,当你创建的复选​​框,code是这样的:

Or the other approach, give them a tag. So when you create the checkbox code something like this:

checkbox.setTag("checkBox1");

您可以使用任何你想要的对象,例如一个整数。而在你的onClick方法,你应该有这样的事情:

You can use whatever object you want, e.g. an integer. And in your onClick method you should have something like this:

String tag = (String) checkbox.getTag();
if ("checkBox1".equals(tag)) {...}

第二次更新:

有关创建复选框的:

for (int i = 0; i < userTasks.size(); i++) {
  ...
  checkBox.setTag(i);
}

在您的onClick方法:

In your onClick method:

int tag = (Integer) checkbox.getTag();
if (tag == 1) {...}

下次更新 - 的onClick

NEXT UPDATE - onClick

public void onClick(View v) {
  CheckBox cb = (CheckBox) v;
  if(cb.isChecked()) {
    list.add((Integer) cb.getTag());
  }
}

这篇关于如何获取复选框导致的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆