【问题标题】:Android: Alert dialog with custom dialog and neutral buttonAndroid:带有自定义对话框和中性按钮的警报对话框
【发布时间】:2013-02-14 12:27:04
【问题描述】:

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

到目前为止,我已经使用了使用 xml 的自定义对话框并将活动更改为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>

通过上述布局,我得到以下结果:

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

【问题讨论】:

  • 您如何将按钮设置为绿色并使其看起来像 3D?
  • @SiKni8 我为此使用了自定义可绘制对象。

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


【解决方案1】:

创建自定义警报对话框,如

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 是您在图像中显示的自定义创建的布局。试试这个,如果您遇到任何问题,请告诉我。

【讨论】:

  • 这个解决方案对我没有帮助。我还想要一个中性按钮,并为警报对话框使用自定义布局。你对此有什么想法吗?
  • 好朋友,您可以在此警报对话框中放置尽可能多的按钮。这些按钮可以在您的布局中实现
  • 请查看已编辑的问题,我在问什么。顺便说一句,感谢您的努力朋友。
  • 朋友在使用自定义时不能使用中性按钮等默认功能。您可以为此创建自己的视图。但是根据您的问题自定义警报对话框是好的。谢谢
  • 那么,现在我如何使用“跳过”按钮,使用相同的 xml 布局?
【解决方案2】:

Anupam,我知道你已经选择了一个答案,但我可能会在我如何做类似的事情上加上我的 2 美分。

我使用了 AlertDialog 而不是 Dialog,它在调用时浮动在应用程序上方。

private void showLoginDialog() {        
    // layout and inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View content = inflater.inflate(R.layout.dialog_settings, null);

    AlertDialog.Builder dialog = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light));
    dialog.setTitle(getString(R.string.title_settings_dialog));
    dialog.setView(content);

    Button butLogin = (Button) content.findViewById(R.id.log_in);
    butLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do your Login stuff here or all another method
            // does not close dialog
        }
    });

    Button butAccount = (Button) content.findViewById(R.id.create_account);
    butAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do your Create Account stuff here or all another method
            // does not close dialog
        }
    });

    dialog.setNegativeButton(R.string.skip, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // do nothing, just allow dialog to close
        }
    });

    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            // do nothing, just allow dialog to close
            // this happens on back button getting hit
        }
    });

    dialog.show();
}

【讨论】:

  • 这是一个更正确的答案! - 但是,最好将 Builder 放在 DialogFragment 中的 onCreateDialog 方法中正确覆盖。使对话框成为单例,只需从活动 YourDialog.newInstance().show(getFragmentManager(), YourDialog.TAG);
  • 干杯,当我再次使用 Davidea 时,我会注意这一点。
【解决方案3】:

你可以简单地设置这个:

dialog.setNeutralButton("skip", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Your code
            }
        });

【讨论】:

    猜你喜欢
    • 2015-04-13
    • 2012-06-15
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多