【发布时间】:2014-06-04 07:08:54
【问题描述】:
我有一个显示联系人列表的 ListView。单击联系人时,会创建一个 DialogFragment,其中显示有关是否取消或确认的通知。根据他们是单击取消还是确认,我想更改 ListView 项的颜色。如何获取对话框的按钮单击状态。我的对话框代码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
String first = this.getArguments().getString("first");
String last = this.getArguments().getString("last");
String phone = this.getArguments().getString("phone");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.add_contact_dialog, null);
TextView tv = (TextView) view.findViewById(R.id.new_first);
tv.setText(first);
TextView tv2 = (TextView) view.findViewById(R.id.new_phone);
tv2.setText(phone);
TextView tv3 = (TextView) view.findViewById(R.id.new_last);
tv3.setText(last);
builder.setView(view);
//builder.setTitle("Are you sure?").setMessage("Would you like to add " + name + " as a contact?");
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Add the contact
}
})
.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
现在我希望 setPositiveButton 或 setNegativeButton 返回一个值,我可以在我的主活动中进行比较,或者让我的 ArrayAdapter 在对话框的范围内,以便我可以对其进行更改。有什么方法可以将DialogFragment.show() 和被点击的对话框按钮联系起来?
【问题讨论】:
标签: android android-fragments android-dialogfragment