【问题标题】:How to detect click events in dynamically allocated radiobuttons?如何检测动态分配的单选按钮中的点击事件?
【发布时间】:2017-09-15 20:03:58
【问题描述】:

我目前正在开发一个应用程序,供学生查看他们的成绩、通知、出勤率等。教师也可以从这个应用程序中获取出勤率。 我已经完成了从网上获取学生信息的部分。

由于学生人数是可变的,因此我使我的内容动态化。

for (int i = 0; i < count; i++) {
            TableRow row = new TableRow(this);
            row.setId(1000 + i);
            //set the color only for the fields in odd places
            if (i % 2 != 0) {
                row.setBackgroundColor(getResources().getColor(R.color.viewSplit));
            }
            row.setGravity(Gravity.CENTER);

            //settting height of each row
            int tt = getResources().getDimensionPixelSize(R.dimen.height);
            row.setMinimumHeight(tt);
            // part1
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(lp);

            //adding data items in textviews
            int dp = getResources().getDimensionPixelSize(R.dimen.student_name);
            TextView textview1 = new TextView(this);
            textview1.setWidth(dp);
            textview1.setTextColor(Color.BLACK);
            textview1.setGravity(Gravity.CENTER);
            textview1.setText(name_of_student.get(i));

            TextView textview2 = new TextView(this);
            textview2.setTextColor(Color.BLACK);
            dp = getResources().getDimensionPixelSize(R.dimen.roll_no);
            textview2.setWidth(dp);
            textview2.setGravity(Gravity.CENTER);
            textview2.setText(roll_no.get(i));

            //if the student is present color is green
            RadioButton radioButtonPresent = new RadioButton(this);
            radioButtonPresent.setHighlightColor(Color.GREEN);
            dp = getResources().getDimensionPixelSize(R.dimen.present_abscent);
            radioButtonPresent.setWidth(dp);

            //if the student is absentcolor is red
            RadioButton radioButtonAbscent = new RadioButton(this);
            radioButtonAbscent.setHighlightColor(Color.GREEN);
            dp = getResources().getDimensionPixelSize(R.dimen.present_abscent);
            radioButtonAbscent.setWidth(dp);

            row.addView(textview1);
            row.addView(textview2);
            row.addView(radioButtonPresent);
            row.addView(radioButtonAbscent);

            tabLayout.addView(row, i);

所以我制作了两个文本视图来显示学生的姓名和卷号。并制作了两个单选按钮来选择学生是否缺席或在场。

我可以检测行中的点击事件,因为我为每一行设置了 ID。 但我遇到的问题是如何检测用户是否按下了当前单选按钮或不存在单选按钮。以及如何获取每个表的哪些字段已被点击。

替代解决方案也会有所帮助

【问题讨论】:

  • 我可以检测到单击了哪个表格行,因为我已为其设置了 id。但我需要知道该表行中的哪个字段被点击了存在或不存在的单选按钮

标签: android


【解决方案1】:

添加行时,使用Map 将学生及其 ID 链接到行的 ID。然后在每个单选按钮上定义以下内容:

rBtnAbsent.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // get the element from the map by the id of the row which will fetch the student as well and do the absent function
        }
    });

rBtnPresent.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // get the element from the map by the id of the row which will fetch the student as well and do the present function
        }
    });

【讨论】:

  • 谢谢。真的很有帮助
  • 很高兴为您提供帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2010-11-28
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多