2012-09-03 9 views
28

He una butoon, al hacer clic en este botón, quiero abrir varios botones en una sola AlertDialog así: enter image description here¿Cómo añadir varios botones en una sola AlertDialog

Dame una ayuda:

I se utiliza este .... añadir varios botones alertDialog.setButton (eliminar, "borrar", nueva OnClickListener() {

  public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 

pero encontré ..., cambiar botón SET() para setButton2(). algo como ..... wt xcan puedo hacer por esto ....

+0

Make s diálogo personalizado y el archivo XML diseño infla para 'setView () ' Ahora agregue todos los botones que desee. – user370305

Respuesta

16

Inflaría el AlertDialog con mi propia vista personalizada (my_alert_dialog.xml).

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
LayoutInflater inflater = getLayoutInflater(); 
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..) 
View view = inflater.inflate(R.layout.my_alert_dialog, (ViewGroup) findViewById(R.id.root)); 

Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4. 
alert.setView(view); 
alert.show(); 
0
Dialog dialog = new Dialog(context); 
RelativeLayout featureLayout = (RelativeLayout) View.inflate(this, R.layout.yourview, null); 
dialog.setContentView(featureLayout); 
dialog.show(); 
88

Una solución sencilla sin xml:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
builder.setTitle("Title"); 
builder.setItems(new CharSequence[] 
     {"button 1", "button 2", "button 3", "button 4"}, 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // The 'which' argument contains the index position 
       // of the selected item 
       switch (which) { 
        case 0: 
         Toast.makeText(context, "clicked 1", Toast.LENGTH_SHORT).show(); 
         break; 
        case 1: 
         Toast.makeText(context, "clicked 2", Toast.LENGTH_SHORT).show(); 
         break; 
        case 2: 
         Toast.makeText(context, "clicked 3", Toast.LENGTH_SHORT).show(); 
         break; 
        case 3: 
         Toast.makeText(context, "clicked 4", Toast.LENGTH_SHORT).show(); 
         break; 
       } 
      } 
     }); 
builder.create().show(); 
+6

¡Muy buen ejemplo, y mucho más comprensible que los otros! ¡Gracias! –

+1

¡Esto funcionó para mí! Sin embargo, esto le da una lista vertical en lugar de una horizontal. – ymerdrengene

+1

Mejor que otras soluciones. – aeren

0
int item = 0; 
ArrayList<String> list = new ArrayList<String>(); 
ArrayList<Integer> intList = new ArrayList<Integer>(); 
list.add("A"); 
list.add("B"); 
list.add("C"); 
list.add("D"); 
item = -1; 

for (int i = 0; i < list.size(); i++) { 
    item++; 
    intList.add(i); 
    } 

showRatingBarAlertDialog(intList); 

private void showRatingBarAlertDialog(ArrayList<Integer> Id) { 
    if (item != -1) { 
     RatingFragment alertDialog = RatingFragment.instance(BaseActivity.this, Id.get(item), (ratingValue, comments) -> { 
      CXLog.d(TAG, "select the rating::" + ratingValue); 
      CXLog.d(TAG, "comment the feednback " + comments); 
      item--; 
      showRatingBarAlertDialog(requestId); 
     }); 
     alertDialog.show(CXBaseActivity.this.getFragmentManager(), "alertDialog"); 
    } 
} 
Cuestiones relacionadas