gpt4 book ai didi

java - 无法在自定义对话框中实现 setSingleChoiceItems

转载 作者:行者123 更新时间:2023-12-02 07:44:19 29 4
gpt4 key购买 nike

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

代码如下:

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 中分配“d”的值

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;

关于java - 无法在自定义对话框中实现 setSingleChoiceItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105498/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com