2012-11-30 142 views
15

我是新來的android。Android AlertDialog將PositiveButton移動到右側並將NegativeButton移動到左側

目前我想用'OK'顯示AlertDialog框&'取消'按鈕。

默認爲PositiveButton:左,NegativeButton:右

你可以讓我知道我怎麼可以移動PositiveButton右側& NegativeButton到左側?

如果按下OK按鈕時Negativebutton導致聲音不好,是否有任何機會/故障?如果我們將文本「OK」改爲NegativeButton &「Cancel」爲PositiveButton。

我的代碼:

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(); 

感謝, 天使

回答

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(); 

但我建議,除非你有一個很好的理由來改變爲了與公約一起去。這將使用戶更容易使用您的應用程序。

+0

是否有任何機會/麻煩,如果Negativebutton造成不好的聲音按下OK的時候,如果我們改變文本「OK」來NegativeButton和「取消」 PositiveButton。 – user1866128

+0

這是可能的,但我從來沒有見過這樣的事情。 – Caner

-1

這還不是最優雅的方式,但它會做你想要

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(); 

只是使Cancel按鈕爲陽性什麼和Ok按鈕爲負。

+0

如果我們將文本「OK」改爲NegativeButton並將「取消」改爲PositiveButton,如果Negativebutton在按OK時導致聲音不好,那麼是否有任何機會/故障? – user1866128

0

有沒有辦法改變的Android 的diffault設置但你可以改變文本的確定CANCLE 設置功能accroding這

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(); 
+0

如果按下OK按鈕時Negativebutton導致聲音不好,那麼是否有任何機會/故障?如果我改變這樣的話 – user1866128

+0

沒有麻煩的機會。我在我的Android應用程序中使用這種類型 –

33

這可能不是一個直接的答案。但只是關於相關主題的一些信息。在谷歌自己的論壇this線程,羅曼蓋伊說..

展望未來的正/負鍵的順序將在ICS中使用的 。

和每個OS版本的約定是

  • 論到蜂窩現有裝置中,按鈕順序(從左到右)是 正 - NEUTRAL - NEGATIVE。
  • 在使用Holo主題的較新設備上,按鈕順序(左至 右)現在爲負 - 中性 - 正。

如果是Android/Google希望遵循的約定,那麼遵循相同的原則會更好,因爲您的用戶不會單獨使用您的應用程序。畢竟用戶友好是開發人員首先尋找的東西..

+3

這是自上述變通辦法以來唯一正確和有用的答案不要修復api 11及以上用戶的問題,這隻會導致混淆;] ... – reVerse

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); 
0

一個非常簡單的方式向AlertDialog的按鍵轉移到右手邊是隱藏leftSpacer,核心XML它處理的默認佈局內的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); 
0

我在buttonBarLayout其中按鈕想通了,存在neutral button-ve/+ve buttons與所述地點之間的空間佈局「1」。

所以,起初我們需要刪除空間,或使它的知名度GONEinvisible會讓它仍然需要一個空間,在buttonBarLayout)也是我們最好使用方法onShowListner更好,這樣做是顯示在對話框後:

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 
      Button neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
      LinearLayout view = (LinearLayout) neutralButtonOrAnyOtherBtnFromThe3Btns.getParent(); 
      Space space= (Space) view.getChildAt(1); 
     } 
}); 

那麼剩下的就是你的設計,希望可以幫助

相關問題