我刚刚在模拟器中安装了 Android R (API 30) 图像来试用我的应用程序,但在尝试设置 Toast 的背景颜色时它崩溃了。
Toast toast = Toast.makeText(ctxt, msg, duration);
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
toast.show();
Run Code Online (Sandbox Code Playgroud)
这真的很奇怪,因为在 Android Q (API 29) 中运行完美。
我为 Android R (API 30) 更新了 build.gradle
compileSdkVersion 30
buildToolsVersion "30.0.1"
Run Code Online (Sandbox Code Playgroud)
有没有新的方法呢??
我有祝酒问题.对于API 26及以下的吐司正确显示(下一个toast正在等待之前消失)但在Android 8.1(API 27)上它们相互覆盖.我的通知频道设置如下:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
Run Code Online (Sandbox Code Playgroud)
这为我修复了8.0的祝酒词,但在8.1上它们仍然重叠

有没有办法解决这个问题而不是记住上次使用过的吐司并手动取消它?
编辑:
此线程的解决方法不起作用
/**
* <strong>public void showAToast (String st)</strong></br>
* this little method displays a toast on the screen.</br>
* it checks if a toast is currently visible</br>
* if so </br>
* ... it "sets" the new text</br>
* else</br>
* ... it "makes" the new text</br>
* and "shows" either or
* @param st the string …Run Code Online (Sandbox Code Playgroud) 我在这里已经阅读过这类问题,但答案似乎没有奏效.
我Toast在用户点击按钮时显示.当用户不断点击按钮时,即使用户退出活动,烤面包仍会一次又一次地显示.
吐司的长度很短.由于文本很长,因此无法更改Toast的长度.
这就是我现在所尝试的:
Toast toast;
toast=Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_SHORT);
if(toast.getView().isShown()==false){
toast.show();
}
Run Code Online (Sandbox Code Playgroud)
这没用.
我试过了 :
if(toast.getView().isShown()==true){
toast.cancel();
}
Run Code Online (Sandbox Code Playgroud)
在onStop().由于某种原因,取消方法永远不会有效.
如果我把.cancel()之前我显示应用程序...那么将有另一个空检查.但这样做之后也无效.我可以显示一个对话框而不是一个吐司,但这不是一个解决方案.
有没有办法检查是否正在显示吐司?
以供参考
我正在开发一个使用系统活动将联系人添加到手机内存的应用程序.保存联系人后,此外部活动将启动Toast.有没有可能摆脱它?如果我可以获得对它的引用来调用cancel()或取消所有排队的Toasts,那将是完美的.有Toast经理吗?
当我点击按钮时,我正在显示一个简单的Toast.我的问题是,当我多次点击一个按钮时,Toast消息会一直显示,直到我进入主屏幕.当我到达主屏幕并在相应的活动中杀死Toast消息时,我想停止Toast.我附上了截图.
我编写的代码如下:
public class Main extends Activity {
Dialog d;
Toast t;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t = Toast.makeText(Main.this, "you clicked on button!", Toast.LENGTH_LONG);
t.show();
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
t.cancel();
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?