2017-06-14 113 views
0

我想用的按鈕,看起來像這樣創建一個AlertDialog:https://developer.android.com/images/ui/dialogs_regions.pngAndroid - 如何更改AlertDialog中按鈕的外觀?

但是,每當我創建了一個AlertDialog.Builder AlertDialog,我最終得到的按鈕,看起來像這樣:https://developer.android.com/images/ui/dialog_buttons.png

哪有我改變AlertDialog,使按鈕看起來像第一個例子(即佔用整個對話框的底部,每個按鈕之間有灰色的分隔線)?請注意,我並不試圖改變對話框窗口的顏色,只是按鈕出現在窗口上。理想情況下,我希望只使用默認的Android樣式並且不定義自定義樣式,這是可能的嗎?

下面是我用它來創建和顯示AlertDialog代碼:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

alertDialogBuilder.setTitle("Include answers in summary?"); 
alertDialogBuilder.setMessage("You have completed " + String.valueOf(questionsCompleted) + " out of 18 questions. Would you like the summary to include these answers along with the questions?"); 
alertDialogBuilder.setCancelable(false); 

alertDialogBuilder.setPositiveButton("Include", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     Intent intent = new Intent(getApplicationContext(), SummaryActivity.class); 
     intent.putExtra("componentNumber", 0); 
     intent.putExtra("includeAnswers", true); 
     startActivity(intent); 
     } 
    }); 

alertDialogBuilder.setNegativeButton("Omit", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     Intent intent = new Intent(getApplicationContext(), SummaryActivity.class); 
     intent.putExtra("componentNumber", 0); 
     intent.putExtra("includeAnswers", false); 
     startActivity(intent); 
    } 
}); 

alertDialogBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
    } 
}); 

AlertDialog alertDialog = alertDialogBuilder.create(); 

alertDialog.show(); 

有這AlertDialog三個動作。在不太瞭解細節的情況下,其中兩個按鈕可讓用戶參與統計問卷的相同活動,但無論是否包含答案,其他按鈕都會取消對話。我知道我應該使用NegativeButton來取消對話框,但即使Android開發人員指南顯示對話框中的正面和負面按鈕之間會出現一箇中性按鈕,但我已經獲得了Neutral-> Negative- >正。因此,我一直在使用左側的省略選項和右側的取消和包含選項進行對話,這對我來說是非常不直觀的。

我想將按鈕的順序改爲Negative-> Neutral-> Positive,並將負面按鈕定義爲取消對話框,以便將導致摘要活動的兩個按鈕組合在一起 - 這是在所有可能的?

+0

爲什麼不更換代碼正確的順序?首先將您想要的方法放入先出現的按鈕並設置相關的按鈕文本。用戶不知道它是否爲中性,正面或負面的按鈕。 – Opiatefuchs

+0

,如果您想要一個符合您需要的對話框,只需創建一個自定義對話框。你可以像做活動一樣進行佈局... https://www.mkyong.com/android/android-custom-dialog-example/ – Opiatefuchs

+0

我試着重新排列代碼中的按鈕分配,但它沒有效果。我擔心使用中性按鈕進行取消操作的原因是,我擔心不同的手機可能會顯示不同的按鈕,因此用戶在實際嘗試取消對話時可能會輸入一個活動。不過,我想用一個自定義對話框可以解決這個問題,所以我會給它一個鏡頭 - 感謝教程鏈接。 –

回答

0

我相信我的回答對您最有幫助。只需爲您的對話框創建一個佈局,就像下面的對話框一樣,我相信這正是您想要的。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:divider="?android:dividerVertical" 
     android:showDividers="middle"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Are you sure?" 
    android:textStyle="bold" 
    android:textSize="20sp" 
    android:gravity="center" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:id="@+id/dialog_text"/> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    style="?android:buttonBarStyle" 
    android:divider="?android:dividerHorizontal" 
    android:showDividers="middle"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/cancel_action" 
     android:layout_weight="1" 
     android:text="Cancel" 
     android:textAllCaps="false" 
     style="?android:buttonBarButtonStyle"/> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/ok_action" 
     android:layout_weight="1" 
     android:text="Ok" 
     android:textAllCaps="false" 
     style="?android:buttonBarButtonStyle"/> 
</LinearLayout> 

</LinearLayout> 

然後設置你的對話框的代碼佈局:

final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); 
    View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout, null); 
    dialog.setView(view); 
    final AlertDialog alert = dialog.create(); 
    Button cancel = (Button) view.findViewById(R.id.cancel_action); 
    cancel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      alert.dismiss(); 
     } 
    }); 
    Button ok = (Button) view.findViewById(R.id.ok_action); 
    ok.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //whatever you want 

     } 
    }); 
    alert.show(); 
+0

這正是我想到的!我將以此爲基礎製作兩個按鈕和三個按鈕的版本,以便我可以將佈局應用於應用中的每個對話框。萬分感謝! –

+0

作爲一個最後的快速問題,有沒有一種方法可以創建一個類似於這樣的對話框,在標題和消息之間沒有分隔線?換句話說,我想保留分隔線,因爲它們位於按鈕和消息區域之間,但刪除標題區域和消息區域之間的線條。 –

+0

對不起,我只注意到一件事。當我按下其中一個按鈕將我帶到下一個活動時,然後按該活動中的後退按鈕,當我返回到原始活動時,對話窗口仍然打開。有沒有辦法在按下這些按鈕時強制關閉對話窗口,或重新加載原始視圖時?我以前使用的默認對話窗口沒有出現這種行爲,並且每當我返回到它的活動時都會關閉,所以我有點難以確定如何解決這個問題。 –

0

AlertDialog按鈕的外觀基於各自的Android API版本,所以您想要的(基於您的第一個屏幕快照)是Android 4.4.2(KitKat)中的AlertDialog,具有Theme.AppCompat應用程序主題 - d知道這一點,因爲這就是它出現在我的平板電腦4.4.2中的原因。例如: enter image description here

+0

這就是我擔心的情況。哦,我只是做一個像Opiatefuchs建議的自定義對話框。謝謝你清理那個,但! –

0

在不同或特定主題加載AlertDialog,你可以嘗試這樣的:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light_Dialog))); 

這可從API 1.

此外,關於按鈕序列,您可以切換您的代碼以獲得按鈕單擊所需的結果,而忽略按鈕類型。 類似於:

builder.setNeutralButton("NO",listener); 
builder.setNegativeButton("NOT SURE",listener); 
builder.setPositiveButton("YES",listener);