简体   繁体   English

如何在Android中堆叠烤面包?

[英]How to stack toasts in Android?

A friend is writing an Android app for school and I'm helping him a bit. 一位朋友正在为学校编写一个Android应用,我正在帮助他。 There is one question that I was not able to solve for like an hour. 我有一个小时无法解决一个问题。

He is trying to show 2 toasts after each other, but we were not able to stack them, or even show them at the same time. 他正试图互相展示2个吐司,但我们无法堆叠它们,甚至无法同时显示它们。 All we see is the second Toast. 我们所看到的只是第二个吐司。 We tried showing it for a shorter time, than the first, to see if it was hiding behind the second, it was not. 我们尝试将其显示的时间比第一个显示的时间短,以查看它是否隐藏在第二个之后。 Then we placed the second in the middle of the screen, but it didn't help either. 然后,我们将第二个放置在屏幕中间,但这也没有帮助。 He said it just works for his friend (I can't confirm that, but also couldn't google anyone having the same issue) 他说这只对他的朋友有用(我无法确认,但也不能用谷歌搜索出现相同问题的任何人)

Toast t1 = Toast.makeText(getApplicationContext(), "first", Toast.LENGTH_LONG);
t1.show();

Toast t2 = Toast.makeText(getApplicationContext(), "second", Toast.LENGTH_SHORT);
t2.setGravity(0, 50, 0);
t2.show();

Are we totally missing something? 我们完全错过了什么吗? Is it even designed to show two toasts the same time, or stack them? 它甚至设计为同时显示两个烤面包,还是将它们堆叠在一起?

Use postDelayed(). 使用postDelayed()。 Show the first Toast and after sometime show the second one: 显示第一个Toast,一段时间后显示第二个:

final Handler handler = new Handler();


    handler.postDelayed(
            () -> //show the toast here,
            1200);
        handler.postDelayed(() -> //show second toast,
     2400);
          }

https://developer.android.com/reference/android/os/Handler https://developer.android.com/reference/android/os/Handler

Try a snackbar (They look way better too!) 试试小吃吧(它们看起来也更好!)

protected ArrayList<Snackbar> mSnackbarList = new ArrayList<>();

protected Snackbar.Callback mCallback = new Snackbar.Callback() {
    @Override
    public void onDismissed(Snackbar snackbar, int event) {
        mSnackbarList.remove(snackbar);
        if (mSnackbarList.size() > 0)
           displaySnackbar(mSnackbarList.get(0));
    }
};

public void addQueue(Snackbar snackbar){
    setLayoutParams(snackbar);
    snackbar.setCallback(mCallback);
    mSnackbarList.add(snackbar);
    if(mSnackbarList.size() == 1)
        displaySnackbar(snackbar);
}

public void displaySnackbar(Snackbar snackbar){
    snackbar.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM