基本的Android启动器启动画面 [英] Basic Android launcher splash screen

查看:78
本文介绍了基本的Android启动器启动画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是刚开始编写代码并为我的猜测道歉的人,这可能是一个非常简单的问题.我遵循了Android的构建您的第一个应用指南.接下来,我尝试通过遵循一些教程(sendMessage函数.

I'm new to writing code and apologize for what I am guessing is probably a very simple question. I followed Android's Build your first app guide. Next, I'm trying to add a launcher splash screen by following a number of tutorials (this being one of them). When I run the app in the emulator it loads so fast that I can't tell if the splash screen works. Is there a way to temporarily slow down the emulator to check the splash screen? Also the app crashes when the Send button is clicked which doesn't make sense because the sendMessage function exists in the MainActivity.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myfirstapp, PID: 9242
    java.lang.IllegalStateException: Could not find method sendMessage(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button'
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

老实说,我不确定是什么问题,或者应用程序太小而无法显示启动屏幕.我也不确定在这里共享我的项目的最佳方法.因此,下面是所有代码.

I'm honestly not sure what is wrong or if the app is just so small that a splash screen won't show. I'm also not sure the best way to share my project here. So below is all of the code.

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp">

    <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=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".DisplayMessageActivity"
                  android:parentActivityName=".MainActivity">
            <!-- For API 15 -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.kt

MainActivity.kt

package com.example.myfirstapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText

const val EXTRA_MESSAGE = "come.example.myfirstapp.MESSAGE"

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    //Called when user taps Send button
    fun sendMessage(view: View)
    {
        val editText = findViewById<EditText>(R.id.editTextTextPersonName)
        val message = editText.text.toString()
        val intent = Intent(this, DisplayMessageActivity::class.java).apply {putExtra(EXTRA_MESSAGE, message)}
        startActivity(intent)
    }
}

DisplayMessageActivity

DisplayMessageActivity

package com.example.myfirstapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class DisplayMessageActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display_message)

        //Get the Intent that started this activity and extract the string
        val message = intent.getStringExtra(EXTRA_MESSAGE)

        //Capture the layout's TextView and set the string as its text
        val text = findViewById<TextView>(R.id.textView).apply {text = message}
    }
}

SplashActivity

SplashActivity

package com.example.myfirstapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class SplashActivity : AppCompatActivity()
{
    override fun onCreate(savedIntanceState: Bundle?)
    {
        setTheme(R.style.AppTheme)
        super.onCreate(savedIntanceState)
        setContentView(R.layout.activity_main)
    }
}

drawable splash_background.xml

drawable splash_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@color/colorPrimary" />
    <item>
        <bitmap android:gravity="center" android:src="@mipmap/ic_launcher" />
    </item>
</layer-list>

重视styles.xml

values styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- Splash Screen theme. -->
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
</resources>

对于新手问题,我们深表歉意,希望我不会发布任何规则,但希望您能提供一些指导.谢谢!

Sorry for the novice question, and I hope I'm not breaking any rules by posting it, but would appreciate some guidance. Thanks!

编辑-在得到 S T

推荐答案

如果您想为首次发布添加教程,那么将很好地解释如何使用幻灯片创建简介: https://www.androidhive.info/2016/05 /android-build-intro-slider-app/

If you want to add a tutorial for the first launch there is well explained how to create an introduction with slides: https://www.androidhive.info/2016/05/android-build-intro-slider-app/

如果只希望每次启动应用程序时都显示一个简单的启动屏幕,请参见以下内容: https://android.jlelse.eu/the-complete- android-splash-screen-guide-c7db82bce565

If you want just a simple splash screen that appears each time the app is started see this: https://android.jlelse.eu/the-complete-android-splash-screen-guide-c7db82bce565

这篇关于基本的Android启动器启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆