,如果你的內容畫面的LinearLayout,嘗試
final CheckBox mCheckBox = new CheckBox(this);
mCheckBox
.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isCheck) {
if (mCheckBox.isChecked()) {
mCheckBox.setText(System.currentTimeMillis() + "");
}
}
});
LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
addContentView(mCheckBox, params);
======編輯======
或試試這個
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
CheckBox[] mCheckBox = new CheckBox[6];
for (int i = 0; i < 6; i++) {
mCheckBox[i] = new CheckBox(this);
mCheckBox[i].setText(i + "");
mCheckBox[i]
.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isCheck) {
if (isCheck) {
Toast.makeText(MainActivity.this,
System.currentTimeMillis() + "",
Toast.LENGTH_SHORT).show();
}
}
});
layout.addView(mCheckBox[i]);
}
無法創建方法,執行上面的代碼時發生了什麼.. ..? – Pragnani 2013-04-11 15:30:32
你到底有什麼問題? – Karakuri 2013-04-11 15:30:45
在上面的代碼之後,你用'mCheckBox'做了什麼? – 2013-04-11 15:30:57