【问题标题】:how to show array in Toast如何在 Toast 中显示数组
【发布时间】:2016-05-24 05:35:20
【问题描述】:

我有一个应用程序来显示具有特定持续时间和特定时间的 toast 来显示下一个 toast 并在显示器上随机显示, 那是安全的,但不要在吐司中显示数组项。 怎么做? tnx

 //MyReceive
public void onReceive(Context con, Intent mIntent) {

    mContext = con;
    final String[] array = { "1", "3", "4", "5", "6", "ffff","END"};


        final Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                Random r = new Random();
                int i1 = r.nextInt(Activity_Main.w);

                r = new Random();
                int i2 = r.nextInt(Activity_Main.h);
                Log.d("tag : ", i1 + "   :   " + i2);

                for (String arr : array) {
                    t1 = Toast.makeText(mContext, arr, Toast.LENGTH_SHORT);

                }

                //delay in show toast duration 100ms
                Handler h = new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        t1.cancel();
                    }

                }, 100);
                //random location on screen
                t1.setGravity(Gravity.TOP, i1, i2);
                t1.show();

                //delay in show next toast
                int min = 3;
                int max = 8;
                Random random = new Random();
                int d = random.nextInt(max - min + 1) + min;
                Log.d("random ", String.valueOf(d));

                mHandler.postDelayed(this, d * 1000);

            }

        }, 100);

【问题讨论】:

  • “数组”里面有什么东西吗?
  • 您想为数组中的每个项目显示一条 Toast 消息吗?我不知道这个数组有多大,但会有大量的 toast 消息。您也可以使用 Arrays.toString(array) 之类的东西在一条 toast 消息中显示孔数组

标签: java android android-toast


【解决方案1】:

您可以使用
指定 Toast 的持续时间 Toast.LENGTH_LONG 和 Toast.LENGTH_SHORT 默认在 android 中。

但 Toast.LENGTH_LONG 持续时间为 1500 毫秒(1.5 秒)
Toast.LENGTH_SHORT 持续时间为 3000 毫秒(3 秒)

1000 毫秒 = 1 秒。

你可以用一个数字代替它们。
500 0.5 秒,
1000 1 秒,
1500 1.5 秒,
2000 2 秒 ,
2500 2.5 秒,
3000 从 3 秒,
或更多
3500 3.5 秒
如你所愿。

【讨论】:

    【解决方案2】:

    在 kotlin 中:

     var allItems = "" //used to display in the toast
     for (str in messageArray)
        {
           allItems = allItems + "\n" + str //adds a new line between items
         }
     Toast.makeText(this,allItems, Toast.LENGTH_SHORT).show()
    

    在 Java 中:

    String allItems = ""; //used to display in the toast
    
    for(String str : messageArray){
    
        allItems = allItems + "\n" + str; //adds a new line between items
    
     }
    
      Toast.makeText(getApplicationContext(),allItems, Toast.LENGTH_LONG).show();
    

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2012-11-27
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      相关资源
      最近更新 更多