Cómo hacer un cuadro de edición de texto en un cuadro de diálogo
Estoy intentando crear un cuadro de edición de texto en un cuadro de diálogo para ingresar una contraseña. y cuando hago no puedo hacer. Soy principiante en ello. Por favor, ayúdame en esto.
public class MainActivity extends Activity {
Button create, show, setting;
//String pass="admin";String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
create = (Button)findViewById(R.id.amcreate);
setting = (Button)findViewById(R.id.amsetting);
show = (Button)findViewById(R.id.amshow);
//input = (EditText)findViewById(R.id.this);
setting.setVisibility(View.INVISIBLE);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent1 = new Intent(view.getContext(), Create.class);
startActivityForResult(myIntent1, 0);
}
});
show.setOnClickListener(new View.OnClickListener() {
//@SuppressWarnings("deprecation")
public void onClick(final View view) {
// Creating alert Dialog with one Button
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
//AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("PASSWORD");
// Setting Dialog Message
alertDialog.setMessage("Enter Password");
**final EditText input = new EditText(this);**
//alertDialog.setView(input);
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.key);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),"Password Matched", Toast.LENGTH_SHORT).show();
Intent myIntent1 = new Intent(view.getContext(), Show.class);
startActivityForResult(myIntent1, 0);
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
dialog.cancel();
}
});
// closed
// Showing Alert Message
alertDialog.show();
}
});
Imagen
quiero llegar como
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("PASSWORD");
alertDialog.setMessage("Enter Password");
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input);
alertDialog.setIcon(R.drawable.key);
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
password = input.getText().toString();
if (password.compareTo("") == 0) {
if (pass.equals(password)) {
Toast.makeText(getApplicationContext(),
"Password Matched", Toast.LENGTH_SHORT).show();
Intent myIntent1 = new Intent(view.getContext(),
Show.class);
startActivityForResult(myIntent1, 0);
} else {
Toast.makeText(getApplicationContext(),
"Wrong Password!", Toast.LENGTH_SHORT).show();
}
}
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
Aceptado
Sé que es demasiado tarde para responder esta pregunta, pero para otros que estén buscando algo similar a esto, aquí hay un código simple de un cuadro de alerta con un texto de edición.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
o
new AlertDialog.Builder(mContext, R.style.MyCustomDialogTheme);
si desea cambiar el tema del diálogo.
final EditText edittext = new EditText(ActivityContext);
alert.setMessage("Enter Your Message");
alert.setTitle("Enter Your Title");
alert.setView(edittext);
alert.setPositiveButton("Yes Option", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//What ever you want to do with the value
Editable YouEditTextValue = edittext.getText();
//OR
String YouEditTextValue = edittext.getText().toString();
}
});
alert.setNegativeButton("No Option", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
alert.show();
Usar contexto de actividad
Reemplace esto
final EditText input = new EditText(this);
Por
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input); // uncomment this line