Dieses dialogFragment erledigt die Arbeit für Sie. Beachten Sie, dass das Dialogfeld nach der Bildschirmrotation geöffnet bleibt und der vom Benutzer bereits eingegebene Text erhalten bleibt. Wenn dies nicht gewünscht wird, müssen Sie das Fragment in onStop Ihrer Aktivität schließen. Die Signatur der newInstance-Methode kann nach Bedarf geändert werden.
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class TextViewDialogFragment extends DialogFragment implements DialogInterface.OnClickListener, DialogInterface.OnShowListener, TextWatcher
{
final static private String TITLE = "title", MESSAGE = "message", IDENTIFIER = "identifier", INPUT_TYPE = "inputType", POSITIVE_TEXT = "pText", NEGATIVE_TEXT = "nText", CANCELABLE = "cancelable";
public TextViewDialogFragment()
{
super();
}
static public TextViewDialogFragment newInstance(int title, @Nullable String message, int identifier, int inputType, int positiveText, int negativeText, boolean cancelable)
{
TextViewDialogFragment fragement = new TextViewDialogFragment();
Bundle args = new Bundle();
args.putInt(TITLE, title);
args.putString(MESSAGE, message);
args.putInt(IDENTIFIER, identifier);
args.putInt(INPUT_TYPE, inputType);
args.putInt(POSITIVE_TEXT, positiveText);
args.putInt(NEGATIVE_TEXT, negativeText);
args.putBoolean(CANCELABLE, cancelable);
fragement.setArguments(args);
return fragement;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Activity activity = getActivity();
Bundle args = getArguments();
EditText input = new EditText(activity);
input.setInputType(args.getInt(INPUT_TYPE));
input.setId(R.id.dialog_edit_text);
input.addTextChangedListener(this);
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setCancelable(args.getBoolean(CANCELABLE)).setTitle(args.getInt(TITLE)).setMessage(args.getString(MESSAGE)).setView(input).setPositiveButton(args.getInt(POSITIVE_TEXT), this);
int negativeText = args.getInt(NEGATIVE_TEXT);
if (negativeText != 0)
{
alert.setNegativeButton(negativeText, this);
}
AlertDialog dialog = alert.create();
dialog.setOnShowListener(this);
return dialog;
}
@Override
public void onShow(DialogInterface dialog)
{
// After device rotation there may be some text present.
if (((EditText)((AlertDialog) dialog).findViewById(R.id.dialog_edit_text)).length() == 0)
{
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
@Override
public void onClick(DialogInterface dialog, int which)
{
String text = ((EditText)((AlertDialog) dialog).findViewById(R.id.dialog_edit_text)).getText().toString();
((Callbacks) getActivity()).onTextViewDialogResult(which, getArguments().getInt(IDENTIFIER), text);
}
@Override
public void onCancel(DialogInterface dialog)
{
((Callbacks) getActivity()).onTextViewDialogActivityCancelled(getArguments().getInt(IDENTIFIER));
super.onCancel(dialog);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void afterTextChanged(Editable s)
{
((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0);
}
void setMessage(String message)
{
Bundle args = getArguments();
args.putString(MESSAGE, message);
setArguments(args);
}
interface Callbacks
{
void onTextViewDialogResult(int which, int identity, String text);
void onTextViewDialogActivityCancelled(int identity);
}
}
Fügen Sie Ihrer Aktivität Geräte hinzu (jede Art von Aktivität ist in Ordnung):
public class Myctivity extends AppCompatActivity implements TextViewDialogFragment.Callbacks
{
...
}
Erstellen Sie das diaglogFragment in Ihrer Aktivität wie folgt:
final static int SOMETHING = 1;
myDF = TextViewDialogFragment.newInstance(R.string.my_title, "my message", SOMETHING, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, /* Whatever is best for your user. */ R.string.yay, android.R.string.cancel, true);
Behandeln Sie das Ergebnis Ihrer Aktivität folgendermaßen:
@Override
public void onTextViewDialogResult(int which, int identity, String text)
{
if (which == AlertDialog.BUTTON_NEGATIVE)
{
// User did not want to do anything.
return;
}
// text now holds the users answer.
// Identity can be used if you use the same fragment for more than one type of question.
}
@Override
public void onTextViewDialogActivityCancelled(int identity)
{
// This is invoked if you set cancelable to true and the user pressed the back button.
}
Sie müssen die Ressourcenkennung erstellen, also fügen Sie diese Ressource irgendwo unter res / values hinzu
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="dialog_edit_text" type="id"/>
</resources>