Ich habe einen Dialog erstellt, der ein benutzerdefiniertes DialogFragment und Interface verwendet sowie ein benutzerdefiniertes Layout, das die Klickereignisse an den Dialoghost zurückgibt (MainActivity). Mein Problem ist jedoch, dass wenn ich versuche, den Text im EditText im Dialog einzugeben, er als leere Zeichenfolge zurückkommt. Irgendwelche Ideen? Ich kann den Text von den TextViews innerhalb des Dialogs bekommen, nur nicht den EditText.Kann Text aus dem Dialog textview holen, kann aber keinen Text aus dem Dialog erhalten edittext
add_class_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/viewClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class: "
android:textSize="25sp"
android:textColor="@color/textColor"/>
<EditText
android:id="@+id/editClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter class"
android:textSize="25sp"
android:textColor="@color/textColor"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
TextView nameView;
EditText editName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null);
nameView = vi.findViewById(R.id.viewClassName);
editName = vi.findViewById(R.id.editClassCredits);
}
public void addClass(View view) {
AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getFragmentManager(), "ac");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
}
AddClassDialogFragment.java:
public class AddClassDialogFragment extends DialogFragment {
public interface AddClassDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
AddClassDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AddClassDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_class_dialog, null));
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClick(AddClassDialogFragment.this);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(AddClassDialogFragment.this);
}
});
return builder.create();
}
}
Sie falsch id sind reffering, sollte diese verwenden ** ** editClassName während findViewById –