gpt4 book ai didi

java - 首次运行时打开教程无法正常工作

转载 作者:行者123 更新时间:2023-12-01 15:13:59 25 4
gpt4 key购买 nike

当用户第一次运行应用程序时,我的应用程序中会显示一个教程

        Handler handler = new Handler(); 
handler.postDelayed(new Runnable() {
public void run() {
if (firstRun == true) {


Intent tut = new Intent(MainActivity.this, Tutorial.class);
startActivity(tut);
firstRun = false;

}
}
}, 200);

我推迟了它,因为如果没有延迟,我只会得到黑屏(界面没有时间加载)

但是这样做我多次打开Tutorial.class,我做错了什么?

编辑:

这里还有一些代码,我不会粘贴全部代码,因为阅读起来太长而且与问题无关

我这样保存我的偏好

 @Override
protected void onStop(){
super.onStop();

// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("Counter1", counter);
editor.putInt("Counter2", counter2);
editor.putBoolean("FirstRun", firstRun);
editor.putString("Label1", label1S);
editor.putString("Label2", label2S);

editor.commit();
}
protected void onPause(){
super.onPause();

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("Counter1", counter);
editor.putInt("Counter2", counter2);

editor.commit();
}

这是我在 onCreate() 中恢复它们的方法;

// Restore previous settings and data


SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int counterRestored = settings.getInt("Counter1", 0);
int counter2Restored = settings.getInt("Counter2", 0);
boolean firstRunRestored = settings.getBoolean("FirstRun", true);
String label1Restored = settings.getString("Label1", "Counter 1");
String label2Restored = settings.getString("Label2", "Counter 2");
counter = counterRestored;
counter2 = counter2Restored;
firstRun = firstRunRestored;
label1S = label1Restored;
label2S = label2Restored;
renameLabel();
calculateTotal();

这是我的第二个 Activity Tutorial.class

public class Tutorial extends MainActivity{

ImageButton btnSkip, btnSkip2, btnNext, btnNext2;
RelativeLayout tutorial, tutPage1, tutPage2;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial);


btnSkip = (ImageButton) findViewById(R.id.btn_skip);
btnNext = (ImageButton) findViewById(R.id.btn_next);
btnSkip2 = (ImageButton) findViewById(R.id.btn_skip2);
btnNext2 = (ImageButton) findViewById(R.id.btn_next2);
tutorial = (RelativeLayout) findViewById(R.id.tutorial);
tutPage1 = (RelativeLayout) findViewById(R.id.page1);
tutPage2 = (RelativeLayout) findViewById(R.id.page2);


btnSkip.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
btnSkip2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
btnNext.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
tutPage1.setVisibility(View.GONE);
tutPage2.setVisibility(View.VISIBLE);
}
});
btnNext2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
tutPage1.setVisibility(View.VISIBLE);
tutPage2.setVisibility(View.GONE);
tutorial.setVisibility(View.VISIBLE);
finish();
}
});

}

}

最佳答案

你为什么不尝试

 if(firstRun){
firstRun = false;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent tut = new Intent(MainActivity.this, Tutorial.class);
startActivity(tut);
}
}
}, 200);
}

关于java - 首次运行时打开教程无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11907991/

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