Toast
import android.widget.Toast;
Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_SHORT).show();
第三个参数:Toast.LENGTH_SHORT或Toast.LENGTH_LONG
全局:
Toast.makeText(getApplicationContext(), "finished!", Toast.LENGTH_SHORT).show();
子线程中:
import android.os.Handler;
import android.os.Message;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) { //判断标志位
case 0:
Toast.makeText(getApplicationContext(), "finished!", Toast.LENGTH_SHORT).show();
break;
}
}
};
new Thread(new Runnable() {
@Override
public void run() {
Message msg = new Message();
msg.what=0; //标志消息的标志
handler.sendMessage(msg);
}
}).start();
快捷代码:
public void MakeToast(String str) { android.os.Message msg = new android.os.Message(); msg.what = TOAST; Bundle bundle = new Bundle(); bundle.putString("message", str); msg.setData(bundle); myhandler.sendMessage(msg); } public Handler myhandler = new Handler() { public void handleMessage(android.os.Message msg) { super.handleMessage(msg); switch (msg.what) { //判断标志位 case TOAST: Toast.makeText(getApplicationContext(), msg.getData().getString("message"), Toast.LENGTH_SHORT).show(); break; } } };