-1

我只是希望出现一个对话框,显示数组中的值。然后单击按钮,我想获取所选单选按钮的值并将其显示在吐司中。感谢您提前回答!非常感谢。

package com.example.moredialogs;

import java.lang.reflect.Array;
import java.util.ArrayList;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.widget.Toast;

public class Next extends Activity {

private Context mContext; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.next);

    mContext = this;


    AlertDialog.Builder builder = 
            new AlertDialog.Builder(mContext);
        builder.setTitle("Show dialog");

        final CharSequence[] choiceList = 
        {"Coke", "Pepsi" , "Sprite" , "Seven Up" };

        int selected = 0; 


        builder.setSingleChoiceItems(choiceList, selected, 
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub


                        // This is where I want the value to be selected


                    }


        });
        builder.setPositiveButton("Sounds Good", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK, so save the mSelectedItems results somewhere
                // or return them to the component that opened the dialog

               // On button click I want the selected Item to go in here
            }

        });
        builder.show();
}

 }
4

2 回答 2

0

首先,你需要制作selected成一个字段。然后

// This is where I want the value to be selected

应该变成:

selected = arg1;

然后在监听器中:

Toast.makeText(
    Next.this,
    "Selected " + choiceList[selected],
    Toast.LENGTH_SHORT)
.show();
于 2013-08-11T22:30:28.070 回答
0

首先 - 创建一个字段mSelected

private int mSelected;

在您的ChoiceListener设定值中mSelected

builder.setSingleChoiceItems(choiceList, selected, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        mSelected = arg1;
    }
});

在你ButtonClickListener只是显示一个Toast并解雇:

builder.setPositiveButton("Sounds Good", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        final CharSequence item = choiceList[mSelected];
        Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
        dialog.dismiss();
    }
});
于 2013-08-11T22:31:52.060 回答