【问题标题】:DialogFragment Buttons not working after screen rotation屏幕旋转后DialogFragment按钮不起作用
【发布时间】:2015-04-07 21:07:53
【问题描述】:

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

我用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;

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

【问题讨论】:

  • 检查轮换后“currentInput”的值是多少
  • 感谢大家的支持,但问题不在 currentInput 变量中,但在搜索 android API 后我解决了这个问题,实际上我是通过 @overriding OnCreateDialog 创建我的对话框并需要实现
  • 请贴出答案,以后会帮助到其他人
  • 对话框片段的工作代码

标签: android android-fragments android-dialogfragment


【解决方案1】:

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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多