//先宣告
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();
}
});
}
總網頁瀏覽量
基礎Note
☪About Me
(1)
免費軟體
(2)
教學
(4)
教學文件
(42)
會計軟體
(1)
電腦系統
(1)
Adapter
(8)
Adobe Premiere
(1)
AlertDialog
(7)
Android App 介紹
(1)
Animation
(1)
API
(2)
APP範例
(1)
Array
(1)
AsyncTask
(1)
Auto Test Case
(32)
AutoCompleteTextView
(1)
Bitmap Drawable
(3)
BroadcastReceiver
(4)
Button
(1)
Codility
(2)
Contact
(4)
DB
(1)
Dialog
(2)
Documents
(1)
Eclipse
(3)
Ellipsize
(1)
File
(4)
Focus
(2)
Fragment
(4)
Gallery
(2)
GIT
(4)
GitHub
(1)
GridView
(8)
HashMap
(1)
HorizontalScrollView
(6)
IIS
(1)
Intent
(3)
IntentService
(1)
Internet
(2)
KeyEvent
(1)
Layout
(1)
ListView
(11)
Log
(1)
Mac / iOS
(11)
Manifest
(1)
Marquee
(2)
Math
(1)
MediaPlayer
(5)
MediaRecorder
(5)
MSMQ
(1)
onClick
(1)
PackageManager
(6)
PHP
(1)
PIS
(3)
PowerManager
(1)
Progress
(2)
SCREEN
(1)
Search
(6)
Service
(1)
SharedPreferences
(3)
SimpleDateFormat
(1)
SonarQube
(1)
Sound Recorder
(1)
Spinner
(2)
SQL server Management
(16)
SQLite
(13)
String
(1)
STS
(5)
SVN
(1)
Thread
(1)
Toast
(3)
Typeface
(1)
Uri
(2)
VB.NET
(17)
VMware
(1)
關於我自己
2014年4月9日 星期三
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()...
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);
}
}
訂閱:
文章 (Atom)



