Help us understand the problem. What is going on with this article?

【Android】Dialog Fragmentの作り方

HogeActivity.java
public class HogeActivity extends AppCompatActivity  {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button = findViewById(R.id.huga_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //ダイアログを表示する
              DialogFragment dialog = new DialogFragment();
              // 注意: getFragmentManager()を使用するとエラーになる
              dialog.show(getSupportFragmentManager(),"sample");
            }
        });
  }
}
DialogFragment.java
public class BookReviewDialogFragment extends DialogFragment {
    // ダイアログが生成された時に呼ばれるメソッド
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // ダイアログ生成  AlertDialogのBuilderクラスを指定してインスタンス化します
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
        // タイトル設定
        dialogBuilder.setTitle("ダイアログタイトル");
        // 表示する文章設定
        dialogBuilder.setMessage("値を入力してください");
        // 入力フィールド作成
        final EditText editText = new EditText(getActivity());
        dialogBuilder.setView(editText);
        // OKボタン作成
        dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // editTextから値を取得
                String returnValue = editText.getText().toString();
                // HogeActivityのインスタンスを取得
                HogeActivity hogeActivity = (HogeActivity) getActivity();
                hogeActivity.setTextView(returnValue);
            }
        });
        // NGボタン作成
        dialogBuilder.setNegativeButton("NG", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 何もしないで閉じる
            }
        });
        // dialogBulderを返す
        return dialogBuilder.create();
    }
}

■ ダイアログフラグメントのOKボタンの文字を@stringsから参照する

Resources res = getResources();
// OKボタン作成 dialogBuilder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away