ユーザーに名前を尋ねているところを表示しようとしてDialogFragment
いますが、この文字列をメインのアクティビティに戻す必要があります。
Android ガイドに従ってカスタム ダイアログ フラグメントを作成しました: http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
私のコードでは、ユーザーが肯定的なボタンをクリックしたことに応じて、メイン アクティビティで関数を呼び出すことができます。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.textdialog, null)).setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int id) {
((mainActivity)getActivity()).doPositiveClick();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
((mainActivity)getActivity()).doNegativeClick();
}
});
return builder.create();
}
R.layout.textdialog は次のとおりです。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/textdraw"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="introduce text" />
</LinearLayout>
私の質問は、どうすればユーザーがダイアログ フラグメントに書いたテキストを取得し、それをメイン アクティビティに送り返すことができるかということです。
ありがとう!