2016-02-29 61 views
3

我目前正在尝试构建一个简单的设置片段警报框。与片段警报框的故障

我已经构建了警告框的主体,但是我注意到实际的Dialog方法没有使用。此外,该对话框方法取代了onCreate()方法。

我该如何实际使用Dialog方法?在创建对话框方法后,我似乎无法简单地调用它,因为它出现错误。

我必须在某些XML中引用它吗? 如果是这样,那么哪个XML - Fragment的XML或其父Activity的XML?

以下是我的Dialog Fragment的代码。我还没有修改它的XML。

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.Fragment; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 

public class ExceedingLimitFragment extends Fragment { 

    public Dialog onCreateDialogExceed (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle(R.string.exceeding_limit_title); 
     builder.setMessage(R.string.exceeding_limit_message); 
     builder.setPositiveButton(R.string.exceeding_limit_positive, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Intent ExceedingLimitPositiveIntent = new Intent (ExceedingLimitFragment.this.getActivity(), SettingsMenu.class); 
       startActivity(ExceedingLimitPositiveIntent); 
      } 
     }); 
     builder.setNegativeButton(R.string.exceeding_limit_negative, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 

     return builder.create(); 
    } 

} 

回答

1

我注意到上述代码的一些问题。 拳头:

public Dialog onCreateDialogExceed (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

看起来不对。您应该只是调用您正在覆盖的方法的super方法。在这种情况下,onCreateDialogExceed是您自己的方法,因此您应该致电onCreate()。系统依赖于你在覆盖它们之后立即调用这些方法,以便它知道它们以正确的顺序被调用。

请问ExceedingLimitFragment需要显示DialogFragment还是实际上是DialogFragment?如果答案是后者,请确保扩展该类而不是Fragment。在完成之后,将onCreateDialogExceed()重命名为onCreateDialog(),当实际需要创建对话框时,该命令将由系统调用。

为了显示你从你的活动或片段需要一个参考对话片段,并做一些这样的:在@ dev.bmax的回答

至于

new ExceedingLimitFragment().show(getFragmentManager(), TAG); 

为您标题,finish()Activity的一种方法。Fragments或DialogFragments都不知道,因此您的IDE抱怨它是有道理的。为了在碎片中使用它,请务必做

if(isAttached(){ 
    getActivity.finish(); 
} 
+0

你好。关于第一点,我已经删除了onCreate调用中的'超级'标签。这个可以吗?另外,在第二段中,Studio声明下一个类不会覆盖它的超类。所以我已经把它留下了。而在最后一段中,只是一个问题 - 我可以使用dialog.cancel()来代替吗?谢谢! – Mildwood

+1

如果你从Android重写一个方法,确保在调用其他任何东西之前先调用'super'。看看FragmentDialogs的文档,看看onCreateDialog的作用和创建时间:)至于'dialog.cancel()'问题,它取决于你想做什么。如果你想关闭对话框,你应该调用'fragmentDialog.dismiss()'而不是'dialog.cancel()'。 FragmentDialogs基本上是一个承载对话框的片段,因此你希望片段为你处理对话框。 –

1

公共静态类MyAlertDialogFragment扩展DialogFragment {

 public static MyAlertDialogFragment newInstance(int title) { 
      MyAlertDialogFragment frag = new MyAlertDialogFragment(); 
      Bundle args = new Bundle(); 
      args.putInt("title", title); 
      frag.setArguments(args); 
      return frag; 
     } 

     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      int title = getArguments().getInt("title"); 

      return new AlertDialog.Builder(getActivity()) 
        .setIcon(R.drawable.alert_dialog_icon) 
        .setTitle(title) 
        .setPositiveButton(R.string.alert_dialog_ok, 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           ((FragmentAlertDialog)getActivity()).doPositiveClick(); 
          } 
         } 
        ) 
        .setNegativeButton(R.string.alert_dialog_cancel, 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           ((FragmentAlertDialog)getActivity()).doNegativeClick(); 
          } 
         } 
        ) 
        .create(); 
     } 
    } 

      public void showDialog() { 
       DialogFragment newFragment = MyAlertDialogFragment.newInstance(
         R.string.alert_dialog_two_buttons_title); 


     newFragment.show(getFragmentManager(), "dialog"); 
     } 



      public void doPositiveClick() { 
       // Do stuff here. 
       Log.i("FragmentAlertDialog", "Positive click!"); 
      } 

    public void doNegativeClick() { 
     // Do stuff here. 
     Log.i("FragmentAlertDialog", "Negative click!"); 
    } 


The activity creating this fragment may have the above methods to show the dialog and receive results from it, This is how you can define your dialog using dialog fragment and get the call backs in your project. 

希望这会帮助你。

+0

请写一个简要说明为什么这应该解决OP的问题。 – Michael

1

通常,你的子类DialogFragment。要启动它,你实例化对话框片段并将其称为show方法。

new ExceedingLimitFragment().show(getFragmentManager(), TAG); 

注意:您应该getFragmentManager()或getSupportFragmentManager(),取决于使用哪个DialogFragment的版本,子类(android.app.DialogFragment或android.support.v4.app.DialogFragment)

1

你可以创建方法的ShowDialog(),并把这个代码吧..这是多么有两个选项确定创建一个简单的警告对话框或取消

new AlertDialog.Builder(context) 
     .setTitle("Add title ") 
     .setMessage("Add some message") 
     .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }) 
     .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }) 
     .setIcon(android.R.drawable.ic_dialog_alert) 
     .show(); 

希望能帮助你!