Este dialogFragment hará el trabajo por usted. Tenga en cuenta que el cuadro de diálogo permanecerá abierto después de la rotación de la pantalla conservando cualquier texto que el usuario ya haya escrito. Si no desea que eso suceda, debe descartar el fragmento en el onStop de su actividad. La firma del método newInstance se puede cambiar a lo que necesite.
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);
}
}
Agregue implementos a su actividad (cualquier tipo de actividad está bien):
public class Myctivity extends AppCompatActivity implements TextViewDialogFragment.Callbacks
{
...
}
Cree el diaglogFragment en su actividad de esta manera:
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);
Maneje el resultado en su actividad de esta manera:
@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.
}
Necesita crear el identificador de recursos, así que agregue este recurso en algún lugar debajo de res / valores
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="dialog_edit_text" type="id"/>
</resources>