我想在自定义Dialog中实现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()
方法中做错了。请帮我纠正。
答案 0 :(得分: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;