它應該是一個非常普遍的事情:讓對話框確認進行與用戶交互的流程。但最好的我可以拿出我挖掘的信息對我來說並不好。我主要擴展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()
}
}
我在想,如果我可以將不同的點擊收聽到各確認創建的,而不是設置使用全球變量/像該成員的對話模式/狀態對話框。是缺少函數指針這裏...
順便說一句,爲什麼我不能正確地張貼在灰色方塊中的申報開始和結束的支架? –