0

我想在自定义对话框中实现 setSingleChoiceItems 但无法做到。我希望当我按下一个按钮时会弹出一个带有 setSingleChoiceItems 的自定义警报框,然后它将转到另一个活动。

下面的代码:

package com.ssaa;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class VoteActivity extends Activity {
    /** Called when the activity is first created. */

    Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b=(Button) findViewById(R.id.vote);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(1);
            }
        });
    }


    public void showallDialog()
    {
        final CharSequence[] items = {"Yes", "No", "Can't Say"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);


        builder.setTitle("AM I AWESOME?");
        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();
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        Dialog d =null;
        switch (id) {
        case 1:

            showallDialog();


        default:
            break;
        }
        return d;
    }

  /*  class MyCustomDialogue extends Dialog
    {

        public MyCustomDialogue(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

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


        }
    }*/

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}

我认为我在方法上做错了showallDialog()。请帮我纠正它。

4

1 回答 1

2

您目前正在尝试显示一个空对话框,因为您从未在onCreateDialog

showallDialog 应该返回您创建的对话框。前任:

     public AlertDialog showallDialog()
    {
        final CharSequence[] items = {"Yes", "No", "Can't Say"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);


        builder.setTitle("AM I AWESOME?");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item)
            {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                //create your intent here
                Intent intent = new Intent(getApplicationContext(), YourNextActivity.class);
                startActivity(intent);
            }
        });
        return builder.create();
    }

然后,当您在其中创建对话框时,onCreateDialog将“d”分配给您的 AlertDialog

case 1:
   d = showallDialog();
   break;
于 2012-06-19T16:50:54.157 回答