Android: Alert Dialog
Help....I have a problem...Here is the cod...
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case IDD_COLOR:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
ListView listView = new ListView (this);
builder.setInverseBackgroundForced(true);
listView.setAdapter(new ArrayAdapter(this, android.R.layout.select_dialog_multichoice, mColors ));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
switch (arg2) {
case 0:
if (!((CheckedTextView)arg0.getChildAt(arg2)).isChecked())
{
((CheckedTextView)arg0.getChildAt(arg2)).setChecked(true);
} else
((CheckedTextView)arg0.getChildAt(arg2)).setChecked(false);
break;
case 1:
开发者_Go百科 if (!((CheckedTextView)arg0.getChildAt(arg2)).isChecked())
{
((CheckedTextView)arg0.getChildAt(arg2)).setChecked(true);
} else
((CheckedTextView)arg0.getChildAt(arg2)).setChecked(false);
break;
case 2:
if (!((CheckedTextView)arg0.getChildAt(arg2)).isChecked())
{
((CheckedTextView)arg0.getChildAt(arg2)).setChecked(true);
} else
((CheckedTextView)arg0.getChildAt(arg2)).setChecked(false);
break;
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setView(listView);
return builder.create();
default:
return null;
}
How to cancel what was clicked by pressing Cancel Button ? Because It all the time save what was clicked.
Jony,
Would this work in your case?
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
((CheckedTextView)arg0.getChildAt(arg2)).setChecked(false)
dialog.cancel();
}
});
I haven't tested it, but it seems if you simply uncheck the box before calling dialog.cancel();
it shouldn't come up checked.
精彩评论