總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。
顯示具有 Toast 標籤的文章。 顯示所有文章
顯示具有 Toast 標籤的文章。 顯示所有文章

2014年4月9日 星期三

【Fragment, IntentService】在Thread中執行toast

//先宣告
Activity activity;

//當此Fragment被呼叫起來時會執行
@Override
    public void onAttach(Activity activity) {
        if (Log.IS_DEBUG) Log.i(TAG, TAG + " onAttach");
        super.onAttach(activity);
        this.activity = activity;
    }


new Thread() {
    public void run() {

        //在Thread中執行toast
        showToastByRunnable(activity, activity.getResources().getString("invalidCharacter"), Toast.LENGTH_LONG);
    }
}

//自訂showToastByRunnable方法
private void showToastByRunnable(final Context context, final CharSequence text, final int duration)     { 
        Handler handler = new Handler(Looper.getMainLooper()); 
        handler.post(new Runnable() { 
            @Override 
            public void run() { 
                Toast.makeText(context, text, duration).show(); 
            } 
        }); 
    }

2012年1月25日 星期三

想要關掉Toast~請Handler來幫忙Toast.cancel()

Toast.SHORT提示著左邊的ImageButton點選後跳出為期2 秒的Toast
Toast.LONG 提示著右邊的ImageButton點選後跳出為期3.5秒的Toast
按下Cancel Toast Button後可以關閉Toast(必須透過Handler)
但本範例還不夠完善的是, ImageButton交互點選無法立即清掉Thread裡的sleep()...








package com.tsots.CancelToast;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class CancelToast extends Activity
{
  Context context = CancelToast.this;
  ProgressDialog myDialog = null;  
  ImageButton ib1;
  ImageButton ib2;
  Button b3;
  String TAG = "CancelToast.this";
  Toast toast_start;
  Thread thread;
  String called_by = null;
  
  volatile boolean terminate = false;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   
    ib1 = (ImageButton)findViewById(R.id.imagebutton1);
    ib2 = (ImageButton)findViewById(R.id.imagebutton2);
    b3 = (Button)findViewById(R.id.button3);
    
    ib1.setOnClickListener(new ImageButton.OnClickListener()
    {
      public void onClick(View arg0)
      {
        {     
         called_by = "ib1";        
       toast_start = Toast.makeText(context, "Start Short Toast", Toast.LENGTH_SHORT);
            toast_start.show(); 
         ib1.setBackgroundDrawable(getResources().getDrawable(R.drawable.ib_click));
         ib2.setBackgroundDrawable(getResources().getDrawable(R.drawable.ib_unclick));  
            fun_thread();         
        }
      }
 });
    
    ib2.setOnClickListener(new ImageButton.OnClickListener()
    {
      public void onClick(View arg0)
      {
        {    
         called_by = "ib2";         
      toast_start = Toast.makeText(context, "Start Long Toast", Toast.LENGTH_LONG);
            toast_start.show();
         ib1.setBackgroundDrawable(getResources().getDrawable(R.drawable.ib_unclick));
         ib2.setBackgroundDrawable(getResources().getDrawable(R.drawable.ib_click));
            fun_thread();          
        }
      }
 });   
    
    b3.setOnClickListener(new Button.OnClickListener()
   {
    public void onClick(View arg0)
       {
         {         
          called_by = "b3";
             fun_thread();          
         }
       }
   });
  }
  
  public void fun_thread()
  {   
      new Thread()
      { 
        public void run()
        { 
         try
            { 
           if (called_by.equals("ib1"))
           {
           sleep(2000);           
           }
           else if(called_by.equals("ib2"))
           {           
           sleep(3500);
           }     
           else
           {
            Thread.currentThread().interrupt();
            handler.sendEmptyMessage(0);
           }
            }
            catch (Exception e)
            {
              e.printStackTrace();
            }   
            finally
            {
             handler.sendEmptyMessage(0);             
            }
        }         
      }.start(); 
  }

  /*
   * 透過Handler將Toast關閉
   */
  Handler handler = new Handler()
  {
      public void handleMessage(Message msg)
      {       
       toast_start.cancel();
       ib1.setBackgroundDrawable(getResources().getDrawable(R.drawable.ib_unclick));  
       ib2.setBackgroundDrawable(getResources().getDrawable(R.drawable.ib_unclick));  
      }
  };
}

2012年1月15日 星期日

帶有圖片的Toast toast.setView(LinearLayout)









GridViewActivity.java

package com.tsots.GridView_ToastWithPhoto;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PhotoActivity extends Activity 
{
 Context context = PhotoActivity.this;
 ImageAdapter adapter = new ImageAdapter(this);
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview1);
        gridview.setNumColumns(3);
        gridview.setAdapter(adapter);

        gridview.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView parent, View v, int position, long id) 
            {
             toastWithPhoto(position, v);
            }
        });
    }
 
    //實作show出圖片的toast
    public void toastWithPhoto(int position, View v)
    {
     ImageView iv = new ImageView(context);
     photoChangeSize(position, iv);
     TextView tv = new TextView(context);
     tv.setText(String.valueOf(position+1));
     LinearLayout lay = new LinearLayout(context);
     //水平排列
     lay.setOrientation(LinearLayout.HORIZONTAL);
     Toast toast = Toast.makeText(context, tv.getText(), Toast.LENGTH_SHORT);
     View view = toast.getView();
     lay.addView(view);//toast加入文字  
     lay.addView(iv);  //toast加入圖片       
     toast.setView(lay);    
     toast.show();
    }
 
    //改變原圖的size
    public void photoChangeSize(int position, ImageView iv)
    {
     Matrix matrix = new Matrix();
     float scaleWidth = (float) 0.5;
     float scaleHeigh = (float) 0.5;
     matrix.postScale(scaleWidth, scaleHeigh);
     //將int轉成Bitmap
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), adapter.mThumbIds[position]);
     int bmpWidth = bmp.getWidth();
     int bmpHeigh = bmp.getHeight();
     //改變Bitmap的size
     Bitmap resizebmp = Bitmap.createBitmap(bmp,0,0, bmpWidth, bmpHeigh, matrix, true);
     iv.setImageBitmap(resizebmp);
    }
}