使用DialogFragment创建对话框总结
回调Activity中的函数
http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
在DialogFragment中,定义Interface,然后在onAttach中绑定,最后在每个按钮的回调函数中调用Interface中定义的函数
在Activity中,实现Interface中定义的函数
Interface中定义的函数参数可以任意指定,因此可以用来在从DialogFragment向Activity传递参数
http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
在DialogFragment中,直接在每个按钮的回调函数中调用Interface中定义的函数
在Activity中,实现Interface中定义的函数
Interface中定义的函数参数可以任意指定,因此可以用来在从DialogFragment向Activity传递参数
自定义Dialog界面,传递参数到Activity
使用OnCreateDialog()
第一种方法:
局部变量view需要声明为final
public class AllBonusDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); final View view = inflater.inflate(R.layout.input_dialog, null); builder.setView(view) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditText allBonusEdit = (EditText)view.findViewById(R.id.inputDialogEdit); String allBonusStr = allBonusEdit.getText().toString(); double allBonus = Double.parseDouble(allBonusStr); mListener.onDialogPositiveClick(allBonus); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { mListener.onDialogNegativeClick(); } }); return builder.create(); }
第二种方法:
把view声明为成员变量
public class AllBonusDialogFragment extends DialogFragment { View view = null; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); view = inflater.inflate(R.layout.input_dialog, null); builder.setView(view) .setPositiveButton("确定", new PositiveOnClickListener()) .setNegativeButton("取消", new NegativeOnClickListener()); return builder.create(); } class PositiveOnClickListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { EditText allBonusEdit = (EditText)view.findViewById(R.id.inputDialogEdit); String allBonusStr = allBonusEdit.getText().toString(); double allBonus = Double.parseDouble(allBonusStr); mListener.onDialogPositiveClick(allBonus); } } class NegativeOnClickListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { mListener.onDialogNegativeClick(); } }
使用OnCreateView()
《Pro Android 4》Chapter 9:Working With Dialogs
http://blog.csdn.net/flowingflying/article/details/13505409
家教呢:www.jiajiaone.com