2017-06-14 212 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);