栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java Android中为自定义对话框创建通用类

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java Android中为自定义对话框创建通用类

首先创建一个base

DialogFragment
来保持的实例
Activity
。因此,当Dialog附加到时
Activity
,您将知道
Activity
创建该对话框的Instance


public abstract class baseDialogFragment<T> extends DialogFragment {        private T mActivityInstance;        public final T getActivityInstance() {     return mActivityInstance;        }        @Override        public void onAttach(Activity activity) {     mActivityInstance = (T) activity; super.onAttach(activity);        }        @Override        public void onDetach() {     super.onDetach();     mActivityInstance = null;        }}

然后,创建一个

GeneralDialogFragment
扩展
baseDialogFragment

public class GeneralDialogFragment extends baseDialogFragment<GeneralDialogFragment.OnDialogFragmentClickListener> {        // interface to handle the dialog click back to the Activity        public interface onDialogFragmentClickListener { public void onOkClicked(GeneralDialogFragment dialog); public void onCancelClicked(GeneralDialogFragment dialog);        }        // Create an instance of the Dialog with the input        public static GeneralDialogFragment newInstance(String title, String message) { GeneralDialogFragment frag = new GeneralDialogFragment(); Bundle args = new Bundle(); args.putString("title", title); args.putString("msg", message); frag.setArguments(args); return frag;        }        // Create a Dialog using default alertDialog builder , if not inflate custom view in onCreateView        @Override        public Dialog onCreateDialog(Bundle savedInstanceState) { return new alertDialog.Builder(getActivity())     .setTitle(getArguments().getString("title"))     .setMessage(getArguments().getString("message"))     .setCancelable(false)     .setPositiveButton("OK",         new DialogInterface.onClickListener() {  public void onClick(DialogInterface dialog, int whichButton) {      // Positive button clicked      getActivityInstance().onOkClicked(GeneralDialogFragment.this);  }         }     )     .setNegativeButton("Cancel",         new DialogInterface.onClickListener() {  public void onClick(DialogInterface dialog, int whichButton) {      // negative button clicked      getActivityInstance().onCancelClicked(GeneralDialogFragment.this);  }         }     )     .create();        }    }

如果您需要使用自己的自定义布局进行对话框,请在中添加布局

onCreateView
并删除
onCreateDialog
。但是添加点击侦听器,
onCreateView
就像我在
onCreateDialog

 @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {    View view = inflater.inflate(R.layout.activity_dialog, container, false);    return view;}

然后,在您

Activity
需要实施一个
interface
处理操作中
dialog

public class TryMeActivity extends     FragmentActivity implements GeneralDialogFragment.onDialogFragmentClickListener {    @Override        public void onOkClicked(GeneralDialogFragment dialog) {     // do your stuff        }        @Override        public void onCancelClicked(GeneralDialogFragment dialog) {     // do your stuff        }}

最后,显示

Dialog
从你
Activity
需要的时候,这样的

    GeneralDialogFragment generalDialogFragment =        GeneralDialogFragment.newInstance("title", "message");    generalDialogFragment.show(getSupportFragmentManager(),"dialog");

希望这可以帮助。我确信这种方法是最优化的方法之一,但是也可能有不同的方法。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377558.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号