Android-toast-詳解-實例-優化

實例:http://www.jb51.net/article/101948.htm

public class MainActivity extends Activity implements OnClickListener {
  Handler handler = new Handler();
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    findViewById(R.id.btnSimpleToast).setOnClickListener(this);
    findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
        this);
    findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
    findViewById(R.id.btnCustomToast).setOnClickListener(this);
    findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
 
  }
 
  public void showToast() {
    handler.post(new Runnable() {
 
      @Override
      public void run() {
        Toast.makeText(getApplicationContext(), "我來自其他線程!",
            Toast.LENGTH_SHORT).show();
 
      }
    });
  }
 
  @Override
  public void onClick(View v) {
    Toast toast = null;
    switch (v.getId()) {
    case R.id.btnSimpleToast:
      Toast.makeText(getApplicationContext(), "默認Toast樣式",
          Toast.LENGTH_SHORT).show();
      break;
    case R.id.btnSimpleToastWithCustomPosition:
      toast = Toast.makeText(getApplicationContext(), "自定義位置Toast",
          Toast.LENGTH_LONG);
      toast.setGravity(Gravity.CENTER, 0, 0);
      toast.show();
      break;
    case R.id.btnSimpleToastWithImage:
      toast = Toast.makeText(getApplicationContext(), "帶圖片的Toast",
          Toast.LENGTH_LONG);
      toast.setGravity(Gravity.CENTER, 0, 0);
      LinearLayout toastView = (LinearLayout) toast.getView();
      ImageView imageCodeProject = new ImageView(getApplicationContext());
      imageCodeProject.setImageResource(R.drawable.ic_launcher);
      toastView.addView(imageCodeProject, 0);
      toast.show();
      break;
    case R.id.btnCustomToast:
      LayoutInflater inflater = getLayoutInflater();
      View layout = inflater.inflate(R.layout.custom,
          (ViewGroup) findViewById(R.id.llToast));
      ImageView image = (ImageView) layout
          .findViewById(R.id.tvImageToast);
      image.setImageResource(R.drawable.ic_launcher);
      TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
      title.setText("Attention");
      TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
      text.setText("完全自定義Toast");
      toast = new Toast(getApplicationContext());
      toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
      toast.setDuration(Toast.LENGTH_LONG);
      toast.setView(layout);
      toast.show();
      break;
    case R.id.btnRunToastFromOtherThread:
      new Thread(new Runnable() {
        public void run() {
          showToast();
        }
      }).start();
      break;
 
    }
 
  }
 
}
優化:http://blog.csdn.net/u012575819/article/details/51194160


使用Toast一般會用下面這條語句:

Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
  • 1
  • 1

但有一個問題,如果頻繁使用toast會導致其長時間顯示。

這和Toast的實現原理是有關係的。 
Toast在調用其show方法時,並不是立刻顯示出來,而是添加到系統的一個隊列中。 
而在這個隊列中,先添加進去的Toast先被顯示出來,所以說,剛show的Toast並不會立刻顯示,而是要等隊列中之前被添加的Toast顯示完之後纔可以顯示。 
而這個隊列中是可以保存相當多數量的Toast的,如果你一次性new出多個Toast並將它們都show了一遍,系統就會依次一個個將這些Toast全部顯示一遍,時間很長,可能你應用都退了,但是Toast還在顯示。

(PS:這種情況並不少見。可能你設置用戶點擊一次按鈕顯示一次Toast,誰也不能保證用戶不會一直狂按這個按鈕。) 
這樣的情況其實是很影響用戶體驗的。 
但有解決辦法。

先貼代碼:

private static Toast myToast;
public static void showToast(Context context, String str) {
        if(myToast == null) {
            myToast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
        } else {
            myToast.setText(str);
        }
        myToast.show();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在new一個Toast之前先判斷這個myToast是不是爲NULL,如果不是NULL,就不要再去new了,以免隊列中存放過多的Toast。 
如果不爲NULL,只是改變了myToast的顯示文字,並調用show()方法。 
這樣就避免隊列中產生大量Toast了。

另外,貼兩個自定義Toast的代碼:

這是一個添加了一張圖片的Toast。 
其實看Toast的源碼就知道,Android默認的Toast的佈局就是一個LinearLayout裏面一個TextView。 
使用:

(LinearLayout)Toast.getView();
  • 1
  • 1

拿到這個View並強轉成LinearLayout就可以隨意向其中增加或移除View了。

private static Toast myIconToast;
public static void showIconToast(Context context, String str, int resId) {

        if(myIconToast == null) {
            myIconToast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
            myIconToast.setGravity(Gravity.CENTER, 0, 0);
            LinearLayout toastLinearLayout = (LinearLayout) myIconToast.getView();
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(resId);
            toastLinearLayout.addView(imageView, 0);
        } else {
            myIconToast.setText(str);
            LinearLayout toastLinearLayout = (LinearLayout) myToast.getView();
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(resId);
            toastLinearLayout.removeViewAt(0);
            toastLinearLayout.addView(imageView, 0);
        }
        myIconToast.show();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

第二個是一個完全自定義的Toast,佈局由我們自己定義, 
利用LayoutInflater拿到我們自定義的佈局後,使用:

Toast.setView()
  • 1
  • 1

即可實現自定義佈局的Toast。

private static Toast myCustomToast;
public static void showCustomToast(Activity activity, String title, String text, int resId) {

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

        TextView titleView = (TextView) layout.findViewById(R.id.tvTitleToast);
        titleView.setText(title);
        ImageView imageView = (ImageView) layout.findViewById(R.id.tvImageToast);
        imageView.setImageResource(resId);
        TextView textView = (TextView) layout.findViewById(R.id.tvTextToast);
        textView.setText(text);

        if(myCustomToast == null) {
            myCustomToast = new Toast(activity);
            myCustomToast.setGravity(Gravity.RIGHT | Gravity.TOP, 300, 300);
            myCustomToast.setDuration(Toast.LENGTH_SHORT);
            myCustomToast.setView(layout);
        } else {
            myCustomToast.setView(layout);
        }
        myCustomToast.show();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

需要注意的是,這幾個方法裏使用的Toast都是成員變量。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章