2014-04-02 71 views
0

我是Android新手,我想在Toast看到我的消息,但它顯示了一段時間。如何顯示Android敬酒時間較長

我想顯示消息,例如直到一個小時或更多。

+0

使用AlertDialog這一點。 –

+0

[Toast.LENGTH \ _LONG可以長於Toast.LENGTH?_LONG]的可能的重複嗎?(http://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-全長) –

回答

1

不,你不能。使用自定義對話框並在需要時關閉它。但是我想知道你爲什麼要展示這樣一個長時間的彈出式。

我會建議重新考慮你的設計。

您可能還需要檢查Crouton

https://github.com/keyboardsurfer/Crouton

0

嘗試使用,而不是麪包對話框

SingleButtton.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View arg0) { 
     // Creating alert Dialog with one Button 

     AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle("Alert Dialog"); 

     // Setting Dialog Message 
     alertDialog.setMessage("Welcome to Android Application"); 

     // Setting Icon to Dialog 
     alertDialog.setIcon(R.drawable.tick); 

     // Setting OK Button 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog,int which) 
        { 
         // Write your code here to execute after dialog closed 

        } 
       }); 

     // Showing Alert Message 
     alertDialog.show(); 

    } 
}); 
0

LENGTH_SHORT and LENGTH_LONG值是0 and 1。他們被視爲標誌因此,我認爲除此之外不可能設定時間。

0

你可以試試這個:

編輯:

int time = 1000*60 // 1 hour 
for (int i=0; i < time; i++) 
{ 
    Toast.makeText(this, "Your msg", Toast.LENGTH_LONG).show(); 
} 
+0

'Toast.LENGTH_LONG'是最大的。不能超過那 – Raghunandan

+0

我試過這個,但它不起作用 –

+0

不,它不這樣工作。請參閱wqrahd的答案以獲得解釋。 –

0

嗯,就像這裏說的,有做這種沒有正確的方法。

但是,有一種破解 - 只需在for-loop中運行您的吐司,並且迭代量將控制長度。例如 - 運行循環兩次(如下)會使時間加倍。運行3次會使長度增加三倍。再次,它只是一個變通的作品:-)

for (int i=0; i < 2; i++) 
{ 
     Toast.makeText(this, "test", Toast.LENGTH_LONG).show(); 
} 

你必須考慮到,它也有缺陷 - 它在用戶退出循環結束之前,應用程序將繼續呈現,並,在某些設備上,Toast可能會在每次迭代之間閃爍。所以,由你決定!

0

敬酒的目的是在一段時間內顯示一個簡單的信息。你很長時間不能展示它。您可以使用對話框爲Toast消息定製自己的用戶界面。

public static void showCustomToast(final Activity mActivity,final String helpText,final int sec) { 
    if(mActivity != null){ 
mActivity.runOnUiThread(new Runnable() { 
@Override 
    public void run() { 
     int mSec = 3000; 
     if(sec != 0){ 
      mSec = sec; 
    } 
    LayoutInflater inflater = mActivity.getLayoutInflater(); 
    View messageDialog = inflater.inflate(R.layout.overlay_message, null); 
    layer = new CustomLayout(mActivity); 
    LayoutParams params = new            LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    messageDialog.setLayoutParams(params); 
    TextView message = (TextView) messageDialog.findViewById(R.id.messageView); 
    Button okBtn = (Button) messageDialog.findViewById(R.id.messageOkbtn); 
    if(okBtn != null){ 
    okBtn.setVisibility(View.GONE); 
    } 
    message.setText(helpText); 
    final Dialog dialog = new Dialog(mActivity,R.style.ThemeDialogCustom); 
    dialog.setContentView(messageDialog); 
    dialog.show(); 
    final Timer t = new Timer(); 
    t.schedule(new TimerTask() { 
    @Override 
    public void run() { 
    if(dialog.isShowing()){ 
    dialog.dismiss(); 
    } 
    t.cancel(); 
    } 
    },mSec); 
    } 
    }); 
    } 
} 

For referance

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); 
     } 
    } 
}