我想设置我的 setOnShowListener() 方法来防止对话框关闭,所以我写了这样的东西,
public Dialog update_pop_up()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(UgJadwal.this);
LayoutInflater inflater = UgJadwal.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.updatepopup, null);
builder.setView(dialogView);
builder.setIcon(R.drawable.update);
builder.setTitle("Update schedule");
builder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
}
});
builder.setNeutralButton(R.string.retrieve, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
}
});
builder.setNegativeButton(R.string.cancel, null);
return builder.create();
builder.setOnShowListener(new DialogInterface.OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
Button retrieve = builder.getButton(AlertDialog.BUTTON_NEUTRAL);
retrieve.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//do nothing;
}
});
}
});
}
至少有两个由 Eclipse 提示的错误,第一个是在上面setOnShowListener
说“方法 setOnShowListener(new DialogInterface.OnShowListener(){}) 未定义类型 AlertDialog.Builder”,另一个在上面builder.getButton(AlertDialog.BUTTON_NEUTRAL)
说“方法 getButton(int) 未为 AlertDialog.Builder 类型定义”。我只是想知道为什么它会引发错误,因为我认为我做得对。请问大家有什么办法解决吗,谢谢。
[更新!!] 感谢大家的帮助。这是我最后的方法setOnShowListener()
dialog.setOnShowListener(new DialogInterface.OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
Button retrieve = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
retrieve.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
datasource.opentoread();
String[] hari = datasource.fetchDaily(ids.getText().toString());
datasource.close();
day.setText(String.valueOf(hari[0]));
subject.setText(String.valueOf(hari[1]));
time.setText(String.valueOf(hari[2]));
location.setText(String.valueOf(hari[3]));
lecture.setText(String.valueOf(hari[4]));
Toast.makeText(getApplicationContext(), "Retrieve success", Toast.LENGTH_LONG).show();
}
});
}
});
return dialog;