【发布时间】:2023-03-17 05:53:01
【问题描述】:
当我运行我的应用程序时,我看不到任何启动画面。我是通过视频执行此操作的,并且我已经设置了我的 AndroidManifest 设置,就像视频中的设置一样。我将 SplashScreen 放在 Manifest 文件中 MainActivity 的顶部并将其设置为正常(就像我创建启动屏幕活动时一样),但它仍然无法正常工作。
我的 AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.labawsrh.aws.introscreen">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreen"></activity>
<activity android:name=".finalActivity" />
<activity android:name=".interests" />
<activity android:name=".IntroActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
SplashScreen.java:
public class SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, 3000);
}
}
activity_splash_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_gradient"
>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/logo"
android:layout_centerInParent="true"
android:src="@drawable/whitegift"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:textColor="#ffffff"
android:layout_alignParentBottom="true"
android:text="Gift Finder"
android:textSize="60dp"
android:fontFamily="@font/intoscript"
android:gravity="center_horizontal"
android:paddingBottom="130dp"/>
</RelativeLayout>
IntroActivty.java(OnBoarding 屏幕代码):
public class IntroActivity extends AppCompatActivity {
private ViewPager screenPager;
IntroViewPagerAdapter introViewPagerAdapter ;
TabLayout tabIndicator;
Button btnNext;
int position = 0 ;
Button btnGetStarted;
Animation btnAnim ;
TextView tvSkip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// make the activity on full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// when this activity is about to be launch we need to check if its openened before or not
if (restorePrefData()) {
Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class );
startActivity(mainActivity);
finish();
}
setContentView(R.layout.activity_intro);
// hide the action bar
//getSupportActionBar().hide();
// ini views
btnNext = findViewById(R.id.btn_next);
btnGetStarted = findViewById(R.id.btn_get_started);
tabIndicator = findViewById(R.id.tab_indicator);
btnAnim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.button_animation);
tvSkip = findViewById(R.id.tv_skip);
// fill list screen
final List<ScreenItem> mList = new ArrayList<>();
mList.add(new ScreenItem("Best Advice!","GiftFinder is an app that gives you the best gift advices.",R.drawable.advice));
mList.add(new ScreenItem("How It Works?","Our AI code just needs couple info to find the best gift.",R.drawable.aicode));
mList.add(new ScreenItem("Then..","Let the AI find the best gift for you!",R.drawable.ai));
// setup viewpager
screenPager =findViewById(R.id.screen_viewpager);
introViewPagerAdapter = new IntroViewPagerAdapter(this,mList);
screenPager.setAdapter(introViewPagerAdapter);
// setup tablayout with viewpager
tabIndicator.setupWithViewPager(screenPager);
// next button click Listner
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
position = screenPager.getCurrentItem();
if (position < mList.size()) {
position++;
screenPager.setCurrentItem(position);
}
if (position == mList.size()-1) { // when we rech to the last screen
// TODO : show the GETSTARTED Button and hide the indicator and the next button
loaddLastScreen();
}
}
});
// tablayout add change listener
tabIndicator.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == mList.size()-1) {
loaddLastScreen();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// Get Started button click listener
btnGetStarted.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open main activity
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(mainActivity);
// also we need to save a boolean value to storage so next time when the user run the app
// we could know that he is already checked the intro screen activity
// i'm going to use shared preferences to that process
savePrefsData();
finish();
}
});
// skip button click listener
tvSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
screenPager.setCurrentItem(mList.size());
}
});
}
private boolean restorePrefData() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPrefs",MODE_PRIVATE);
Boolean isIntroActivityOpnendBefore = pref.getBoolean("isIntroOpnend",false);
return isIntroActivityOpnendBefore;
}
private void savePrefsData() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPrefs",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isIntroOpnend",true);
editor.commit();
}
// show the GETSTARTED Button and hide the indicator and the next button
private void loaddLastScreen() {
btnNext.setVisibility(View.INVISIBLE);
btnGetStarted.setVisibility(View.VISIBLE);
tvSkip.setVisibility(View.INVISIBLE);
tabIndicator.setVisibility(View.INVISIBLE);
// TODO : ADD an animation the getstarted button
// setup animation
btnGetStarted.setAnimation(btnAnim);
}
}
【问题讨论】:
-
嗨,尝试删除这些行 if (restorePrefData()) { Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class );开始活动(主活动);结束();我认为这可能是您的视图不可见的问题
-
如果我这样做,我必须删除你所说的行的整个 funcs 底部。
-
我在我的真实手机上运行我的应用程序,而不是虚拟机。我刚刚意识到有 2 个相同的应用程序。为什么会发生?
标签: java android splash-screen