<i id='ujHPk'><tr id='ujHPk'><dt id='ujHPk'><q id='ujHPk'><span id='ujHPk'><b id='ujHPk'><form id='ujHPk'><ins id='ujHPk'></ins><ul id='ujHPk'></ul><sub id='ujHPk'></sub></form><legend id='ujHPk'></legend><bdo id='ujHPk'><pre id='ujHPk'><center id='ujHPk'></center></pre></bdo></b><th id='ujHPk'></th></span></q></dt></tr></i><div id='ujHPk'><tfoot id='ujHPk'></tfoot><dl id='ujHPk'><fieldset id='ujHPk'></fieldset></dl></div>
  • <tfoot id='ujHPk'></tfoot>

  • <small id='ujHPk'></small><noframes id='ujHPk'>

        <bdo id='ujHPk'></bdo><ul id='ujHPk'></ul>
      1. <legend id='ujHPk'><style id='ujHPk'><dir id='ujHPk'><q id='ujHPk'></q></dir></style></legend>
      2. 在java Android中为自定义Dialog创建一个通用类

        时间:2023-06-28

            <bdo id='vFVbm'></bdo><ul id='vFVbm'></ul>

              <tfoot id='vFVbm'></tfoot>
              <i id='vFVbm'><tr id='vFVbm'><dt id='vFVbm'><q id='vFVbm'><span id='vFVbm'><b id='vFVbm'><form id='vFVbm'><ins id='vFVbm'></ins><ul id='vFVbm'></ul><sub id='vFVbm'></sub></form><legend id='vFVbm'></legend><bdo id='vFVbm'><pre id='vFVbm'><center id='vFVbm'></center></pre></bdo></b><th id='vFVbm'></th></span></q></dt></tr></i><div id='vFVbm'><tfoot id='vFVbm'></tfoot><dl id='vFVbm'><fieldset id='vFVbm'></fieldset></dl></div>
                <tbody id='vFVbm'></tbody>

                <small id='vFVbm'></small><noframes id='vFVbm'>

                <legend id='vFVbm'><style id='vFVbm'><dir id='vFVbm'><q id='vFVbm'></q></dir></style></legend>
                • 本文介绍了在java Android中为自定义Dialog创建一个通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的应用程序显示了许多自定义对话框,例如是/否或接受/取消决定,当我编写代码时,我意识到有很多代码重复,遵循相同的架构.

                  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.

                  我想构建一个通用类,但我不知道该怎么做,或者更确切地说,我必须做的正确方法(接口、抽象类、继承、静态类……)

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

                  }

                  当我需要使用这个类时,这就是我正在做的事情:

                  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();
                  

                  我确信它是可以改进的,但是你怎么能这样做呢?

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

                  谢谢

                  推荐答案

                  首先创建一个 Base DialogFragment 来保存 Activity 的实例.因此,当 Dialog 附加到 Activity 时,您将知道创建它的 Activity 的实例.

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

                  然后,创建一个扩展 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 .但是像我在 onCreateDialog 中解释的那样在 onCreateView 中添加点击监听器

                  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中需要实现一个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
                          }
                  }
                  

                  最后,在需要时显示 Activity 中的 Dialog,像这样

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

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

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

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

                  这篇关于在java Android中为自定义Dialog创建一个通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:点击 Android 对话框外部以将其关闭? 下一篇:无法解析方法'show(android.support.v4.app.FragmentManager, java

                  相关文章

                • <tfoot id='CdBld'></tfoot>
                        <bdo id='CdBld'></bdo><ul id='CdBld'></ul>

                    1. <small id='CdBld'></small><noframes id='CdBld'>

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

                      <legend id='CdBld'><style id='CdBld'><dir id='CdBld'><q id='CdBld'></q></dir></style></legend>