Android DialogFragment实例-分配不同的操作
我在理解DialoFragment的工作原理时遇到了很多问题 单击6个不同的操作按钮时,我需要在片段中生成6个类似的结构化AlertDialog。唯一的问题是,对话框的PositiveButtonOnClick事件在每种情况下都是不同的(基于单击的操作按钮) 我尝试使用DIalogFragment,而不是6次编写相同的代码行。到目前为止,我已经能够成功地显示6个不同的警报,但仍然无法为它们分配不同的任务。任何帮助都将不胜感激Android DialogFragment实例-分配不同的操作,android,android-dialogfragment,Android,Android Dialogfragment,我在理解DialoFragment的工作原理时遇到了很多问题 单击6个不同的操作按钮时,我需要在片段中生成6个类似的结构化AlertDialog。唯一的问题是,对话框的PositiveButtonOnClick事件在每种情况下都是不同的(基于单击的操作按钮) 我尝试使用DIalogFragment,而不是6次编写相同的代码行。到目前为止,我已经能够成功地显示6个不同的警报,但仍然无法为它们分配不同的任务。任何帮助都将不胜感激 public class AlertDialogSingleField
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
}
}
我刚找到一个办法。只是张贴供参考。在原始代码(有问题)中更改2个函数就可以了 在主片段中:
public void alert1(){
DialogFragment alertdialog = new AlertDialogSingleField();
Bundle args = new Bundle();
args.putString("title", "Title1");
alertdialog.setArguments(args);
alertdialog.setTargetFragment(this,0); **//ADD THIS LINE**
alertdialog.show(getFragmentManager(), "alert1");
}
myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Fragment ft = getTargetFragment();
if(ft != null){
((eximaction) ft).importFromInternalMemory(et.getText().toString());
}
}
});
在对话框片段中:
public void alert1(){
DialogFragment alertdialog = new AlertDialogSingleField();
Bundle args = new Bundle();
args.putString("title", "Title1");
alertdialog.setArguments(args);
alertdialog.setTargetFragment(this,0); **//ADD THIS LINE**
alertdialog.show(getFragmentManager(), "alert1");
}
myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Fragment ft = getTargetFragment();
if(ft != null){
((eximaction) ft).importFromInternalMemory(et.getText().toString());
}
}
});
只需使用对话框正按钮侦听器作为实例变量
public class AlertDialogSingleField extends DialogFragment{
DialogInterface.OnClickListener positiveListener;
@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", positiveListener);
return myDialog.create();
}
public void setPositiveListener(DialogInterface.OnClickListener positiveListener){
this.positiveListener = positiveListener;
}
public Interface testActions{
public void ActionForalert1();
public void ActionForalert2();
}
}
public class ImportExportFragment extends Fragment implements testActions{
public void alert1(){
DialogFragment alertdialog = new AlertDialogSingleField();
alertdialog.setPositiveListener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActionForalert1();
}
});
Bundle args = new Bundle();
args.putString("title", "Title1");
alertdialog.setArguments(args);
alertdialog.show(getFragmentManager(), "alert1");
}
public void alert2(){
DialogFragment alertdialog = new AlertDialogSingleField();
alertdialog.setPositiveListener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActionForalert2();
}
});
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
}
}