2013-05-28 66 views
1

我遇到一些問題時,我試圖顯示自定義吐司(使用圖標和消息)。這是代碼。更改自定義吐司中的文本和圖標?

private void makeToast(String text, int icon) { 

     LayoutInflater inflater = getLayoutInflater(); 
     View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.message)); 
     ((TextView) layout.findViewById(R.id.message)).setText(text); 

     layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.icon)); 

     // 0 - Exclamation, 1 - Noow icon 
     if(icon == 0){ 
      ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.exclamation); 
     }else if(icon == 1){ 
      ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.nicon); 
     } 


     Toast toast = new Toast(getBaseContext()); 
     toast.setDuration(Toast.LENGTH_LONG); 
     toast.setView(layout); 
     toast.show(); 
    } 

我想更改圖標和消息時,第二個參數是0或1。我知道這個問題是在調用第二佈局,但我不知道如何解決它。

謝謝你。

編輯:

我打電話從onPostExecute中的AsyncTask此方法。我忘了說這個。

Thread [<11> AsyncTask #1] (Suspended (exception RuntimeException)) 
    ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: not available 
    ThreadPoolExecutor$Worker.run() line: not available 
    Thread.run() line: not available  

編輯2:

@Override 
protected void onProgressUpdate(Void... values) { 
    super.onProgressUpdate(values); 
    makeToast("loading nightclubs...", 1); 
    } 
} 

編輯3:

@Override 
     protected void onPostExecute(List<Category> result) { 
      Log.i("result", result.toString()); 

      // Cargamos el listado a pasarle a categoryId en la 
      // consulta 
      for (int k = 0; k < result.size(); k++) { 
       ALLOFTHEM += result.get(k).getId(); 
       if (k < result.size() - 1) { 
        ALLOFTHEM += ","; 
       } 
      } 

      Log.i("POST", ALLOFTHEM); 
     } 

解決!!!

我用...

runOnUiThread(new Runnable() { 
       public void run() { 
       makeToast("loading nightclubs...", 1); 
       } 
      }); 

...每次我想在UI敬酒和它的作品好。

感謝大家。

+3

是什麼錯誤?把你的logcat的消息 –

+0

好,一秒^^對不起 – MAOL

+2

加滿日誌.. –

回答

0

你吹你的佈局兩次,在第一個,它被丟棄設置文本:

View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.message)); 
((TextView) layout.findViewById(R.id.message)).setText(text); 

// you discard the previously inflated layout, for which you have set text 
layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.icon)); 

你想大概是什麼是這樣的:

View layout = inflater.inflate(R.layout.custom_toast, null); 
((TextView) layout.findViewById(R.id.message)).setText(text); 

,然後你的代碼設置圖標。

+0

謝謝,現在如果我從主線程調用它顯示。但是,如果我嘗試從onPostExecute方法中顯示,Logcat會向我說明上述評論中的錯誤。 – MAOL

+0

@Victor_J_Martin你可以發佈你的'onPostExecute()'方法嗎? – Vladimir

+0

添加了代碼。 – MAOL

5

我的自定義吐司解決方案:

public class MyToast { 
    public static void show(Context context, String text, boolean isLong) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View layout = inflater.inflate(R.layout.toast_layout, null); 

     ImageView image = (ImageView) layout.findViewById(R.id.toast_image); 
     image.setImageResource(R.drawable.ic_launcher); 

     TextView textV = (TextView) layout.findViewById(R.id.toast_text); 
     textV.setText(text); 

     Toast toast = new Toast(context); 
     toast.setGravity(Gravity.CENTER, 0, 0); 
     toast.setDuration((isLong) ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT); 
     toast.setView(layout); 
     toast.show(); 
    } 
} 

而且toast_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/toast_border" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <ImageView 
     android:id="@+id/toast_image" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/app_name" 
     android:layout_marginRight="10dp" /> 

    <TextView 
     android:id="@+id/toast_text" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:textColor="#FFF" /> 

</LinearLayout> 

而且toast_border.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <stroke 
     android:width="2dp" 
     android:color="#ee777777" /> 

    <solid android:color="#ee444444" /> 

    <padding 
     android:bottom="2dp" 
     android:left="20dp" 
     android:right="20dp" 
     android:top="2dp" /> 

    <corners android:radius="5dp" /> 
</shape> 

結果圖像:

enter image description here

我希望我已經幫你!

+2

不,你沒有幫助..他問的不同:D –

+0

如果我從非ui線程調用它將失敗。謝謝 – MAOL

+0

@ Victor ....如果你從一個非ui線程調用一個普通的toast ...那也將失敗....如果你使用異步任務,你將它稱爲正在進行的更新事件! – Devrath

0

用這樣的方式:

 LayoutInflater inflater = getLayoutInflater(); 
     View layout = inflater.inflate(R.layout.custom_toast_layout, null); 

     TextView text = (TextView) layout.findViewById(R.id.toast_message); 
     ImageView img = (ImageView) layout.findViewById(R.id.imageView1); 

     if (icon == 0) { 
      text.setText("icon 0"); 
      img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); 
     } 

     if (icon == 1) { 
      text.setText("icon 1"); 
      img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_one)); 
     } 

     Toast toast = new Toast(getApplicationContext()); 
     toast.setGravity(Gravity.BOTTOM, 0, 0); 
     toast.setDuration(Toast.LENGTH_LONG); 
     toast.setView(layout); 
     toast.show(); 
0

更改onPostExecute()方法

protected void onPostExecute(String file_url) { 
      // anything else; you want to do here 

      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
       makeToast("loading nightclubs...", 1); 
       } 
      }); 

     }