23

これは、Toast500ミリ秒の間表示する必要がある方法です。しかし、それは1秒以上を示しています。

Toast.makeText(LiveChat.this, "Typing", 500).show(); 

Toast500ミリ秒だけ表示するにはどうすればよいですか?

4

12 に答える 12

71

これはできません。より短い長さのトーストを表示するには、Toast.LENGTH_SHORT希望する時間の後にそれをキャンセルする必要があります。何かのようなもの:

final Toast toast = Toast.makeText(getApplicationContext(), "This message will disappear in half a second", Toast.LENGTH_SHORT);
    toast.show();

    Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               toast.cancel(); 
           }
    }, 500);
于 2013-01-24T14:36:16.907 に答える
3

@Senthの回答に加えて、同じメッセージでshowToastメソッドを複数回呼び出したときに時間を累積したくない場合は、次のようにします。

private Toast mToastToShow = null;
String messageBeingDisplayed = "";

/**
 * Show Toast message for a specific duration, does not show again if the message is same
 *
 * @param message     The Message to display in toast
 * @param timeInMSecs Time in ms to show the toast
 */
public void showToast(String message, int timeInMSecs) {
    if (mToastToShow != null && message.equals(messageBeingDisplayed)) {
        Log.d("DEBUG", "Not Showing another Toast, Already Displaying");
        return;
    } else {
        Log.d("DEBUG", "Displaying Toast");
    }
    messageBeingDisplayed = message;
    // Set the toast and duration
    int toastDurationInMilliSeconds = timeInMSecs;
    mToastToShow = Toast.makeText(this, message, Toast.LENGTH_LONG);

    // Set the countdown to display the toast
    CountDownTimer toastCountDown;
    toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, timeInMSecs /*Tick duration*/) {
        public void onTick(long millisUntilFinished) {
            if (mToastToShow != null) {
                mToastToShow.show();
            }
        }

        public void onFinish() {
            if (mToastToShow != null) {
                mToastToShow.cancel();
            }
            // Making the Toast null again
            mToastToShow = null;
            // Emptying the message to compare if its the same message being displayed or not
            messageBeingDisplayed = "";
        }
    };

    // Show the toast and starts the countdown
    mToastToShow.show();
    toastCountDown.start();
}

次のように、トーストを500ミリ秒間表示できます。

showToast("Not Allowed", 500);
于 2017-02-22T14:08:38.880 に答える
2

私はこの答えを見つけました。少し複雑ですが、Toast.LENGTH_LONGよりも長いトーストを作成することもできます。ティック期間を1000msから500msに変更する必要がある場合があります。

private Toast mToastToShow;
public void showToast(View view) {
   // Set the toast and duration
   int toastDurationInMilliSeconds = 10000;
   mToastToShow = Toast.makeText(this, "Hello world, I am a toast.", Toast.LENGTH_LONG);

   // Set the countdown to display the toast
   CountDownTimer toastCountDown;
   toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
      public void onTick(long millisUntilFinished) {
         mToastToShow.show();
      }
      public void onFinish() {
         mToastToShow.cancel();
         }
   };

   // Show the toast and starts the countdown
   mToastToShow.show();
   toastCountDown.start();
}

仕組みは次のとおりです。カウントダウンの通知時間は、フラグに従ってトーストが表示される時間よりも短いため、カウントダウンが終了していなくてもトーストを再度表示できます。トーストが画面に表示されているときに再び表示された場合、トーストは点滅せずにその間ずっとそこにとどまります。カウントダウンが終了すると、トーストはキャンセルされ、表示時間が終了していなくても非表示になります。

これは、トーストをデフォルトの期間よりも短い期間表示する必要がある場合でも機能します。カウントダウンが終了すると、最初に表示されたトーストは単にキャンセルされます。

于 2015-12-30T16:38:36.817 に答える
1

標準のトーストでは、求めていることを実行できません。おそらく、より優れたToastオプション(Croutonという名前)を提供するサードパーティライブラリの統合を検討する必要があります。自分で使ったことはありませんが、気に入っているようです。

標準OSではトーストの長さを制御することはできません。

クルトンリンク: https ://github.com/keyboardsurfer/Crouton

于 2013-01-24T14:38:59.350 に答える
1

これは私にとってはうまく機能しています。

final Toast mToastToShow;
            int toastDurationInMilliSeconds = 10000;
            mToastToShow =  Toast.makeText(getApplicationContext(), "Snapshot Saved Successfully.",Toast.LENGTH_LONG);


            // Set the countdown to display the toast
            CountDownTimer toastCountDown;
            toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
                public void onTick(long millisUntilFinished) {
                    mToastToShow.show();
                }
                public void onFinish() {
                    mToastToShow.cancel();
                }
            };

            // Show the toast and starts the countdown
            mToastToShow.show();
            toastCountDown.start();

カウントダウンは、特定の期間のトーストメッセージを表示するために使用されます。

于 2017-01-17T12:39:55.653 に答える
0

これはできません。との値は0Toast.LENGTH_SHORTToast.LENGTH_LONG1です。これは、実際の期間ではなくフラグとして扱われることを意味するため、期間をこれらの値以外に設定することはできないと思います。

于 2013-01-24T14:39:12.183 に答える
0

私はこれができるとは思わない、あなたは使うことができるだけであるToast.LENGTH_LONGか、Toast.LENTH_SHORTあなたはあなたの知っている速度を定義することができない。

于 2013-01-24T14:35:04.770 に答える
0

最初に試してください。これにより、トーストがミリ秒単位の特定の期間に設定されます。

public void toast(int millisec, String msg) {
    Handler handler = null;
    final Toast[] toasts = new Toast[1];
    for(int i = 0; i < millisec; i+=2000) {
        toasts[0] = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toasts[0].show();
        if(handler == null) {
            handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    toasts[0].cancel();
                }
            }, millisec);
        }
    }
}
于 2015-08-26T10:29:50.323 に答える
0

別の方法を試しましたが、この方法でうまくいきます

 final Toast mytoast = Toast.makeText(getApplicationContext(), jsonObject.getString("response_message"), Toast.LENGTH_SHORT);
 mytoast.show();

                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mytoast.cancel();
                            }
                        }, 5000);// 5 sec
于 2017-12-06T07:28:05.967 に答える
0

ドロイド側にToastMessageクラスを作成しました。

   public class ToastMessage: IToast
        {
            public void LongAlert(string message)
            {
                Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short);
                toast.Show();
                Device.StartTimer(TimeSpan.FromSeconds(0.5), () =>
                {               
                   toast.Cancel();
                    return false;
                });
            }
        }

インターフェイスIToastを作成しました

 public  interface IToast
    {
        void LongAlert(string message);
    }

依存関係サービスによる呼び出し

 DependencyService.Get<IToast>().LongAlert("Right Answer");
于 2018-05-22T07:27:37.307 に答える
0

受け入れられた答えは正しいですが、私の場合、トーストの点滅(表示と非表示)に気付くことができます。

トーストが点滅しない解決策があり、トーストをカスタマイズすることもできます。

さぁ、始めよう、

1)LongToastという名前のクラスを作成します。

class LongToast {

private LongToast() {}

static void makeLongToast(Context context,String text, long durationInMillis) 
{

 final Toast toastMessage = new Toast(context);

 //Creating TextView.
 TextView textView = new TextView(context);

 //Setting up Text Color.
 textView.setTextColor(Color.parseColor("#fafafa"));

 //Setting up Text Size.
 textView.setTextSize(17);

 //Setting up Toast Message Text.
 textView.setText(text);

 //Add padding to Toast message.
 textView.setPadding(20, 20, 20, 23);

 //Add Gravity TextView.
 textView.setGravity(Gravity.CENTER);

 //Adding TextView into Toast.
 toastMessage.setView(textView);

 //Access toast message as View.
 View toastView = toastMessage.getView();

 //Set Custom Background on Toast.
 toastView.setBackgroundResource(R.drawable.test);


 new CountDownTimer(durationInMillis, 1000)
  {
   public void onTick(long millisUntilFinished)
   {
    toastMessage.show();
   }
  public void onFinish()
   {
    toastMessage.cancel();
   }

  }.start();
 }
}

2)トーストをカスタマイズするための描画可能なxmlを作成します。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
 <shape android:shape="rectangle">
 <solid android:color="#009973"/>
 <corners android:radius="20dp" />
 <stroke
  android:width="4dp"
  android:color="#01ffc0"
 />
</shape>

必要に応じてトーストをカスタマイズできます。

3)最後にトーストを呼び出します。

LongToast.makeLongToast(this,"whatever you want",10000);//duration in seconds

ref:ここをクリックして確認してください

ありがとう!!。

于 2019-03-29T10:24:54.273 に答える
-3
Toast.makeText(LiveChar.this,"Typing",Toast.LENGTH_SHORT);

これがあなたができる唯一の方法です。

于 2013-01-24T14:35:48.560 に答える