21

ボタンを押したときにカスタムダイアログを閉じようとしています

        //set up dialog
        Dialog dialog = new Dialog(BrowseActivity.this);
        dialog.setContentView(R.layout.about);
        dialog.setTitle("This is my custom dialog box");
        dialog.setCancelable(true);
        //there are a lot of settings, for dialog, check them all out!

        //set up text
        TextView text = (TextView) dialog.findViewById(R.id.TextView01);
        text.setText(R.string.app_help_message);

        //set up image view
        ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
        img.setImageResource(R.drawable.icon);

      //set up button
        Button button = (Button) dialog.findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
            Dialog.dismiss();

            }
        });

        //now that the dialog is set up, it's time to show it    
        dialog.show();

       return true;

dialog.dismiss が機能しません。このカスタム ダイアログをヘルプ画面として使用しようとしているだけで、ボタンを押して閉じたいと思っています。

私はAndroid開発に非常に慣れていませんが、これを何時間も試してきました

アドバイスをありがとう

4

2 に答える 2

34
final Dialog dialog = new Dialog(BrowseActivity.this);

小文字のダイアログが必要です。

public void onClick(View v) {
   dialog.dismiss();
}

またAlertDialog.Builder、あなたにとってより良い選択かもしれません。

于 2011-06-06T21:24:06.520 に答える
3

Dismiss(); を呼び出すことができます。ダイアログで。これは私にとってはうまくいきます。

于 2014-04-16T10:33:26.980 に答える