2015-07-03 102 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

什麼是推薦的替代方案? –