久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    • <bdo id='9oXBK'></bdo><ul id='9oXBK'></ul>

    <small id='9oXBK'></small><noframes id='9oXBK'>

    <tfoot id='9oXBK'></tfoot>
    <i id='9oXBK'><tr id='9oXBK'><dt id='9oXBK'><q id='9oXBK'><span id='9oXBK'><b id='9oXBK'><form id='9oXBK'><ins id='9oXBK'></ins><ul id='9oXBK'></ul><sub id='9oXBK'></sub></form><legend id='9oXBK'></legend><bdo id='9oXBK'><pre id='9oXBK'><center id='9oXBK'></center></pre></bdo></b><th id='9oXBK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='9oXBK'><tfoot id='9oXBK'></tfoot><dl id='9oXBK'><fieldset id='9oXBK'></fieldset></dl></div>

      <legend id='9oXBK'><style id='9oXBK'><dir id='9oXBK'><q id='9oXBK'></q></dir></style></legend>

      在java Android中為自定義Dialog創(chuàng)建一個(gè)通用類

      Create a general class for custom Dialog in java Android(在java Android中為自定義Dialog創(chuàng)建一個(gè)通用類)
        <tbody id='YfWXa'></tbody>
      1. <tfoot id='YfWXa'></tfoot>
      2. <small id='YfWXa'></small><noframes id='YfWXa'>

        • <bdo id='YfWXa'></bdo><ul id='YfWXa'></ul>
            • <legend id='YfWXa'><style id='YfWXa'><dir id='YfWXa'><q id='YfWXa'></q></dir></style></legend>
              <i id='YfWXa'><tr id='YfWXa'><dt id='YfWXa'><q id='YfWXa'><span id='YfWXa'><b id='YfWXa'><form id='YfWXa'><ins id='YfWXa'></ins><ul id='YfWXa'></ul><sub id='YfWXa'></sub></form><legend id='YfWXa'></legend><bdo id='YfWXa'><pre id='YfWXa'><center id='YfWXa'></center></pre></bdo></b><th id='YfWXa'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='YfWXa'><tfoot id='YfWXa'></tfoot><dl id='YfWXa'><fieldset id='YfWXa'></fieldset></dl></div>

                本文介紹了在java Android中為自定義Dialog創(chuàng)建一個(gè)通用類的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我的應(yīng)用程序顯示了許多自定義對(duì)話框,例如是/否或接受/取消決定,當(dāng)我編寫代碼時(shí),我意識(shí)到有很多代碼重復(fù),遵循相同的架構(gòu).

                My app shows many custom dialog like Yes/No or Accept/Cancel decissions and, while I was coding, I realized that there are so much code repeated, following the same schema.

                我想構(gòu)建一個(gè)通用類,但我不知道該怎么做,或者更確切地說,我必須做的正確方法(接口、抽象類、繼承、靜態(tài)類……)

                I want to build a general class but I don't know how to do it or, more exactly, the correct way that I have to do it(interfaces, abstract classes, inheritance, static classes, ...)

                這是我目前的課程:

                public class DialogTwoOptions extends Dialog {
                
                TextView title_tv;
                // Button yes_btn, no_btn;
                
                public DialogTwoOptions(Context context) 
                {
                    super(context);     
                    setContentView(R.layout.dialogo_sino); // a simple layout with a TextView and Two Buttons
                
                    title_tv = (TextView) findViewById(R.id.dialogo_titulo_sino);
                    // yes_btn = (Button) findViewById(R.id.dialogo_aceptar); 
                    // no_btn = (Button) findViewById(R.id.dialogo_cancelar);
                
                    View v = getWindow().getDecorView();
                    v.setBackgroundResource(android.R.color.transparent);
                }
                
                 public void quitDialog(View v) {
                     if (isShowing()) dismiss();
                 }
                
                 public void setTitle(String title) {
                     title_tv.setText(title);
                 }
                

                }

                當(dāng)我需要使用這個(gè)類時(shí),這就是我正在做的事情:

                And this is what I am doing when I need to use this class:

                final DialogTwoOptions dialog = new DialogTwoOptions(this);
                
                    Button yes = (Button) dialog.findViewById(R.id.dialog_yes_btn);
                    Button no = (Button) dialog.findViewById(R.id.dialog_no_btn);
                
                    yes.setOnClickListener(new Button.OnClickListener() 
                    {
                        public void onClick(View v)     {
                            dialog.dismiss();
                            // Do something 
                        }
                    });
                
                    no.setOnClickListener(new Button.OnClickListener() 
                    {
                        public void onClick(View v)     {
                            dialog.dismiss();
                            // Do something
                        }
                    });
                
                    dialog.show();
                

                我確信它是可以改進(jìn)的,但是你怎么能這樣做呢?

                I am sure that it is improvable, but how could you do this?

                謝謝

                推薦答案

                首先創(chuàng)建一個(gè) Base DialogFragment 來保存 Activity 的實(shí)例.因此,當(dāng) Dialog 附加到 Activity 時(shí),您將知道創(chuàng)建它的 Activity 的實(shí)例.

                First create an Base DialogFragment to keep hold of the instance of the Activity. So when the Dialog is attached to the Activity , you will know the instance of the Activity which created it.

                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;
                        }
                }
                

                然后,創(chuàng)建一個(gè)擴(kuò)展 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();
                        }
                
                    }
                

                如果您需要為對(duì)話框使用自己的自定義布局,請(qǐng)?jiān)?onCreateView 中填充布局并刪除 onCreateDialog .但是像我在 onCreateDialog 中解釋的那樣在 onCreateView 中添加點(diǎn)擊監(jiān)聽器

                If you need to use your own custom layout for dialog,then inflate a layout in onCreateView and remove onCreateDialog . But Add the click listeners in onCreateView like i explained in onCreateDialog

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

                那么,在你的Activity中需要實(shí)現(xiàn)一個(gè)interface來處理dialog

                Then , In your Activity need to implement an interface to handle the action in 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
                        }
                }
                

                最后,在需要時(shí)顯示 Activity 中的 Dialog,像這樣

                Finally, Show the Dialog from your Activity when required, like this

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

                希望這會(huì)有所幫助.我確信這種方法是優(yōu)化的方法之一,但也可能有不同的方法.

                Hope this helps. I am sure this approach is one of the optimized way, but there could be also different approaches .

                這篇關(guān)于在java Android中為自定義Dialog創(chuàng)建一個(gè)通用類的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                How can I detect integer overflow on 32 bits int?(如何檢測(cè) 32 位 int 上的整數(shù)溢出?)
                Local variables before return statements, does it matter?(return 語(yǔ)句之前的局部變量,這有關(guān)系嗎?)
                How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                Inconsistent behavior on java#39;s ==(java的行為不一致==)
                Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲(chǔ)為 int?)
                  <bdo id='6k5EW'></bdo><ul id='6k5EW'></ul>
                      <tbody id='6k5EW'></tbody>

                    <small id='6k5EW'></small><noframes id='6k5EW'>

                        1. <legend id='6k5EW'><style id='6k5EW'><dir id='6k5EW'><q id='6k5EW'></q></dir></style></legend>

                        2. <tfoot id='6k5EW'></tfoot>
                          <i id='6k5EW'><tr id='6k5EW'><dt id='6k5EW'><q id='6k5EW'><span id='6k5EW'><b id='6k5EW'><form id='6k5EW'><ins id='6k5EW'></ins><ul id='6k5EW'></ul><sub id='6k5EW'></sub></form><legend id='6k5EW'></legend><bdo id='6k5EW'><pre id='6k5EW'><center id='6k5EW'></center></pre></bdo></b><th id='6k5EW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6k5EW'><tfoot id='6k5EW'></tfoot><dl id='6k5EW'><fieldset id='6k5EW'></fieldset></dl></div>
                          主站蜘蛛池模板: 91视频免费黄 | 在线播放国产一区二区三区 | 精品成人 | 亚洲美女网站 | 日韩欧美一区二区三区免费观看 | 日韩在线一区二区三区 | 成人欧美一区二区三区黑人孕妇 | 日韩欧美在线视频 | 中国一级大毛片 | 色网在线看| 一区视频在线播放 | 亚洲欧美激情精品一区二区 | 一区二区三区观看视频 | a黄视频| 正在播放国产精品 | 亚洲一区二区三区高清 | 97精品国产97久久久久久免费 | 国产精品美女久久久久久免费 | 欧美国产一区二区 | 欧美日韩中文字幕在线播放 | 波多野结衣一区二区三区 | 久久久久网站 | 在线一级片 | 国产一级片在线观看视频 | 成人在线播放网址 | 久久久久国 | 免费视频二区 | 男女视频91| 亚洲二区在线 | 亚洲国产精品一区二区www | 99久视频| 日本天天操| 国产在线一区二区三区 | 亚洲国产欧美日韩 | 一区精品视频在线观看 | 在线午夜| 中文字幕在线免费观看 | 成人免费网站在线 | 综合色影院 | 九九热在线免费观看 | 午夜精品久久久久久久 |