Toast.show() 未在模拟器中显示消息 [英] Toast.show() not showing the message in Emulator

查看:55
本文介绍了Toast.show() 未在模拟器中显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎适用于 Android 6 Marshmallow 设备小米红米 3S.看起来像是模拟器的问题,但在任何其他意义上都不是真正的行为不端.

This seems to work on Android 6 Marshmallow device Xiaomi Redmi 3S. Seems like an emulator problem, but it isn't really misbehaving in any other sense.

我是 Android 开发的初学者,了解不多.我已确认我的应用通知已开启.

I am a beginner in Android development and don't know much. I have already confirmed that notifications for my app is ON.

规格:API 级别:Android 4.4 (KitKat)

Specs: API Level : Android 4.4 (KitKat)

Android Studio 版本:3.6.4

Android Studio version: 3.6.4

模拟器:Pixel XL API 28

Emulator: Pixel XL API 28

我试图做的只是弹出一条吐司消息,告诉我们按钮被点击了多少次.

All I attempted to do was a toast message would pop up telling how many times the button has been clicked.

这是 MainActivity.kt:

Here's the MainActivity.kt:

class MainActivity : AppCompatActivity() {
    private var greetingCount = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btnToastMessage.setOnClickListener {
            val suffix = when (++greetingCount) {
                1 -> "st"
                2 -> "nd"
                3 -> "rd"
                else -> "th"
            }
            val greetingMsg = "Welcome for the $greetingCount$suffix time(s)!"
            Toast.makeText(this@MainActivity, greetingMsg, Toast.LENGTH_LONG).show()
            Log.i(".MainActivity", "Clicked Button")
        }
    }
}

这是 activity_main.xml 文件:

And here's the activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="30dp">
    <TextView
        android:id="@+id/txtHelloWorld"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.314" />

    <Button
        android:id="@+id/btnToastMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtHelloWorld"
        app:layout_constraintVertical_bias="0.49" />

</androidx.constraintlayout.widget.ConstraintLayout>

推荐答案

不知道可以这样做,但擦除数据和冷启动设备修复了它.

Didn't know this could be done, but wiping data and cold-booting the device fixed it.

这篇关于Toast.show() 未在模拟器中显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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