单击 DialogFragment 正按钮后如何执行操作 [英] How to execute action after DialogFragment positive button clicked
问题描述
我从 Android 文档中创建了以下 DialogFragment:
I created the following DialogFragment deriving it from the Android documentation:
公共类 PayBillDialogFragment 扩展了 DialogFragment{
public class PayBillDialogFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Bundle b = this.getArguments();
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Paga bollettino")
.setPositiveButton("Paga", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
从另一个片段(一个 ListFragment),当单击列表的一行时,应该打开 DialogFragment,按下 DialogFragment 的正按钮后,我希望能够删除 ListFragment 的选定行并调用一种执行与删除相关的远程操作的方法.我按如下方式实现了 ListFragment:
From another Fragment (a ListFragment), when a row of the list is clicked the DialogFragment should be opened and after pressing the positive button of the DialogFragment I want to be able to remove the selected row of the ListFragment and also to call a method to perform a remote action associated to the removal. I implemented the ListFragment as follows:
public static class ListFragment extends android.support.v4.app.ListFragment {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.list_fragment_view,
container, false);
ListView lv = (ListView)rootView.findViewById(android.R.id.list);
}});
adapter=new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
return rootView;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//opening the dialogfragment
}
}
}
不知道是如何处理DialogFragment的正按钮点击后的action.你能帮助我吗?
What I don't know is how to handle the action after the click of the positive button of the DialogFragment. Can you help me?
澄清一下,这是工作流程:单击列表 -> 显示 DialogFragment -> 单击 DialogFragment 后从列表中删除元素.
to clarify, this is the workflow: click on the list -> show the DialogFragment -> after click on DialogFragment remove the element from the list.
推荐答案
这就是我处理片段和对话框片段之间通信的方式
This is how I handle communication between fragment and dialog fragment
示例片段:
public class MainFragment extends Fragment {
private static final int REQ_CODE = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main_fragment, container, false);
Button b = (Button) v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(MainFragment.this, REQ_CODE);
dialog.show(getFragmentManager(), "dialog");
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), "Result: " + resultCode,
Toast.LENGTH_SHORT).show();
super.onActivityResult(requestCode, resultCode, data);
}
}
示例对话框片段:
public class MyDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("My dialog message")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_OK);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_CANCELED);
}
});
return builder.create();
}
private void notifyToTarget(int code) {
Fragment targetFragment = getTargetFragment();
if (targetFragment != null) {
targetFragment.onActivityResult(getTargetRequestCode(), code, null);
}
}
}
这是我在改变方向时也能使用的唯一方法.
This is the only method I got working also when changing orientation.
这篇关于单击 DialogFragment 正按钮后如何执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!