如何只显示一次对话框

时间:2017-03-29 21:46:37

标签: java android dialog alertdialog

您好,我试图创建包含两个按钮的对话框[GOT IT! ]和[不要再显示我这个]我想要点击[不要再显示我这个]按钮对话框完全关闭并且不再显示应用程序重新打开

enter image description here

public boolean Show = false;

public void IntroSupport(){

    Show = true;

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    ImageView image = new ImageView(this);
    image.setImageResource(R.mipmap.ic_launcher);
    builder.setIcon(R.mipmap.service)
            .setTitle("Online Support :")
            .setView(image)
            .setMessage("Some text")
            .setNegativeButton("GOT it!",null)
            .setPositiveButton("Don't Show Me this Again", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
    builder.setCancelable(false);
    AlertDialog about = builder.create();
    about.show();
    TextView messageText = (TextView) about.findViewById(android.R.id.message);
    messageText.setGravity(Gravity.CENTER);
    Button nbutton = about.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setTextColor(Color.BLACK);
}


        @Override
        public void onPageSelected(int position) {
            switch (position){
                case 0:
          getSupportActionBar().setTitle(Html.fromHtml("title :"+Pb));
                    tabLayout.getTabAt(0).setIcon(R.mipmap.ic1vrai);
                    tabLayout.getTabAt(1).setIcon(R.mipmap.ic__2vrai);
                    break;
                case 1:

                    if(Show == false){
                        IntroSupport();
                    }

                    getSupportActionBar().setTitle(Html.fromHtml("("titl"+sus));
                    tabLayout.getTabAt(1).setIcon(R.mipmap.ic_2faux);
                    tabLayout.getTabAt(0).setIcon(R.mipmap.ic1faux);
                    break;
            }
        }

2 个答案:

答案 0 :(得分:2)

使用SharedPreference显示对话框。试试这段代码:

public boolean Show = false;

public void IntroSupport(){
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    ImageView image = new ImageView(this);
    image.setImageResource(R.mipmap.ic_launcher);
    builder.setIcon(R.mipmap.service)
            .setTitle("Online Support :")
            .setView(image)
            .setMessage("Somme text")
            .setNegativeButton("GOT it!",null)
            .setPositiveButton("Don't Show Me Again", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = getSharedPreferences("mypref", MODE_PRIVATE).edit();
                     editor.putBoolean("dontshow", true);
                     editor.commit();
                }
            });
    builder.setCancelable(false);
    AlertDialog about = builder.create();
    about.show();
    TextView messageText = (TextView) about.findViewById(android.R.id.message);
    messageText.setGravity(Gravity.CENTER);
    Button nbutton = about.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setTextColor(Color.BLACK);
}


    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
          getSupportActionBar().setTitle(Html.fromHtml("title :"+Pb));
                tabLayout.getTabAt(0).setIcon(R.mipmap.ic1vrai);
                tabLayout.getTabAt(1).setIcon(R.mipmap.ic__2vrai);
                break;
            case 1:
                SharedPreferences prefs = getSharedPreferences("mypref", MODE_PRIVATE); 
                show = prefs.getBoolean("dontshow", false);
                if(Show == false){
                    IntroSupport();
                }

                getSupportActionBar().setTitle(Html.fromHtml("("titl"+sus));
                tabLayout.getTabAt(1).setIcon(R.mipmap.ic_2faux);
                tabLayout.getTabAt(0).setIcon(R.mipmap.ic1faux);
                break;
        }
    }

答案 1 :(得分:1)

如果用户按下SharedPreferences按钮,您可以将值存储到Don't show me this again。然后你应该把你的对话框东西包装在一个if子句中,该子句检查SharedPreferences中的值是否设置。

以下是SharedPreferences文档的链接:https://developer.android.com/reference/android/content/SharedPreferences.html

相关问题