2012-10-13 14 views
34

Pracuję nad projektem dla uczelni, który pozwoli użytkownikowi umieścić punkt na mapie, a następnie ustawić tytuł i opis dla obiektu nakładki. Problem polega na tym, że drugie pole EditText nadpisuje pierwszy. Oto mój kod dla okna dialogowego.Wiele obiektów EditText w AlertDialog

//Make new Dialog 
AlertDialog.Builder dialog = new AlertDialog.Builder(mapView.getContext()); 
dialog.setTitle("Set Target Title & Description"); 
dialog.setMessage("Title: "); 

final EditText titleBox = new EditText(mapView.getContext()); 
dialog.setView(titleBox); 

dialog.setMessage("Description: "); 
final EditText descriptionBox = new EditText(mapView.getContext()); 
dialog.setView(descriptionBox); 

Każda pomoc zostanie doceniona !! Dzięki!

Odpowiedz

76

Okno dialogowe zawiera tylko jeden widok główny, dlatego setView() zastępuje pierwszy EditText. Rozwiązanie jest proste umieścić wszystko w jednym ViewGroup, przykładowo LinearLayout:

Context context = mapView.getContext(); 
LinearLayout layout = new LinearLayout(context); 
layout.setOrientation(LinearLayout.VERTICAL); 

// Add a TextView here for the "Title" label, as noted in the comments 
final EditText titleBox = new EditText(context); 
titleBox.setHint("Title"); 
layout.addView(titleBox); // Notice this is an add method 

// Add another TextView here for the "Description" label 
final EditText descriptionBox = new EditText(context); 
descriptionBox.setHint("Description"); 
layout.addView(descriptionBox); // Another add method 

dialog.setView(layout); // Again this is a set method, not add 

(jest to prosty przykład, ale powinno Ci zacząć.)

należy wziąć pod uwagę różnicę nomenklatury między metoda set i add. setView() zawiera tylko jeden widok, to samo jest podobne dla setMessage(). W rzeczywistości powinno to być prawdziwe dla każdej metody set, o której myślisz, są polecenia add. add metody są kumulatywne, tworzą listę wszystkiego, co wpychasz, podczas gdy metody set są w liczbie pojedynczej, zastępują istniejące dane.

+0

Ach to genialny dziękuję , dokładnie to, czego szukałem, jedno pytanie, czy jest jakikolwiek sposób, aby napisać powyżej ea ch pole tekstowe? Jak "Tytuł:", a następnie "Opis:" ?? – TomSelleck

+1

@Tomcelic tak, możesz to zrobić, dodaj kolejno TextView, EditText, TextView i wreszcie EditText, ale myślę, że użycie layoutInflator byłoby lepsze –

+0

Dźwięk, dziękuję za pomoc! – TomSelleck

7

Możesz zbudować swój układ zawierający dwa EditText, nadmuchać go za pomocą LayoutInflater i użyć go jako Widok swojego AlertDialog.

LayoutInflater factory = LayoutInflater.from(this); 

//text_entry is an Layout XML file containing two text field to display in alert dialog 
final View textEntryView = factory.inflate(R.layout.text_entry, null); 

final EditText input1 = (EditText) textEntryView.findViewById(R.id.EditText1); 
final EditText input2 = (EditText) textEntryView.findViewById(R.id.EditText2); 


input1.setText("DefaultValue", TextView.BufferType.EDITABLE); 
input2.setText("DefaultValue", TextView.BufferType.EDITABLE); 

final AlertDialog.Builder alert = new AlertDialog.Builder(this); 
alert.setIcon(R.drawable.icon).setTitle("EntertheText:").setView(textEntryView).setPositiveButton("Save", 
    new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, 
    int whichButton) { 

    Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString()); 
    Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString()); 
    /* User clicked OK so do some stuff */ 
    } 
    }).setNegativeButton("Cancel", 
    new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, 
    int whichButton) { 
    /* 
    * User clicked cancel so do some stuff 
    */ 
    } 
    }); 
alert.show(); 

enter image description here

można dodać EditText programowo też tak:

LinearLayout layout = new LinearLayout(mapView.getContext()); 
layout.setOrientation(LinearLayout.VERTICAL); 

final EditText titleBox = new EditText(mapView.getContext()); 
titleBox.setHint("Title"); 
layout.addView(titleBox); 

final EditText descriptionBox = new EditText(mapView.getContext()); 
descriptionBox.setHint("Description"); 
layout.addView(descriptionBox); 

dialog.setView(layout); 
1

Kod stworzyć okienko z dwoma EditText użyciu Xamarin

public void dial() 
    { 

     AlertDialog alerta = new AlertDialog.Builder(this).Create(); 
     LinearLayout layout = new LinearLayout(this); 

        layout.Orientation = Orientation.Vertical; 


     EditText factinput = new EditText(this); 
     alerta.SetMessage("Facturas Disponibles:"); 
     layout.AddView(factinput); 

     EditText canttinput = new EditText(this); 
     alerta.SetMessage("Cantidad:"); 
     layout.AddView(canttinput); 

     alerta.SetView(layout); 


     alerta.SetButton("Cancelar", (a, b) => 
     { 

      AlertDialog cencelacion = new AlertDialog.Builder(this).Create(); 
      cencelacion.SetMessage("Desea Cancelar"); 
      cencelacion.SetButton("OK", (c, d) => { }); 
      cencelacion.Show(); 

     }); 
     alerta.SetButton2("Aceptar", (ee, f) => 
     { 
      AlertDialog confirmacion = new AlertDialog.Builder(this).Create(); 
      confirmacion.SetMessage("Realizar Busqueda de Factura"); 
      confirmacion.SetButton("OK", (c, d) => { }); 
      confirmacion.Show(); 
     } 
     ); 

     alerta.Show(); 

    } 
Powiązane problemy