如何取消在android上以不同方法创建的Toast?

发布于 2024-10-29 13:10:47 字数 743 浏览 3 评论 0原文

我有以下代码:

private Toast movieRecordToast;

    private void displayNextMovie() {
        if (movieRecordToast != null) movieRecordToast.cancel(); // cancel previous Toast (if user changes movies too often)
        movieRecordToast = Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT);
        movieRecordToast.show();

    private void displayPrevMovie() {
        if (movieRecordToast != null) movieRecordToast.cancel();
        movieRecordToast = Toast.makeText(getApplicationContext(), "Prev", Toast.LENGTH_SHORT);
        movieRecordToast.show();        

但是,如果 displayNextMovie 被快速调用几次,然后 displayPrevMovie 被调用,“Next”Toast 仍然显示,并且只有在“Prev”显示之后。 看来取消功能不能正常工作。

I have the following code:

private Toast movieRecordToast;

    private void displayNextMovie() {
        if (movieRecordToast != null) movieRecordToast.cancel(); // cancel previous Toast (if user changes movies too often)
        movieRecordToast = Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT);
        movieRecordToast.show();

    private void displayPrevMovie() {
        if (movieRecordToast != null) movieRecordToast.cancel();
        movieRecordToast = Toast.makeText(getApplicationContext(), "Prev", Toast.LENGTH_SHORT);
        movieRecordToast.show();        

But if displayNextMovie is called quickly several times and then displayPrevMovie is called, "Next" Toast is still shown and only after that "Prev" is displayed.
Looks like cancellation doesn't work properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪恋 2024-11-05 13:10:47

您可以轻松地只保留一个 Toast 对象并取消当前的 ToastToast 对象> 只要你愿意。在显示下一个 Toast 之前,您可以使用 Toast.setText() 函数更改文本。

示例代码:

private Toast mToastText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create the object once.
    mToastText = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}

private void displayText(final String message) {
    mToastText.cancel();
    mToastText.setText(message); 
    mToastText.show();
}

Instead of creating a new Toast object each time you want a new text displayed you can easily hold on to only one Toast object and cancel the current Toast whenever you want. Before the next Toast is being displayed you can change text with Toast.setText() function.

Sample code:

private Toast mToastText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create the object once.
    mToastText = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}

private void displayText(final String message) {
    mToastText.cancel();
    mToastText.setText(message); 
    mToastText.show();
}
你穿错了嫁妆 2024-11-05 13:10:47

我认为有很多方法可以实现向用户显示下一个/上一个信息。我会完全放弃吐司,并使用下一个/上一个电影的名称来更新 TextView 的文本。这将消除你的问题,而且恕我直言,还可以带来更好的用户界面。

但是,如果您的设计要求确实要求 Toast 通知,请尝试:

    private Toast nextMovieRecordToast;
private Toast prevMovieRecordToast;


private void displayNextMovie() {
    if (prevMovieRecordToast != null) prevMovieRecordToast.cancel(); // cancel previous Toast (if user changes movies too often)
    nextMovieRecordToast = Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT);
    nextMovieRecordToast.show();}

private void displayPrevMovie() {
    if (nextMovieRecordToast != null) nextMovieRecordToast.cancel();
    prevMovieRecordToast = Toast.makeText(getApplicationContext(), "Prev", Toast.LENGTH_SHORT);
    prevMovieRecordToast.show();    }    

I think there are many ways you can achieve displaying the next/prev info to the user. I would ditch the toasts altogether and update the text of a TextView with the name of next/prev movie. That would eliminate your problem and also IMHO makes for better UI.

However, if your design requirements do ask for toast notifications, try:

    private Toast nextMovieRecordToast;
private Toast prevMovieRecordToast;


private void displayNextMovie() {
    if (prevMovieRecordToast != null) prevMovieRecordToast.cancel(); // cancel previous Toast (if user changes movies too often)
    nextMovieRecordToast = Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT);
    nextMovieRecordToast.show();}

private void displayPrevMovie() {
    if (nextMovieRecordToast != null) nextMovieRecordToast.cancel();
    prevMovieRecordToast = Toast.makeText(getApplicationContext(), "Prev", Toast.LENGTH_SHORT);
    prevMovieRecordToast.show();    }    
爱的十字路口 2024-11-05 13:10:47

wroclai的解决方案非常棒!然而,当从长消息 Toast 变为短消息 Toast 时,它会搞砸 Toast,反之亦然。
要修复此问题而不是使用以前的对象重新创建它。所以代替这一行:
mToastText.setText(消息);
写这个:myToast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
动画看起来也更好:)

The wroclai's solution is excellent! However it screws the Toast when going form long message toast to short one and vice versa.
To fix this instead of using previous object recreate it. So instead of this line:
mToastText.setText(message);
write this one: myToast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
The animations also looks better :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文