Android:带有自定义对话框和中性按钮的警报对话框

标签 android android-widget android-alertdialog android-theme

我正在编辑我的问题以便更好地理解我的需要,因为下面的答案没有给我适当的解决方案。所以,我要做的是制作一个底部带有中性按钮的自定义警报对话框。这是它应该是什么样子的例子:

I have to make a dialog which looks like this

到目前为止,我已经使用了使用 xml 的自定义对话框并将 Activity 更改为 android:theme="@android:style/Theme.Holo.Light.Dialog",因此我可以了解外观像一样。这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/login_prompt_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="@string/login_prompt_rewards_header"
        android:textSize="17sp"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/login_prompt_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_prompt_header"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical|center_horizontal"
        android:paddingLeft="18dp"
        android:paddingRight="18dp"
        android:text="@string/login_prompt_rewards_text"
        android:textSize="15sp" >
    </TextView>

    <Button
        android:id="@+id/log_in"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_prompt_text"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:background="@drawable/log_in"
        android:contentDescription="@string/none" >
    </Button>

    <Button
        android:id="@+id/create_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/log_in"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:background="@drawable/create_account"
        android:contentDescription="@string/none" >
    </Button>

</RelativeLayout>

通过上面的布局我得到以下结果:

Till now implemented

我面临的问题是:如何在自定义对话框中设置中性按钮?我对此一无所知。如果您对我的问题有疑问或您听不懂语言,请发表评论,以便我再次给您一个清晰的思路。我们将不胜感激。

最佳答案

像这样创建自定义警报对话框

public void messageDialog(String title, String message, final Context activity) {

        final Dialog myDialog = new Dialog(activity);
        myDialog.setContentView(R.layout.messagescreen);
        myDialog.setTitle(title);
        myDialog.setCancelable(false);

        TextView text = (TextView) myDialog.findViewById(R.id.bidmessage);
        text.setMovementMethod(ScrollingMovementMethod.getInstance());
        text.setText(message);

        Button login = (Button) myDialog.findViewById(R.id.buttonlogin);
        login.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                myDialog.dismiss();


            }
        });

        Button createAccount= (Button) myDialog.findViewById(R.id.buttoncreateaccount);
        createAccount.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                myDialog.dismiss();


            }
        });


        myDialog.show();

    }

其中 R.layout.messagescreen 是您在图片中显示的自定义布局。试试这个,如果您遇到任何问题,请告诉我。

关于Android:带有自定义对话框和中性按钮的警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874868/

相关文章:

android - 在 Android 的网格中随机设置图像

android - 当应用程序进入前台时,保持 EditText 的键盘打开/关闭状态

android - 警报对话框主题

android - 为什么 AlertDialog.Builder 没有 setOnDismissListener 或 setOnShowListener 方法?

android - 有一种方法可以在 BottomSheetDialogFragment 中使用 AlertDialog 吗?

android - 在 Android 中创建菜单的正确方法是什么?

android - 膨胀类 android.support.v7.widget.CardView ClassNotFoundException 时出错

Flutter 如何使用列表小部件进行内部 for 循环?

android - 带有自定义适配器的微调器不会在选择时消失

android - 使用 ssl 时 POCO 库卡在 Android 上