无法实现自定义对话框内setSingleChoiceItems [英] Unable to implement setSingleChoiceItems inside custom Dialog

查看:128
本文介绍了无法实现自定义对话框内setSingleChoiceItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个自定义对话框,但无法做到这里面setSingleChoiceItems。我想,当我preSS按钮自定义警告框会弹出setSingleChoiceItems,那么它会去到另一个活动。

I want to implement setSingleChoiceItems inside a custom Dialog but unable to do it. I want when I press a button a custom alert box will pop up with setSingleChoiceItems, then it will go to another activity.

code如下:

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()方法。请帮我纠正。

推荐答案

您目前正试图显示空对话框,因为你从来没有在 onCreateDialog

You currently are trying to show a null dialog since you never assign the value of "d" in onCreateDialog

showallDialog应该返回您创建对话框。
 例如:

showallDialog should return your dialog that you created. ex:

     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

Then when you create your dialog in onCreateDialog you assign "d" to your AlertDialog

case 1:
   d = showallDialog();
   break;

这篇关于无法实现自定义对话框内setSingleChoiceItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆