代码如下:
private Dialog createMusicListDialog =null;
private View textEntryView ;
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case 1:
if (createMusicListDialog == null)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent()) ;
builder.setIcon(R.drawable.dialog);
builder.setTitle("添加列表");
builder.setView(getTextEntryView());
builder.setCancelable(true);
builder.setPositiveButton("确定", addMusicListButtonListener());
createMusicListDialog = builder.create();
}
return createMusicListDialog;
default:
Log.e("music","Attempting to create an unkonwn dialog with an id of " + id);
return null;
}
}
//对话框里面的view
synchronized protected View getTextEntryView()
{
if (textEntryView == null)
{
LayoutInflater factory = LayoutInflater.from(this);
textEntryView = factory.inflate(R.layout.musiclist_add, null);
}
return textEntryView;
}
//按钮处理函数
protected DialogInterface.OnClickListener addMusicListButtonListener()
{
return new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//这里 id 的值 是-1
System.out.println(id);
}
};
}
显示 对话框的代码是 showDialog(1);为什么在按钮的处理函数里面对话框的id是-1 呢,
private Dialog createMusicListDialog =null;
private View textEntryView ;
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case 1:
if (createMusicListDialog == null)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent()) ;
builder.setIcon(R.drawable.dialog);
builder.setTitle("添加列表");
builder.setView(getTextEntryView());
builder.setCancelable(true);
builder.setPositiveButton("确定", addMusicListButtonListener());
createMusicListDialog = builder.create();
}
return createMusicListDialog;
default:
Log.e("music","Attempting to create an unkonwn dialog with an id of " + id);
return null;
}
}
//对话框里面的view
synchronized protected View getTextEntryView()
{
if (textEntryView == null)
{
LayoutInflater factory = LayoutInflater.from(this);
textEntryView = factory.inflate(R.layout.musiclist_add, null);
}
return textEntryView;
}
//按钮处理函数
protected DialogInterface.OnClickListener addMusicListButtonListener()
{
return new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//这里 id 的值 是-1
System.out.println(id);
}
};
}
显示 对话框的代码是 showDialog(1);为什么在按钮的处理函数里面对话框的id是-1 呢,
setPositiveButton 的button id 为 -1
setNegativeButton 的button id 为 -2
setNeutralButton 的button id 为 -3