请帮帮我。我不知道为什么,但在屏幕旋转后,EditText为空。我使用setRetainInstance(true),但它对我没有帮助。请告诉我使用选择和文本保存对话框的状态和EditText状态的正确方法
我的代码:
public class InputTextDialog extends DialogFragment implements OnClickListener {
static final String DIALOG_TAG = "InputTextDialog";
static EditText input;
String initialText;
InputTextDialogListener inputTextDialogListener = null;
public interface InputTextDialogListener {
void onChangeText(String text);
}
public void setInputTextDialogListener(InputTextDialogListener listener) {
inputTextDialogListener = listener;
}
public void setInitialText(String text) {
initialText = text;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
getDialog().setDismissMessage(null);
super.onDestroyView();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
input = new EditText(getActivity());
input.setHint("Enter your text");
if (initialText != null) {
input.setText(initialText);
} else {
input.setText("");
}
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
.setTitle("Enter your text").setPositiveButton("Apply", this)
.setNegativeButton("Cancel", this).setView(input);
return adb.create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == Dialog.BUTTON_POSITIVE && inputTextDialogListener != null) {
inputTextDialogListener.onChangeText(input.getText().toString());
}
}
}
在活动中显示对话框:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InputTextDialog inputDialog = new InputTextDialog();
inputDialog.setInitialText(initialText);
inputDialog.setInputTextDialogListener(new InputTextDialogListener() {
@Override
public void onChangeText(String text) {
resultText = text;
}
});
inputDialog.show(getSupportFragmentManager(), InputTextDialog.DIALOG_TAG);
}
});
答案 0 :(得分:0)
谢谢我通过以下方式解决了这个问题:
package com.xfakehopex.lovelywidget.Dialogs;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.EditText;
public class InputTextDialog extends DialogFragment implements OnClickListener {
public static final String DIALOG_TAG = "InputTextDialog";
private static final String DIALOG_TITLE_BUNDLE= "dialogTitle";
private static final String DIALOG_TEXT_BUNDLE= "dialogText";
private static final String DIALOG_TEXT_SELECTION_START_BUNDLE = "dialogTextSelectionStart";
private static final String DIALOG_TEXT_SELECTION_END_BUNDLE = "dialogTextSelectionEnd";
private EditText input;
private String initialText;
private String title;
InputTextDialogListener inputTextDialogListener = null;
public interface InputTextDialogListener {
void onChangeText(String text);
}
public void setInputTextDialogListener(InputTextDialogListener listener) {
inputTextDialogListener = listener;
}
public void setInitialText(String text) {
initialText = text;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
input = new EditText(getActivity());
input.setHint("Введите текст");
if (savedInstanceState != null) {
String text = savedInstanceState.getString(DIALOG_TEXT_BUNDLE);
title = savedInstanceState.getString(DIALOG_TITLE_BUNDLE);
int selectionStart = savedInstanceState.getInt(DIALOG_TEXT_SELECTION_START_BUNDLE);
int selectionEnd = savedInstanceState.getInt(DIALOG_TEXT_SELECTION_END_BUNDLE);
input.setText(text);
input.setSelection(selectionStart, selectionEnd);
}
else
{
if (initialText != null) {
input.setText(initialText);
} else {
input.setText("");
}
}
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
.setTitle(title).setPositiveButton("Apply", this)
.setNegativeButton("Cancel", this).setView(input);
return adb.create();
}
@Override
public void onSaveInstanceState(Bundle saved) {
super.onSaveInstanceState(saved);
saved.putString(DIALOG_TEXT_BUNDLE, input.getText().toString());
saved.putString(DIALOG_TITLE_BUNDLE, title);
saved.putInt(DIALOG_TEXT_SELECTION_START_BUNDLE, input.getSelectionStart());
saved.putInt(DIALOG_TEXT_SELECTION_END_BUNDLE, input.getSelectionEnd());
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == Dialog.BUTTON_POSITIVE && inputTextDialogListener != null) {
inputTextDialogListener.onChangeText(input.getText().toString());
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}