2016-04-13 22 views
2

ich die Schaffung nächste AlerDialog zu bekommen:Wie EditText von Fragment

AlertDialog.Builder alert = new AlertDialog.Builder(appContext); 

       alert.setTitle("Add subcontractors").setView(R.layout.add_subcontractor_form); 

       //final EditText input = new EditText(appContext); 

       alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         String YouEditTextValue = input.getText().toString(); 
        } 
       }); 

       alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // what ever you want to do with No option. 
        } 
       }); 

       alert.show(); 

Und haben neben Layout für sie:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
      <EditText 
      android:layout_margin="10dp" 
      android:id="@+id/et_sub_name" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:hint="Type name here" 
      android:textSize="20sp"/> 
</LinearLayout> 

Meine Frage ist, wie EditText Blick aus meinem Layout in Code zu bekommen? Weil ich einen eingegebenen Text haben möchte, nachdem der Benutzer die "OK" Taste gedrückt hat.

Antwort

3

versuchen, wie dies zu tun:

LayoutInflater inflater = getActivity().getLayoutInflater(); 
View dialogView = inflater.inflate(R.layout.add_subcontractor_form, null); 
alert.setView(dialogView); 

EditText editText = (EditText) dialogView.findViewById(R.id.et_sub_name); 
+0

Große, es funktioniert für mich. Vielen Dank – neustart47

1
AlertDialog.Builder alert = new AlertDialog.Builder(appContext); 
// inflate your view 
View inflatedView = LayoutInflator.from(appContext).inflate(R.layout.add_subcontractor_form) 
// find the edittext 
final EditText input = (EditText) inflatedView.findViewById(R.id. et_sub_name) 

alert.setTitle("Add subcontractors") 
    .setView(inflatedView); 

alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     String YouEditTextValue = input.getText().toString(); 
    } 
}); 

alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     // what ever you want to do with No option. 
    } 
}); 

alert.show(); 
Verwandte Themen