我有一个TableLayout,里面有TableRows和CheckBox以及TextView。当我按下CheckBox时,我需要知道TableRow中的CheckBox被放置以使TextView与此CheckBox“关联”。例如:
TableLayout:
我正在尝试这样的事情:
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cb.isChecked())
{
int id_cb = cb.getId();
boolean encontrado = false;
for (int i = 0; i < tabla_tareas.getChildCount() && !encontrado; i++)
{
TableRow aux_tr = (TableRow) tabla_tareas.getChildAt(i);
CheckBox aux_cb = (CheckBox) aux_tr.getChildAt(0);
if (id_cb == aux_cb.getId())
encontrado = true;
}...
但它不起作用。
答案 0 :(得分:1)
利用Elior所说的,你为什么不用下面的方法来达到你想要的目的:
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cb.isChecked())
{
cb.getText().toString(); //This will retrieve the text associated with the Checkbox
cb.setText("Your new string here"); //This will set the text associated with the CheckBox
}...
使用上述两种方法,您应该能够在不需要TextView
的情况下完成所需的一切,并且可能没有TableLayout
,因为我假设您正在使用它来对齐CheckBoxes
和TextViews
。