我的吐司消息没有出现在应用程序中

问题描述 投票:0回答:1

一开始是可见的,不知道是不是模拟器的问题,我的toast消息现在已经不可见了。你能帮帮我吗?

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends AppCompatActivity {
    private static final String PREFS_NAME = "MyPrefsFile";
    private static final String KEY_FIRST_TIME = "first_time";

    private boolean isFirstTime() {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        boolean firstTime = preferences.getBoolean(KEY_FIRST_TIME, true);
        if (firstTime) {
            preferences.edit().putBoolean(KEY_FIRST_TIME, false).apply();
        }
        return firstTime;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (isFirstTime()) {
            Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
        }

        new Handler().postDelayed(() -> {
            Intent i = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(i);
            finish();
        }, 5000);



    }


}

我的代码是这样的 而且正如我所说,一开始屏幕上出现了吐司消息,但我不知道我是否做了什么,但它不再出现了。它可能与 xml 文件有关吗?

java android android-studio android-toast android-splashscreen
1个回答
0
投票

我希望这些解决方案之一可以帮助您解决 Toast 消息的问题。 Toast 消息未出现在您的应用程序中。以下是您可以尝试解决问题的一些方法:

  1. 检查Toast消息是否被调用:确保

Toast.makeText()

方法正在被调用,消息不是 空的或空的。您可以添加一条日志语句来检查代码是否 到达 Toast 消息或使用调试器单步执行 代码。 2.检查Toast时长是否过短:The duration of the Toast 消息设置为 Toast.LENGTH_SHORT,即 2 秒。如果 消息出现和消失的太快,你可以试试 将持续时间更改为 Toast.LENGTH_LONG(5 秒)或自定义 使用持续时间

Toast.setDuration()

方法。 3、查看Toast是否在离屏显示:有时, Toast 消息可能会出现在屏幕外,尤其是在具有 缺口或圆角。你可以尝试改变重力 使用

吐司消息

Toast.setGravity()

确保它的方法 出现在可见区域。

© www.soinside.com 2019 - 2024. All rights reserved.