我动态添加了3个复选框。
问题在于,当我选中复选框1和2时,会引发“未选中任何复选框”吐司,但是当我选中复选框3时,它将引发“已选中复选框3”吐司。
那么,当我选中复选框3时,如何使复选框1和2带来相同的吐司?
这是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>
答案 0 :(得分:0)
将您的听众更改为此:
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
仅保存了您创建的最后一个复选框。