Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多次尝试关闭对话框_Java_Android - Fatal编程技术网

Java 多次尝试关闭对话框

Java 多次尝试关闭对话框,java,android,Java,Android,我试图根据已发生的点击次数显示一个对话框。我有两个小问题,我将在下面解释: 因此,我清除应用程序上的数据,以便点击次数从0开始。基本上,我试图做的是,当我访问下面的类时,如果单击次数=4、8或12,那么在if-else语句中输出与它们相关的消息。如果它不等于这些数字中的任何一个,那么每点击4次(16、20、24、28等)就会显示默认消息“您将获得视频奖励” 因此,从零次单击时的刷新开始,当我导航到此页面时,我注意到每次单击(1、2、3、4等)都会显示默认的对话框消息,这不是我所需要的。我想让它显

我试图根据已发生的点击次数显示一个对话框。我有两个小问题,我将在下面解释:

因此,我清除应用程序上的数据,以便点击次数从0开始。基本上,我试图做的是,当我访问下面的类时,如果单击次数=4、8或12,那么在if-else语句中输出与它们相关的消息。如果它不等于这些数字中的任何一个,那么每点击4次(16、20、24、28等)就会显示默认消息“您将获得视频奖励”

因此,从零次单击时的刷新开始,当我导航到此页面时,我注意到每次单击(1、2、3、4等)都会显示默认的对话框消息,这不是我所需要的。我想让它显示4、8、12的消息,它们有自己的特定消息,然后(16、20、24、28等)应该显示一般消息

我还注意到,如果我通过选择“上一步”按钮退出页面,然后再次访问该页面,则每次对话框出现时,我都需要多次点击“确定”按钮才能关闭对话框。最初,在我从页面返回之前,我只需点击一次即可关闭对话框,但当我重新进入页面时,需要多次点击,我不知道为什么

如何解决这两个问题

代码如下:

import java.util.Random;

public class Content extends AppCompatActivity {

Button backButton;
Button selectAnotherButton;
TextView clickCountText;
int getClickCountInt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content);


    final SharedPreferencesManager prefManager = SharedPreferencesManager.getInstance(Content.this);
    clickCountText = findViewById(R.id.click_count);
    clickCountText.setText(Integer.toString(prefManager.getClicks()));
    getClickCountInt = Integer.parseInt(clickCountText.getText().toString());

    backButton = findViewById(R.id.button_back);
    selectAnotherButton = findViewById(R.id.button_select_another);

    setContent();

    selectAnotherButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clickCountText.setText(Integer.toString(prefManager.increaseClickCount()));

            if (getClickCountInt == 4){
                ShowRewardDialog("You are rewarded with a the yellow smiley face in the homepage");
            } else if (getClickCountInt == 8) {
                ShowRewardDialog("You are rewarded with a the green smiley face in the homepage");
             } else if (getClickCountInt == 12) {
                ShowRewardDialog("You are rewarded with a the red smiley face in the homepage");
             } else {
                for(int i = 0; i <= getClickCountInt; i+=4) {
                ShowRewardDialog("You are rewarded with a video\"");
                }
            }

            setContent();
        }
    });

    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}


private void ShowRewardDialog(String message) {

    final Dialog dialog = new Dialog(Content.this);
    dialog.setContentView(R.layout.custom_dialog);

    SpannableString title = new SpannableString("YOU GAINED A REWARD");

    title.setSpan(new ForegroundColorSpan(Content.this.getResources().getColor(R.color.purple))
            , 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // set the custom dialog components - text, image and button
    TextView text = dialog.findViewById(R.id.dialog_text);
    dialog.setTitle(title);

    text.setText(message);

    Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
  }
}
MainActivity类(第1页)

内容类(第2页):

公共类内容扩展了AppCompatActivity{
按钮背面按钮;
按钮选择另一个按钮;
文本查看单击计数文本;
int getClickCountInt;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u content);
final SharedReferencesManager prefManager=SharedReferencesManager.getInstance(Content.this);
clickCountText=findViewById(R.id.click\u count);
clickCountText.setText(Integer.toString(prefManager.getClicks());
getClickCountInt=Integer.parseInt(clickCountText.getText().toString());
backButton=findViewById(R.id.按钮返回);
selectAnotherButton=findViewById(R.id.button\u选择另一个);
setContent();
选择另一个按钮。setOnClickListener(新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
getClickCountInt++;
clickCountText.setText(Integer.toString(prefManager.increaseClickCount());
如果(getClickCountInt==4){
ShowRewardDialog(“主页上的黄色笑脸奖励您”);
}else if(getClickCountInt==8){
ShowRewardDialog(“主页上的绿色笑脸奖励您”);
}else if(getClickCountInt==12){
ShowRewardDialog(“主页上的红色笑脸奖励您”);
}//否则{

//对于(int i=0;i,算法的第一个问题是没有将当前的点击次数添加到点击次数中。 在selectAnotherButton.setOnClickListener的onClick中,您应该添加一个getClickCountInt++(并且不要忘记用这个新值更新clickCountText)

此外,在onCreate上,您应该从SharedReferences获取getClickCountInt的值,然后使用它设置clickCountText上的值,而不是相反


answear展示了如何在SharedReferences中读取/存储数据。

好的,我会试试,如果你可以发布一个编码示例,那么如果我不明白,我可以看看如何按照你所说的方式实现它。我想这是保存它的最后一部分,我可能需要看看,因为我还在学习共享首选项是如何工作的。是的,我需要一个编码示例:无法使其工作嘿!我刚刚更新了我的answear,添加了一个指向另一个answear的链接,介绍如何处理共享引用。如果您有任何问题,请告诉我!您还应该检查它,看看除了存储数据外,如何正确检索您的数据。好的,我几乎已经获得了它,但我想我需要您的帮助,因为我一直在使用它从第二个类中保存它。我在问题中放置了我的代码更新,但基本上我已经将所有共享首选项存储到自己的类中,并从主活动中检索它,并且能够从内容类中检索它。我将解释发生了什么。给我15分钟的时间来编写它。好的,我提供了一个更新来解释当前的问题。
public class SharedPreferencesManager{

    private static final String APP_PREFS = "AppPrefsFile";
    private static final String NUMBER_OF_CLICKS = "numberOfClicks";

    private SharedPreferences sharedPrefs;
    private static SharedPreferencesManager instance;



    private SharedPreferencesManager(Context context) {
        sharedPrefs = context.getApplicationContext().getSharedPreferences(APP_PREFS, MODE_PRIVATE);
    }


    public static synchronized SharedPreferencesManager getInstance(Context context){
        if(instance == null)
            instance = new SharedPreferencesManager(context);

        return instance;
    }

    public int increaseClickCount() {
        int clickCount = sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
        clickCount++;
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putInt(NUMBER_OF_CLICKS, clickCount);
        editor.apply();
        return clickCount;
    }

    public int getClicks(){
        return sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
    }
}
public class MainActivity extends AppCompatActivity {



       SharedPreferencesManager prefManager = SharedPreferencesManager.getInstance(this);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button jokesButton = findViewById(R.id.button_jokes);;

            jokesButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    prefManager.increaseClickCount();
                    openContentPage("jokes");
                }
            });


            TextView clickCountText = findViewById(R.id.click_count);
            clickCountText.setText(Integer.toString(prefManager.increaseClickCount()));


        }

        private void openContentPage(String v) {
            Intent intentContentPage = new Intent(MainActivity.this, Content.class);
            intentContentPage.putExtra("keyPage", v);
            startActivity(intentContentPage);

        }


    }
public class Content extends AppCompatActivity {

Button backButton;
Button selectAnotherButton;
TextView clickCountText;
int getClickCountInt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content);


    final SharedPreferencesManager prefManager = SharedPreferencesManager.getInstance(Content.this);
    clickCountText = findViewById(R.id.click_count);
    clickCountText.setText(Integer.toString(prefManager.getClicks()));
    getClickCountInt = Integer.parseInt(clickCountText.getText().toString());

    backButton = findViewById(R.id.button_back);
    selectAnotherButton = findViewById(R.id.button_select_another);

    setContent();

    selectAnotherButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getClickCountInt++;
            clickCountText.setText(Integer.toString(prefManager.increaseClickCount()));

            if (getClickCountInt == 4){
                ShowRewardDialog("You are rewarded with a the yellow smiley face in the homepage");
            } else if (getClickCountInt == 8) {
                ShowRewardDialog("You are rewarded with a the green smiley face in the homepage");
             } else if (getClickCountInt == 12) {
                ShowRewardDialog("You are rewarded with a the red smiley face in the homepage");
             } //else {
                //for(int i = 0; i <= getClickCountInt; i+=4) {
                //ShowRewardDialog("You are rewarded with a video\"");
                //}
            //}
        }
    });

    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}


private void ShowRewardDialog(String message) {

    final Dialog dialog = new Dialog(Content.this);
    dialog.setContentView(R.layout.custom_dialog);

    SpannableString title = new SpannableString("YOU GAINED A REWARD");

    title.setSpan(new ForegroundColorSpan(Content.this.getResources().getColor(R.color.purple))
            , 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // set the custom dialog components - text, image and button
    TextView text = dialog.findViewById(R.id.dialog_text);
    dialog.setTitle(title);

    text.setText(message);

    Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
  }
}