简体   繁体   English

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

[英]Android: Alert dialog with custom dialog and neutral button

I'm editing my question for better understanding for what I need, because answer below were not giving me proper solution for this. 我正在编辑我的问题,以便更好地理解我需要的东西,因为下面的答案没有给我正确的解决方案。 So, what I have to do is make a custom alert dialog with a neutral button at the bottom. 所以,我要做的是使用底部的中性按钮创建一个自定义警报对话框。 Here is the example what it should look like: 以下是它应该是什么样子的示例:

我必须做一个看起来像这样的对话框

Till now I have used custom dialog using xml and changing activity to android:theme="@android:style/Theme.Holo.Light.Dialog" , so I can get the look and feel like the same. 直到现在我已经使用自定义对话框使用xml并将活动更改为android:theme="@android:style/Theme.Holo.Light.Dialog" ,所以我可以得到相同的外观和感觉。 Here is my xml code: 这是我的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>

By the above layout I'm getting the following outcome: 通过上面的布局我得到了以下结果:

直到现在实施

The problem I'm facing is: How to set neutral button in the custom dialog? 我面临的问题是:如何在自定义对话框中设置中性按钮? I'm not getting any idea regarding this. 我对此并不了解。 If you have doubt in my question or you can't understand the language please leave comment, so that I can again give you a clear idea. 如果您对我的问题有疑问或者您无法理解该语言,请发表评论,以便我再次给您一个清晰的想法。 Help will be appreciated. 帮助将不胜感激。

Create custom alert dialog like 创建自定义警报对话框

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

    }

where R.layout.messagescreen is your custom created layout as you have shown in your image. 其中R.layout.messagescreen是您在图像中显示的自定义创建的布局。 try this out and let me know if you face any problem. 试试这个,如果你遇到任何问题,请告诉我。

Anupam, I know you have selected an answer already, but I though I might add in my 2 cents in how I have done something similar. Anupam,我知道你已经选择了一个答案,但我可能会在我的2美分中添加类似的东西。

I have used AlertDialog instead of Dialog and it floats above the application when called. 我使用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();
}

you can simply set this: 你可以简单地设置这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM