【问题标题】:"Show once" Screen boolean Issue“显示一次”屏幕布尔问题
【发布时间】:2013-02-02 16:13:11
【问题描述】:

我有一个应用程序,它显示一个启动屏幕,然后是一个“Showonce”屏幕,我使用了应用程序首选项和一个布尔值“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();
        }

    }

}

【问题讨论】:

    标签: java android boolean splash-screen


    【解决方案1】:

    您正在保存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);
    

    【讨论】:

    • 谢谢,我就知道会这么简单,看不到森林的树,再次谢谢!整天把我逼疯了。
    • 只是在等待计时器 :)
    猜你喜欢
    • 2018-05-18
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2014-09-28
    • 2021-08-25
    • 2014-09-02
    • 2012-04-15
    相关资源
    最近更新 更多