简体   繁体   English

在for循环之前显示烤面包不起作用

[英]Show toast prior to a for loop does not work

I am trying to show a toast prior to a for loop, however, the toast does not show. 我正在尝试在for循环之前显示烤面包,但是,烤面包没有显示。 I also tried adding a snackbar, but that didn't work either. 我也尝试添加一个小吃店,但是那也不起作用。 I would like to know why the toast does not fire first. 我想知道为什么不先烤面包。 I even tried the following, but it kept looping: 我什至尝试了以下方法,但一直循环播放:

Toast toast = Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);
while (!toast.getView().isShown()) {
    toast.show();
}

How can I show a toast prior to a for loop? 如何在for循环之前显示烤面包?

ArrayList<String> pizzaBases = new ArrayList<> ();    

@Override
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            main.runOnUiThread(new Runnable() {
                public void run() {
                        Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);
                        Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();
                    }
            });

            SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);

            for (int i = 0; i < 100; i++) {
                try {
                    String pizza = preferences.getString("TypeOfBase" + i, "");
                    if (!pizza.equals("")) {
                        pizzaBases.add(pizza);
                    }
                } catch (NullPointerException e) {
                    //Do nothing here
                }
            }
        }
    });

    Button button1 = (Button) findViewById(R.id.buttonOne);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (String myString : pizzaBases) {
                System.out.println(myString);
            }
        }
    });
}

I tried solutions like runOnUiThread, asynctask and while loops, but they all didn't show a toast nor a snackbar. 我尝试了诸如runOnUiThread,asynctask和while循环之类的解决方案,但它们都没有显示烤面包或小吃店。

EDIT: 编辑:

I found out that the UI only updates AFTER the for loop is done. 我发现UI仅在for循环完成后才更新。 I am wondering why it does that, although it is activated first. 我想知道为什么会这样做,尽管它先被激活了。

The problem is that Toast.show() is asynchronous. 问题在于Toast.show()是异步的。 The toast will not be displayed until the current method returns, and even then, Android may decide to show it at a later time (for example if there is already another toast displayed on the screen) or even not at all. 在当前方法返回之前,不会显示该Toast,即使如此,Android可能会决定稍后再显示它(例如,如果屏幕上已经显示了另一个Toast),甚至根本不显示它。

Do you really need to use the native toasts? 您真的需要使用土司面包吗? Maybe the best would be to use another system that is more reliable. 也许最好的办法是使用另一个更可靠的系统。 What about a custom view that appears on top of everything that looks like a toast and that you can show immediately and reliably? 如果自定义视图出现在看起来像烤面包的所有事物之上,并且可以立即可靠显示,那又如何呢?

You are not calling show() method on this line: 您不会在此行调用show()方法:

Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);

It should be: 它应该是:

Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();

You don't need to use runOnUiThread cause you're already on the main thread and you're not calling show() on your toast in the second code. 您不需要使用runOnUiThread因为您已经在主线程上,并且没有在第二个代码中的烤面包上调用show()
It's also better to pass in the nearest Context object to your toast. 最好将最近的Context对象传递给吐司。

public void onClick(View view) {
        Toast.makeText(YourActivity.this, "Hello", Toast.LENGTH_SHORT).show(); // show must be called
        Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();

        SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
    ...
}

Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);

  • When you are show toast you have to call .show() method. 当您表演吐司时,必须调用.show()方法。

  • If you are showing Toast in Activity it will be better to use Activity 's context 如果要在Activity中显示Toast ,最好使用Activitycontext

Correction 更正

Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();


Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();

If you want to show SnackBar than you have to provide the view of current activity not android.R.id.content 如果要显示SnackBar ,则必须提供当前活动的视图,而不是android.R.id.content

Correction 更正

Snackbar.make(findViewById(R.id.parent_of_your_activity), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();


  • You don't need to use runOnUiThread cause you're already on the main thread 您不需要使用runOnUiThread因为您已经在主线程上了

  • You can get Context 您可以获取Context

    1. via YourActivity.this or this in Activity itself 通过YourActivity.thisActivity本身中的this

    2. via getContext() in Fragment class 通过Fragment类中的getContext()

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

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