대화 상자 호스트 (MainActivity)로 다시 클릭 이벤트를 전달하는 사용자 정의 레이아웃뿐만 아니라 사용자 정의 DialogFragment 및 인터페이스를 사용하여 대화 상자를 만들었습니다. 그러나 내 문제는 대화 상자에서 EditText에 입력 된 텍스트를 가져 오려고하면 다시 빈 문자열로 나타납니다. 어떤 아이디어? TextView의 텍스트를 대화 상자에서 가져올 수 있습니다. EditText가 아닙니다.대화 상자 textview에서 텍스트를 가져올 수 있지만 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();
}
}
당신이 잘못된 ID를 참고로하는 것에있다 ** editClassName ** findViewById를 –