Add positive button to Dialog

后端 未结 3 691
不知归路
不知归路 2020-12-31 15:39

I have a very simple custom dialog and I wan\'t to add a positive button without having to modify the XML file, just like you would do it with an AlertDialog but I don\'t kn

相关标签:
3条回答
  • 2020-12-31 16:20

    You can use the AlertDialog.Builder class:

    http://developer.android.com/reference/android/app/AlertDialog.Builder.html

    Create a new instance of it with AlertDialog.Builder myAlertDialogBuilder = new AlertDialog.Builder(context). Then use methods such as setTitle() and setView() to customize it. This class also has methods for setting the buttons. setPositiveButton(String, DialogInterface.OnClickListener) to set up your buttons. Finally, use AlertDialog myAlertDialog = myAlertDialogBuilder.create() to get your instance of AlertDialog, which you can then further customize with methods such as setCancelable().

    Edit: Also, from the docs: http://developer.android.com/guide/topics/ui/dialogs.html

    "The Dialog class is the base class for creating dialogs. However, you typically should not instantiate a Dialog directly. Instead, you should use one of the... subclasses"

    If you really don't want to use an AlertDialog, it'll probably be best to extend the Dialog class yourself, rather than using it as-is.

    0 讨论(0)
  • 2020-12-31 16:40

    You also can use this function

    public void showMessage(String title,String message)
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("OK", new
                DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        builder.show();
    }
    
    0 讨论(0)
  • 2020-12-31 16:41

    You should use the builder.

    LayoutInflater inflater = LayoutInflater.from(this);
    View dialog_layout = inflater.inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.dialog_root_layout));
    AlertDialog.Builder db = new AlertDialog.Builder(MyActivity.this);
    db.setView(dialog_layout);
    db.setTitle("settings");
    db.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    AlertDialog dialog = db.show();
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题