2015-07-03 41 views
0

它应该是一个非常普遍的事情:让对话框确认进行与用户交互的流程。但最好的我可以拿出我挖掘的信息对我来说并不好。我主要扩展DialogFragment(首先搜索示例)并实现NoticeDialogListener。如何使用Android正确编码确认对话框?

我开始相信这不是更好的方法,因为就程序流而言,这非常麻烦。程序流向明白无论如何,因为Dialog显示为从主开始的另一个线程,但我想应该有更好的方法来为不同的对话框分配不同的响应方法。但除了接下来的事情,我一直无法找到方法。

希望我已经清楚地描述了我的问题。预先感谢回复。

public class Confirm extends DialogFragment { 

    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle(sQ); 
     builder 
       .setPositiveButton(sYes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         mListener.Yes(); 
        } 
       }) 
       .setNegativeButton(sNo, null); 
     return builder.create(); 
    } 

    public interface NoticeDialogListener { 
     void Yes(); 
    } 
    private NoticeDialogListener mListener; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     mListener = (NoticeDialogListener) activity; 
    } 
} 

public class Main extends ActionBarActivity 
     implements Confirm.NoticeDialogListener { 
... 

    private int iDialogMode; 
    private final static int DIALOG_ST_0 = 0; 
    private final static int DIALOG_ST_1 = DIALOG_ST_1 + 1; 
    private final static int DIALOG_ST_2 = DIALOG_ST_1 + 1; 

    @Override 
    public void Yes() { 
     switch (iDialogMode) { 
      case DIALOG_ST_0: // follow up HERE0 for what that dialog prompted 
       break; 
      case DIALOG_ST_1: // HERE1: feeling not smart 
       break; 
      case DIALOG_ST_2: // HERE2: believe there should be a better way 
       break; 
     } 
    } 

    public ... State_0_doing_something (...) { 
     ... 
     Confirm diaBox = new Confirm (...); 
     iDialogMode = DIALOG_ST_0; 
     diaBox.show(getSupportFragmentManager(), "State_0"); 
     // what's supposed to continue if confirmed will be followed up in HERE0 in Yes() 
    } 

    public ... State_1_doing_something_else (...) { 
     ... 
     Confirm diaBox = new Confirm (...); 
     iDialogMode = DIALOG_ST_1; 
     diaBox.show(getSupportFragmentManager(), "State_2"); 
     // what's supposed to continue if confirmed will be followed up in HERE1 in Yes() 
    } 

    public ... State_2_doing_yet_something_else (...) { 
     ... 
     Confirm diaBox = new Confirm (...); 
     iDialogMode = DIALOG_ST_2; 
     diaBox.show(getSupportFragmentManager(), "State_3"); 
     // what's supposed to continue if confirmed will be followed up in HERE2 in Yes() 
    } 
} 

我在想,如果我可以将不同的点击收听到各确认创建的,而不是设置使用全球变量/像该成员的对话模式/状态对话框。是缺少函数指针这里...

+0

顺便说一句,为什么我不能正确地张贴在灰色方块中的申报开始和结束的支架? –

回答

0

解决:唯一的问题是,它不是好看

AlertDialog.Builder builder = new AlertDialog.Builder(Home.theContext); 
      builder.setMessage(R.string.confirm_delete); 
      builder.setCancelable(true); 
      builder.setPositiveButton(R.string.confirm_yes, vCabDelete()); 
      builder.setNegativeButton(Home.theContext.getString(R.string.confirm_no), null); 
      AlertDialog dialog = builder.create(); 
      dialog.show(); 

.... 

private static DialogInterface.OnClickListener vCabDelete() { 
    return new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface di, int id) { 
      .... 
     } 
    }; 
} 
0

“顺便说一下,为什么我不能正确地张贴在灰色方块开始申报和结尾括号?”

文本前加4个空格,或在代码文本的开始和结尾处添加`字符。 例子: Fake code that does nothing

“程序流明智我的理解是很麻烦反正作为对话框出现在主另一个线程开始与”

这基本上是为什么它不打算为用户保留关于使用对话框。到目前为止,我看到大多数程序的方式是,只有可能严重延迟应用程序或可能对用户收费的操作才会使用对话框。另外,请注意,Activity生命周期将使您保持其“状态”的记忆,这可以进一步增加onPause/onResume与您的应用程序的问题。

+0

什么是推荐的替代方案? –