gpt4 book ai didi

java - "Show once"屏幕 boolean 值问题

转载 作者:行者123 更新时间:2023-12-01 14:52:56 25 4
gpt4 key购买 nike

我有一个应用程序显示启动屏幕,然后显示“Showonce”屏幕,我使用了应用程序首选项和 boolean 值“user_accept”来决定屏幕是否显示,无论 boolean 值是否= true,它仍然存在问题显示页面,请查看我的页面代码,非常感谢任何帮助:)。

package com.overclockerz.webtest;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Window;
import android.widget.Toast;



public class SplashScreen extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash_layout);
final SharedPreferences settings = getSharedPreferences("APP_PREFS",
MODE_PRIVATE);
final boolean hasAgreed = settings.getBoolean("user_accepted", false);
Handler handler = new Handler();

// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {

@Override
public void run() {
if (hasAgreed == false){
Intent intent = new Intent(SplashScreen.this, ShowOnce.class);
SplashScreen.this.startActivity(intent);
Toast.makeText(getApplicationContext(), "DEBUG: USER HAS NOT ACCEPTED",Toast.LENGTH_LONG).show();
}else if (hasAgreed == true){
Toast.makeText(getApplicationContext(), "DEBUG: USER HAS ACCEPTED",Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}

finish();
}

}, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called

}
}



package com.overclockerz.webtest;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class ShowOnce extends Activity implements OnClickListener {
Button show_options_dialog, accept_button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show_once);
show_options_dialog = (Button) findViewById(R.id.show_options_dialog);
accept_button = (Button) findViewById(R.id.accept_button);
show_options_dialog.setOnClickListener(this);
accept_button.setOnClickListener(this);


}

@Override
public void onClick(View v) {
if (v == show_options_dialog){
Intent intent = new Intent(getApplicationContext(), SettingScreen.class);
startActivity(intent);
}
else if (v == accept_button){
SharedPreferences settings = getSharedPreferences("APP_PREFS",
MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putBoolean("user_accept", true);
Toast.makeText(getApplicationContext(), "DEBUG: USER CLICKED ACCEPT", Toast.LENGTH_LONG).show();
prefEditor.commit();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}

}

}

最佳答案

您正在保存user_accept并检查user_accepted

请使用public static final String以避免此类问题:

public static final String USER_ACCEPTED = "accepted";
.........
prefEditor.putBoolean(USER_ACCEPTED , true);
.........
settings.getBoolean(USER_ACCEPTED , false);

关于java - "Show once"屏幕 boolean 值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14663792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com