2012-10-13 42 views
34

Tôi đang làm việc trên một dự án cho trường đại học cho phép người dùng đặt một điểm trên bản đồ và sau đó đặt tiêu đề và mô tả cho đối tượng lớp phủ. Vấn đề là, hộp thứ hai EditText ghi đè lên hộp đầu tiên. Đây là mã của tôi cho hộp thoại.Nhiều đối tượng EditText trong 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); 

Mọi trợ giúp sẽ được đánh giá cao !! Cảm ơn!

Trả lời

76

Hộp thoại chỉ chứa một chế độ xem gốc, đó là lý do tại sao setView() ghi đè EditText đầu tiên. Giải pháp là đơn giản đặt mọi thứ trong một ViewGroup, ví dụ một 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 

(Đây là một ví dụ cơ bản, nhưng nó sẽ giúp bạn bắt đầu.)

Bạn nên lưu ý về sự khác biệt danh pháp giữa phương thức setadd. setView() chỉ giữ một Chế độ xem, điều này tương tự với setMessage(). Trên thực tế, điều này đúng với mọi phương thức set, những gì bạn đang nghĩ đến là các lệnh add. add phương pháp được tích lũy, họ xây dựng một danh sách tất cả mọi thứ bạn đẩy trong khi phương pháp set là số ít, họ thay thế các dữ liệu hiện có.

+0

Ah đó là rực rỡ cảm ơn bạn , chính xác những gì tôi đang tìm kiếm, một câu hỏi, có cách nào để viết lên trên ea không ch hộp văn bản? Giống như "Tiêu đề:" thì "Mô tả:" ?? – TomSelleck

+1

@Tomcelic có bạn có thể làm điều đó, chỉ cần thêm vào thứ tự một TextView, EditText, TextViewand cuối cùng là EditText, nhưng tôi nghĩ rằng việc sử dụng layoutInflator sẽ tốt hơn –

+0

Âm thanh, cảm ơn sự giúp đỡ của bạn! – TomSelleck

7

Bạn có thể tạo bố cục chứa hai EditText, thổi phồng nó bằng LayoutInflater và sử dụng bố cục đó làm Chế độ xem của 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

bạn có thể thêm EditText bạn lập trình quá như thế này:

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

Mã cho tạo một popup với hai EditText sử dụng 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(); 

    } 
Các vấn đề liên quan