19

私はアンドロイドが初めてです。

AlertDialog現在、「OK」と「キャンセル」ボタンのあるボックスを表示したいと考えています。

デフォルトは PositiveButton: Left、NegativeButton: Right です。

PositiveButton を右側に、NegativeButton を左側に移動する方法を教えてください。

「OK」を「NegativeButton」に、「Cancel」を「PositiveButton」に変更すると、OK を押したときに Negativebutton が悪い音を出す可能性や問題はありますか。

私のコード:

AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
                    builder.setMessage("Confirmation?")
                    .setCancelable(false)
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //Todo
                            dialog.cancel();
                        }
                    })
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                        }
                    })

dialog = builder.create();

ありがとう、天使

4

7 に答える 7

39

これは直接的な答えではないかもしれません。しかし、関連トピックに関するいくつかの情報。Google 自身のフォーラムのこのスレッドから、ロマンの男は言った..

今後、ポジティブ/ネガティブ ボタンの順序は ICS で使用されるものになります。

OSバージョンごとの規則は

  • Honeycomb より前のデバイスでは、ボタンの順序 (左から右) は、ポジティブ - ニュートラル - ネガティブでした。
  • Holo テーマを使用する新しいデバイスでは、ボタンの順序 (左から右) が NEGATIVE - NEUTRAL - POSITIVE になりました。

Android/Google が従いたい規則である場合は、ユーザーがアプリを単独で使用することはないため、同じ規則に従う方がよいのではないでしょうか。結局、開発者が最初に求めるのは使いやすさです..

于 2012-11-30T11:42:07.373 に答える
3
AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
                builder.setMessage("Confirmation?")
                    .setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                        }
                    })
                    .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                            dialog.cancel();
                        }
                    })


                diaglog = builder.create();

ただし、順序を変更する正当な理由がない限り、規則に従うことをお勧めします。これにより、ユーザーはアプリケーションを使いやすくなります。

于 2012-11-30T11:37:54.873 に答える
0

これは最もエレガントな方法ではありませんが、あなたが望むことをします

AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
            builder.setMessage("Confirmation?")
                .setCancelable(false)
                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //TOdo
                        dialog.cancel();
                    }
                })
                .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //TOdo
                    }
                })


            diaglog = builder.create();

ボタンを正CancelOkボタンを負にするだけです。

于 2012-11-30T11:38:33.553 に答える
0

Android で diffault 設定を変更する方法はありませんが、テキストを変更して、これに応じて機能的に設定をキャンセルすることができます。

AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
    builder.setMessage("Confirmation?")
           .setCancelable(false)
           .setNegativeButton("OK", 
               new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                        }
                   })
           .setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                            dialog.cancel();
                        }
                    });


dialog = builder.create();
于 2012-11-30T11:40:46.263 に答える
0

のボタンをAlertDialog右側に移動する非常に簡単な方法は、デフォルトのレイアウトを処理するコア XML 内のを非表示leftSpacerにすることです。LinearLayout

// Fetch the PositiveButton
final Button       lPositiveButton = lDialog.getButton(AlertDialog.BUTTON_POSITIVE);
// Fetch the LinearLayout.
final LinearLayout lParent         = (LinearLayout) lPositiveButton.getParent();
// Ensure the Parent of the Buttons aligns it's contents to the right.
lParent.setGravity(Gravity.RIGHT);
// Hide the LeftSpacer. (Strict dependence on the order of the layout!)
lParent.getChildAt(1).setVisibility(View.GONE);
于 2017-03-03T09:01:30.070 に答える
0

これをチェックしてください https://github.com/hslls/order-alert-buttons

dependencies {
    compile 'com.github.hslls:order-alert-buttons:1.0.0'
}


AlertDialogParams params = new AlertDialogParams();
params.mTitle = "title";
params.mMessage = "message";
params.mPositiveText = "Ok";
params.mNegativeText = "Cancel";
params.mCancelable = true;
params.mAlign = BUTTON_ALIGN.POSITIVE_BUTTON_LEFT;  // fix button position
params.mClickListener = new AlertDialogClickListener() {
    @Override
    public void onPositiveClicked() {

    }

    @Override
    public void onNegativeClicked() {

    }
};

AlertDialog dialog = AlertDialogBuilder.createAlertDialog(this, params);
于 2016-02-10T09:31:05.007 に答える