我有很多理解DialoFragment如何工作的問題。DialogFragment實例 - 分配不同的動作
當我點擊6個不同的動作按鈕時,我需要在我的片段中生成6個類似結構的AlertDialog。唯一的問題是,Dialog的PositiveButton onClick事件在每種情況下都會有所不同(基於哪個操作按鈕被點擊)。
而不是寫6次相同的代碼行,我試圖使用DIalogFragment。到目前爲止,我已經到了可以成功顯示6種不同警報的程度,但仍然無法爲他們分配不同的任務。任何幫助將不勝感激。
public class AlertDialogSingleField extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
String title = args.getString("title");
AlertDialog.Builder myDialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.single_field_alert, null);
myDialog.setView(layout);
myDialog.setTitle(title);
myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//ACTIONS HERE WILL BE DIFFERENT DEPENDING ON WHICH METHOD WAS CALLED TO CREATE THIS DIALOG
}
});
return myDialog.create();
}
public Interface testActions{
public void ActionForalert1();
public void ActionForalert2();
}
}
public class ImportExportFragment extends Fragment implements testActions{
public void alert1(){
DialogFragment alertdialog = new AlertDialogSingleField();
Bundle args = new Bundle();
args.putString("title", "Title1");
alertdialog.setArguments(args);
alertdialog.show(getFragmentManager(), "alert1");
}
public void alert2(){
DialogFragment alertdialog = new AlertDialogSingleField();
Bundle args = new Bundle();
args.putString("title", "Title2");
alertdialog.setArguments(args);
alertdialog.show(getFragmentManager(), "alert2");
}
public void ActionForalert1(){
//THINGS TO DO WHEN AlertDialog created through alert1 method
}
public void ActionForalert2(){
//THINGS TO DO WHEN AlertDialog created through alert2 method
}
}