String[] colors = {"red", "green", "blue", "black"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on colors[which]
}
});
builder.show();
final String[] Options = {"Red", "Blue"};
AlertDialog.Builder window;
将此代码放在将触发此
window = new AlertDialog.Builder(this);
window.setTitle("Pick a color");
window.setItems(Options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){
//first option clicked, do this...
}else if(which == 1){
//second option clicked, do this...
}else{
//theres an error in what was selected
Toast.makeText(getApplicationContext(), "Hmmm I messed up. I detected that you clicked on : " + which + "?", Toast.LENGTH_LONG).show();
}
}
});
window.show();