【发布时间】: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