0

ダイアログボックスを何度も表示する必要があるアプリケーションを作成していますが、ダイアログボックスのコードを何度も書きたくないので、はいを選択するとさまざまな機能を実行できるように、共通のダイアログボックスを作成するにはどうすればよいですかまたは別の場所でいいえ。助けてください、私はたくさん試しましたが、解決策がありませんでした.

4

5 に答える 5

2

次のようなメソッドを作成できます。

public Dialog showDialog(String title, String msg, final Activity activity) {

        final AlertDialog alertDialog = new AlertDialog.Builder(activity)
                .create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(msg);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();

                    activity.finish();

            }
        });
        alertDialog.show();

        return alertDialog;

    }

そして、ダイアログを作成したい場所ならどこでも呼び出すだけですshowDialog("Title","your message",Acitivity);

于 2013-09-19T10:03:44.897 に答える
0

Dialog/AlertDialog または必要なものから拡張されたクラスを作成してみてください。次に、カスタム ダイアログ ボックスのいくつかの機能を実装できます。カスタム DialogBox を使用する必要がある場合に単純にオーバーライドできる yesPerformed() および noPerformed() メソッドを作成できます。

于 2013-09-19T10:02:59.130 に答える
0

プロジェクトで行ったように、既製のコードをここに貼り付けています。それに応じて別のタグを変更するか、さらに助けを求めてください!

  • 次のダイアログは、Android v4 サポート ライブラリを使用して 1.6 との下位互換性を持たせることができます。
  • ダイアログは、デバイスの回転時にそれ自体を閉じません。

ダイアログは次のようになります。 ここに画像の説明を入力

ステップ 1: 名前で新しいクラスを作成しConfirmationDialog、次のコードを貼り付けます。

public class ConfirmationDialog extends DialogFragment {

    private Handler mHandler;
private Bundle mBundle;
private String messageToShow;
private String dialogTitle;

public ConfirmationDialog(Handler mHandler, String dialogTitle,
        String messageToShow) {
    this.mHandler = mHandler;
    mBundle = new Bundle();
    this.messageToShow = messageToShow;
    this.dialogTitle = dialogTitle;
    Log.i(MyConstants.LOG_TAG,
            "------------CONSTRUCTOR EXECUTED------------");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    Log.i(MyConstants.LOG_TAG, "------------ONCREATE EXECUTED------------");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(dialogTitle);
    builder.setIcon(R.drawable.ic_alerts_and_states_warning);
    builder.setMessage(messageToShow);
    builder.setPositiveButton(getString(R.string.ok),
            new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mBundle.clear();
                    mBundle.putInt(MyConstants.CONFIRM_NOTE_DELETE_TAG,
                            MyConstants.DIALOG_OK);
                    Message m = new Message();
                    m.setData(mBundle);
                    mHandler.sendMessage(m);
                }
            });

    builder.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mBundle.clear();
            mBundle.putInt(MyConstants.CONFIRM_NOTE_DELETE_TAG,
                    MyConstants.DIALOG_CANCEL);
            Message m = new Message();
            m.setData(mBundle);
            mHandler.sendMessage(m);
        }
    });
    Dialog theDialog = builder.create();
    theDialog.setCanceledOnTouchOutside(true);
    return theDialog;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance()) {
        getDialog().setDismissMessage(null);
    }
    super.onDestroyView();
    }
}

次に、ダイアログを表示するフラグメントに次のコードを貼り付けます。

Handler mHandler = new Handler() {
    public void handleMessage(Message m) {
        Bundle mBundle = new Bundle();
        mBundle = m.getData();
        Log.i(MyConstants.LOG_TAG, "Got the data returned by the Dialog!");

        int dialogOkCancel = MyConstants.DIALOG_CANCEL;
        if (!mBundle.isEmpty())
            dialogOkCancel = mBundle.getInt(
                    MyConstants.CONFIRM_NOTE_DELETE_TAG,
                    MyConstants.DIALOG_CANCEL);
        if (dialogOkCancel == MyConstants.DIALOG_OK) {
            Log.i(MyConstants.LOG_TAG, "Ok selected");              
        } else if (dialogOkCancel == MyConstants.DIALOG_CANCEL) {
            Log.i(MyConstants.LOG_TAG, "Cancel selected");
        }
    } // end handleMessage
};

ステップ 3: ダイアログを表示するには、次のコードを実行します。

ConfirmationDialog mConfirmationDialog = new ConfirmationDialog(
                        mHandler, "Delete Routine Task",
                        "Are you sure to delete Routine Task?");
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.add(mConfirmationDialog,
                        MyConstants.CONFIRM_NOTE_DELETE_TAG);
                fragmentTransaction.commit();
于 2013-09-19T10:10:46.823 に答える
0

ダイアログ ボックスをパブリックとして宣言する

        public Dialog dialog_box;

それを初期化します.... oncreateで...

    dialog_box = new Dialog(HSMPDFViewer.this,android.R.style.Theme_Translucent);
    dialog_box.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog_box.setCancelable(true);

そのための関数を作成し、それを使用します...

     /**
 * Alert Dialog 
 */
public void AlertDialogMessage() {
    dialog_text.setText("Problem in loading ..");
    dialog_btnOk.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            dialog_box.dismiss();
        }
    });
    dialog_box.show();

}
于 2013-09-19T10:01:43.200 に答える