0
final CharSequence[] items = {"Red", "Green", "Blue"}

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Pick a color");

builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int item) {

        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});

AlertDialog alert = builder.create();

当我从(红色,绿色,蓝色)中选择一个值时,它应该删除它的方式。请帮忙。

4

3 回答 3

2
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int item) {  
    //Here you gets dialog as argument

        dialog.dismiss(); <---------
    }
});
于 2012-05-09T11:10:01.657 回答
0

试试这个,

final CharSequence[] items = {"Red", "Green", "Blue"}   
final AlertDialog alert = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);  
builder.setTitle("Pick a color");    
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int item) {

        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
     alert.dismiss();
    }
}); 
 alert = builder.create();
 alert.show();
于 2012-05-09T11:10:15.127 回答
0

使用下面的代码它将起作用。

public class TestingActivity extends Activity {

    AlertDialog alert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final CharSequence[] items = { "Red", "Green", "Blue" };

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Pick a color");



        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int item) {

                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                        dismiss();
            }

        });
        alert = builder.create();

        alert.show();
    }

    private void dismiss() {
        alert.dismiss();

    }
    }
于 2012-05-09T11:29:58.143 回答