【问题标题】:Dismiss DialogFragment onClick关闭 DialogFragment onClick
【发布时间】:2012-05-07 10:15:19
【问题描述】:

我试图制作一个可以在点击时关闭的 DialogFragment,经过一番搜索后我决定使用此实现:

public class ErrorDialogFragment extends RoboDialogFragment {

private static final String MESSAGE_ARG = "message";
private TextView text;

public ErrorDialogFragment newInstance (String message){
    ErrorDialogFragment f = new ErrorDialogFragment();

        Bundle args = new Bundle();
        args.putString(MESSAGE_ARG, message);
        f.setArguments(args);

        return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
    View v = inflater.inflate(R.layout.error_dialog_fragment, container, false);
    v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ErrorDialogFragment.this.dismiss();
        }
    });

    text = (TextView) v.findViewById(R.id.error_dialog_text_textView);
    text.setText(getArguments().getString(MESSAGE_ARG));
    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, 0);
}

警报对话框可以有一条自定义消息,并在点击时消失。

您认为实现这一目标的更好方法吗?

谢谢。

【问题讨论】:

    标签: android dialog android-fragments


    【解决方案1】:

    您可以使用 dialog.setCanceledOnTouchOutside(true); 如果您触摸对话框之外,它将关闭对话框。或

    试试这个教程http://iserveandroid.blogspot.com/2010/11/how-to-dismiss-custom-dialog-based-on.html。 希望对您有所帮助..!!

    【讨论】:

    • 感谢@Raghav 我现在使用 setCanceledOnTouchOutside,但是对于内部点击我将继续我的方法,该链接指的是一个活动,也不是一个 DialogFragment :)
    • 你能告诉我你在哪里调用 setCanceledOnTouchOutside 方法吗?因为我需要那个对话框对象
    • 只需将它写在您对对话框执行所有其他操作的地方。
    【解决方案2】:
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle(title);
            alertDialog.setMessage(msg);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    alertDialog.dismiss();
                }
            });
            alertDialog.setIcon(R.drawable.error_icon);
            alertDialog.show();
    

    只要您想显示警报,只需使用此代码,其 ok onclick 事件对话框将关闭。

    【讨论】:

    • 感谢@user1208720,但我的类从 DialogFragment 扩展而来(故意不是 AlertDialog),并且在单击按钮时它不会关闭,而是在单击视图上的任何位置时关闭。问题是关于使用android框架中的这个DialogFragment类以及如何正确调度这样的事件:)
    【解决方案3】:

    我正在从 Activity 调用 DialogFragment。单击对话框中的按钮后,我使用接口调用活动中的方法。在那个活动中,我正在执行这个:

    // This is the code inside the activity that call the dialog
    Fragment fragment = getSupportFragmentManager().findFragmentByTag("MyDialog");
    if(fragment != null) {
        DialogFragment dialog = (DialogFragment) fragment;
        dialog.dismiss();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 2013-07-01
      • 2014-07-31
      • 2012-02-02
      • 2012-06-27
      • 2021-04-11
      相关资源
      最近更新 更多