Android: Establecer margen para editar texto en el cuadro AlertDialog
Estoy tratando de crear un cuadro de diálogo Alerta como Lollipop , todo va bien, pero estoy atrapado en una sección el caso de EditText
Quiero un EditText con subrayado y margen izquierdo y derecho con 20dp.Para subrayar intenté setBackground () , y su funcionamiento bien.
- Resaltar texto en varios controles EditText simultáneamente
- Evite que EditText consuma un evento de clic
- Teclado Android oscurece EditText
- Estilo EditarTexto de contenido 'sobre la marcha'?
- Cómo obtener la posición del carácter seleccionado o String en EditText
Pero hay un problema que setBackground () no funcionará nivel API por debajo de 16.
Para setMargin lo intenté
final EditText input = new EditText(MainActivity.this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(30,0,30,0); input.setLayoutParams(lp); input.setGravity(View.TEXT_ALIGNMENT_GRAVITY); input.setBackground(getResources().getDrawable(R.drawable.edit_text_line)); builder.setView(input);
Pero Editar texto utilizando el ancho completo de los padres.
Código Completo
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Message"); builder.setMessage("Do you want to\n"+""+"exit from app"); final EditText input = new EditText(MainActivity.this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(30,0,30,0); input.setLayoutParams(lp); input.setGravity(View.TEXT_ALIGNMENT_GRAVITY); input.setBackground(getResources().getDrawable(R.drawable.edit_text_line)); //call reequires api 16 and above builder.setView(input); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(), Toast.LENGTH_LONG).show(); } }); AlertDialog alert = builder.create(); alert.show(); Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE); nbutton.setTextColor(Color.parseColor("#7e7e7e")); Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE); pbutton.setTextColor(Color.parseColor("#109c8f"));
¿Hay alguna manera de establecer el fondo para el EditText que trabajan debajo de API 16 y setMargin izquierda y derecha para el EditText .
- Androide. OnEditorAction nunca llamado
- Androide modificar el texto EditarTexto
- Agregue ID de programación a R.id
- Cambiar el color de línea inferior de EditText con appcompat v7
- Cómo cambiar el color de los controladores de EditText?
- Barra de acción de corte / copia personalizada para EditText que muestra las manijas de selección de texto
- Cómo crear una preferencia que acepte sólo valores enteros
- Editar el foco de texto en android
Los márgenes en la vista de raíz no funcionarán. Trate de añadir relleno a la disposición de los padres como se indica en otras respuestas.
Pero en lugar de crear el diseño de diálogo en java, le aconsejo inflar un xml y utilizar AppCompatEditText si desea utilizar línea de fondo
He aquí un código de ejemplo para usted
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Message"); // Why are you setting message here when you are inflating custom view? // You need to add another TextView in xml if you want to set message here // Otherwise the message will not be shown // builder.setMessage("Do you want to\n"+""+"exit from app"); View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null); final AppCompatEditText input = (AppCompatEditText) view.findViewById(R.id.editText); builder.setView(view); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(), Toast.LENGTH_LONG).show(); } }); AlertDialog alert = builder.create(); alert.show();
Por último, no puede obtener botones al instante después de crear el diálogo. Necesita hacer esto en OnShowListener si desea personalizar colores de texto de botón. O use android.support.v7.app.AlertDialog
para los diseños de diálogo más recientes.
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE); // will be null // nbutton.setTextColor(Color.parseColor("#7e7e7e")); Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE); // will be null // pbutton.setTextColor(Color.parseColor("#109c8f"));
Dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.AppCompatEditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_margin="16dp" app:backgroundTint="@color/colorPrimary"/> </LinearLayout>
Utilice este código que funciona para mí.
final EditText input = new EditText(MainActivity.this); input.setSingleLine(); FrameLayout container = new FrameLayout(thisActivity); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.topMargin = convertDpToPx(30); params.bottomMargin = convertDpToPx(30); input.setLayoutParams(params); container.addView(input);