是否可以在不点击任何按钮的情况下发出弹出消息?

时间:2015-07-24 01:33:59

标签: android popup message

   AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
    myAlert.setMessage(username)
            .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setTitle("Welcome To iShop!")
            .create();
    myAlert.show();

活动开始时可以弹出它吗?

1 个答案:

答案 0 :(得分:3)

只需将您编写的代码放在Activity的onCreate()方法中。

@Override
protected void onCreate(Bundle savedInstanceState) {
    AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
    myAlert.setMessage(username)
        .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        })
        .setTitle("Welcome To iShop!")
        .create();
    myAlert.show();
}