0

下面代码的输出是一个图像按钮,当我们单击图像按钮时,会显示一个带有两个单选按钮的警报对话框,用于在 2 个不同的轨道之间进行选择。一切正常,但是当我再次单击图像按钮(即第二次)时,单选按钮不会显示当前播放的曲目,即先前选择的曲目。以前选择的项目不会被记住。?

public class MainActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button) findViewById(R.id.button1);
    final CharSequence[] items = {"Track #1", "Track #2"};

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a number");
builder.setIcon(R.drawable.ic_launcher);
builder.setNeutralButton("OK",
    new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog
            // closed
            Toast.makeText(getApplicationContext(), "You clicked on OK",
                    Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }
    });
    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    // TODO Auto-generated method stub
                    switch (item) {
                    case 0:
                        Toast.makeText(getApplicationContext(),
                                items[item], Toast.LENGTH_SHORT).show();
                        // Your code when first option seletced
                        break;
                    case 1:
                        // Your code when 2nd option seletced
                        Toast.makeText(getApplicationContext(),
                                items[item], Toast.LENGTH_SHORT).show();
                        break;

                    }
                    dialog.cancel();
                }
            });
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}
}
4

2 回答 2

1

当用户更改它时保存选择的索引。将此传递给builder.setSingleChoiceItems()

public class MainActivity extends Activity {
    int currentChoice = -1;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button) findViewById(R.id.button1);
        final CharSequence[] items = {"Track #1", "Track #2"};

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a number");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setNeutralButton("OK",
            new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                // closed
                Toast.makeText(getApplicationContext(), "You clicked on OK",
                Toast.LENGTH_SHORT).show();
                dialog.cancel();
            }
        });

    builder.setSingleChoiceItems(items, currentChoice,
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                // TODO Auto-generated method stub
                switch (item) {
                case 0:
                    Toast.makeText(getApplicationContext(),
                            items[item], Toast.LENGTH_SHORT).show();
                    // Your code when first option seletced
                    currentChoice = 0;
                    break;
                case 1:
                    // Your code when 2nd option seletced
                    Toast.makeText(getApplicationContext(),
                            items[item], Toast.LENGTH_SHORT).show();
                    currentChoice = 1;
                    break;

                }
                dialog.cancel();
            }
        });
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}

}

于 2013-08-01T18:32:27.360 回答
0

你只需要取一个名为的新变量

public int mSelectedValue = -1;

并将其传递给

builder.setSingleChoiceItems(items, mSelectedValue, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialmSelectedValue og, int item) {
                switch (item) {
                case 0:
                    mSelectedValue = 0;
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    mSelectedValue = 1;
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    break;
                }
                dialog.cancel();
            }
        });
于 2013-08-01T18:31:37.797 回答