首页 > 解决方案 > Multiple checkbox state

问题描述

I dynamically added 3 checkboxes.
The problem is that when I check checkboxes 1 and 2, it raises "No CheckBox Checked" toast, but when I check checkbox 3, it raises "Checkbox 3 is checked" toast.
So how to make checkboxes 1 and 2 bring up the same toast when I check checkbox 3?

This is the full code of the MainActivity:

public class MainActivity extends Activity {

EditText text;
TextView tx, jText;
CheckBox noCheck, jCheck;
LinearLayout l1;
int checkId = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    text = (EditText)findViewById(R.id.edit);
    l1 = (LinearLayout)findViewById(R.id.CheckBoxLayout);
    noCheck = (CheckBox)findViewById(R.id.noCheck);

}

public void fTambah(View view) {

    /**
     * Tambah.onClick function
     * To add checkbox dynamically
     * @param textData - EditText text data
     * @param jCheck - New CheckBox
     * @param jText - New TextView
     */

    checkId++;
    String textData = text.getText().toString();
    jCheck = new CheckBox(this);
    jText = new TextView(this);

    if (noCheck.isChecked()) {
        jText.setText(textData);
        l1.addView(jText);
    } else {
        jCheck.setTag(checkId);
        jCheck.setText(textData);
        jCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (jCheck.isChecked()) {
                    Toast.makeText(MainActivity.this, "CheckBox " + jCheck.getTag().toString() + " is checked", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "No Checkbox Checked", Toast.LENGTH_LONG).show();
                }
            }
        });

        // Add CheckBox to layout
        l1.addView(jCheck);

    }   
}
}

activity_main.xml

<!-- ScrollView CheckBox layout -->
<ScrollView
    android:id="@+id/scr"
    android:layout_width="fill_parent"
    android:layout_height="1400px"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

        <!-- LinearLayout CheckBox MainLayout -->
        <LinearLayout
            android:id="@+id/CheckBoxLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
</ScrollView>

<RelativeLayout
    android:id="@+id/bottomLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/scr" 
    android:isScrollContainer="true" >
        <Button
            android:id="@+id/tambah"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tambah"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:onClick="fTambah" />
        <Button
            android:id="@+id/space"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Space"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_below="@id/tambah" />
        <CheckBox
            android:id="@+id/noCheck"
            android:text="No CheckBox ?"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/tambah" />
        <EditText
            android:id="@+id/edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/space" />
</RelativeLayout>
</RelativeLayout>

标签: androidcheckbox

解决方案


将您的侦听器更改为:

    jCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;
            if (cb.isChecked()) {
                Toast.makeText(Main4Activity.this, "CheckBox " + cb.getTag().toString() + " is checked", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(Main4Activity.this, "No Checkbox Checked", Toast.LENGTH_LONG).show();
            }
        }
    });

您使用的变量jCheck仅保存了您创建的最后一个复选框。


推荐阅读