DialogFragment按钮在屏幕旋转后不起作用

时间:2015-03-27 11:59:52

标签: android android-fragments android-dialogfragment

在代码中我创建了一个对话框片段,在搜索了不同的博客后,能够在屏幕旋转后保留Dialog Fragment,但是当我将它用作输入对话框时,它的按钮不起作用。

我使用了setRetainInstance(true);

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance())
        getDialog().setOnDismissListener(null);
    super.onDestroyView();
}

但按钮不工作
我的对话框片段代码

public class TagDialogFragment extends DialogFragment {
Dialog tagDialog=null;
 public static TagDialogFragment newInstance(String title) {
        TagDialogFragment frag = new TagDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    } 
public interface TagDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog,String tag);
    public void onDialogNegativeClick(DialogFragment dialog);
}
 // Use this instance of the interface to deliver action events
TagDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the TagDialogListener
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the TagDialogListener so we can send events to the host
        mListener = (TagDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement TagDialogListener");
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    //Use inflater to inflate the custom layout for our alert
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogLayout= inflater.inflate(R.layout.tag_dialog,null);
    final TextView entredTag=(TextView)dialogLayout.findViewById(R.id.tag);
    builder.setView(dialogLayout)
           .setTitle("Enter tag name for calculation")
           .setPositiveButton("Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   String tag=entredTag.getText().toString();
                   mListener.onDialogPositiveClick(TagDialogFragment.this,tag);
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   mListener.onDialogNegativeClick(TagDialogFragment.this);
               }
           });
    // Create the AlertDialog object and return it
    tagDialog=builder.create();
    return tagDialog;
}

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance())
        getDialog().setOnDismissListener(null);
    super.onDestroyView();
}
}

在我的活动对话框中实例化为

case R.id.btntag:
         //create the input dialog
        if(Double.parseDouble(currentInput)!=0){
            tagDialog=TagDialogFragment.newInstance("tagDialog");
            tagDialog.show(getSupportFragmentManager(), "tagDialog");
        }
        break;

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:2)

Dialog Fragment的功能代码,我们需要覆盖onCreate()和onDestroyView()

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;


public class TagDialogFragment extends DialogFragment {
 public static TagDialogFragment newInstance(String title) {
        TagDialogFragment frag = new TagDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    } 
public interface TagDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog,String tag);
}
 // Use this instance of the interface to deliver action events
TagDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the TagDialogListener
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the TagDialogListener so we can send events to the host
        mListener = (TagDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement TagDialogListener");
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    String title = getArguments().getString("title");
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    //Use inflater to inflate the custom layout for our alert
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogLayout= inflater.inflate(R.layout.tag_dialog,null);
    final EditText entredTag=(EditText)dialogLayout.findViewById(R.id.tag);
    builder.setView(dialogLayout)
           .setTitle(title)
           .setPositiveButton("Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   String tag=entredTag.getText().toString();
                   mListener.onDialogPositiveClick(TagDialogFragment.this,tag);
               }
           })
           .setNegativeButton("Cancel",null);
    // Create the AlertDialog object and return it
    return builder.create();
}

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance())
        getDialog().setOnDismissListener(null);
    super.onDestroyView();
}