[问题] 对话盒的建置(已解)

楼主: epistemie (1111)   2015-07-04 05:54:38
英文易读版
http://tinyurl.com/nqx48t7
私以为非常基本的东西:用对话盒来确认与使用者互动的流程
无奈遍巡资源后我所能做到的差强私意
主要是扩充 DialogFragment 和 implement NoticeDialogListener
这样做我觉得流程来讲实属混乱 我了解流程本身可能是会混乱一点
因为对话盒是以主续之外的续来进行的; 但我希望能有一个更好的做法
来让不同的对话盒有不同的回应 method 但我还没找到该怎么做
希望这样的描述够清楚 谢谢先
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()
}
}
我希望能让不同的确认对话盒有不同的 click listener
而非像这样用 *global* 变量/member 设定 dialog mode/state 来控制
怀念函数指标中
作者: ssccg (23)   2015-07-04 13:33:00
不要默认用activity当listener,建每个dialog的时候自己传一个listener进去不就好了只是要记得处理detach/attach时,要把listener换掉
作者: y3k (激流を制するは静水)   2015-07-04 19:55:00
你可以直接new listener
作者: blackZ2   2015-07-05 10:38:00
建议 先了解 java 中 set listener 的观念

Links booklink

Contact Us: admin [ a t ] ucptt.com