- 浏览: 320343 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
crxiang:
写得很好啊,最近正好需要这个,谢谢分享了!
Android中ProgressDialog的简单示例 -
shangs2010:
分析的真仔细,多谢!
SharePreference类似于JavaEE中的session -
guochongcan:
fantaxy025025 写道兄弟,你这个不行的。
只能删 ...
MYSQL用一条SQL语句删除重复记录 -
fantaxy025025:
兄弟,你这个不行的。只能删除 重复个数是2的,如果重复个数大于 ...
MYSQL用一条SQL语句删除重复记录 -
wujiandong:
不错,多写点这方面的东西~~,加油~~,哥们~~
布局学习——妙用TabHost
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。
1.默认效果
代码
Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();
2.自定义显示位置效果
代码
toast = Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3.带图片效果
代码
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.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4.完全自定义效果
代码
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.icon);
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();
5.其他线程
代码
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
1.Main,java
package com.wjq.toast;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Main 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.icon);
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.icon);
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;
}
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" android:gravity="center"> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnSimpleToast" android:text="默认"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="自定义显示位置" android:id="@+id/btnSimpleToastWithCustomPosition"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage" android:text="带图片"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="完全自定义" android:id="@+id/btnCustomToast"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="其他线程" android:id="@+id/btnRunToastFromOtherThread"></Button> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="#ffffffff" android:orientation="vertical" android:id="@+id/llToast" > <TextView android:layout_height="wrap_content" android:layout_margin="1dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:gravity="center" android:background="#bb000000" android:id="@+id/tvTitleToast" /> <LinearLayout android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/llToastContent" android:layout_marginLeft="1dip" android:layout_marginRight="1dip" android:layout_marginBottom="1dip" android:layout_width="wrap_content" android:padding="15dip" android:background="#44000000" > <ImageView android:layout_height="wrap_content" android:layout_gravity="center" android:layout_width="wrap_content" android:id="@+id/tvImageToast" /> <TextView android:layout_height="wrap_content" android:paddingRight="10dip" android:paddingLeft="10dip" android:layout_width="wrap_content" android:gravity="center" android:textColor="#ff000000" android:id="@+id/tvTextToast" /> </LinearLayout> </LinearLayout>
发表评论
-
使用 SharedPreferences
2013-01-31 21:37 1173SharedPreferences是一种轻量级的数据存储 ... -
android:windowSoftInputMode属性详解
2012-11-01 07:36 799android:windowSoftInputMode ac ... -
自定义ListView中的分割线
2012-11-01 07:12 2266ListView中每个Item项之间都有分割线,设置andro ... -
Android中Bitmap和Drawable
2012-10-24 17:54 952一、相关概念 1、Drawable就是一个可画的对象,其可能 ... -
在EditText/TextView中插入表情图片、样式、下划线等
2012-09-23 20:03 2659EditText: 通常用于显示 ... -
Android开发:在EditText中关闭软键盘
2012-09-23 14:41 17651、EditText有焦点(focusable为true) ... -
Android 字体和颜色
2012-06-23 18:34 939对于能够显示文字的控件(如TextView EditText ... -
解决Eclipse Indigo(3.7)中文字体偏小问题
2012-06-04 20:33 1075===========转============= ... -
Android中Cursor 的一些方法
2012-05-16 17:15 1411close() //关闭游标,释放资源 copyStrin ... -
ContentProvider分析
2012-05-15 22:24 972红色部分较重要的 private static ... -
Java的log 的几种表示颜色
2012-05-09 16:12 0Log下面的日志颜色 E Error 红 ... -
android单元测试时,异常情况解决记录
2012-04-07 20:26 1502异常内容 java.lang.NoClassDefFo ... -
Android_SDK及ADT升级方法
2012-04-05 10:57 25377本文只讲如何 ... -
android 退出程序 对话框提醒
2012-03-17 20:23 0if (keyCode == KeyEvent.KEYCO ... -
DBA应该具有什么样的素质?——转
2012-03-17 20:18 0问题起源于在写一份材 ... -
抓住移动互联网发展最佳时机,开发最好的移动应用程序,为用户提供最佳的软件服务
2012-03-04 20:03 0抓住移动互联网发展最佳时机,开发最好的移动应用程序,为用户提供 ... -
Android WebView 浏览器
2012-01-01 09:38 4160WebView的使用很方便。在学习WebView之前,我 ... -
android ImageView的scaleType属性
2011-12-27 13:07 1157ImageView:Displays an arbitra ... -
程序实现线性布局相关
2011-12-26 15:42 1013程序实现线性布局相关1、 LinearLayout l ... -
android 中checkbox的使用
2011-12-22 16:59 1853主要是讲checkbox的使用,其实接触过html都应该知道表 ...
相关推荐
Android Toast各种使用方法及DEMO
Android_Toast用法
实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...
PhoneGap android的Toast插件,使用方法不用介绍了,用phoneGap开发android应用的人都知道,希望对大家有帮助。
Android学习笔记之Button,Toast,menu的简单用法
Android Notification消息框 Toast弹出框用法演示范例,本例中关于 Toast弹出框的演示,演示了适时的 Toast和长时间的 Toast,关于Notification的定义,则演示了高级Notification的用法,自定义4种Notification的...
Android 演示简单toast和带图片toast的实现方法,这些toast在平时的Android应用开发中使用频繁,本源码演示了两种最实用toast的用法,一种是不带图片,另一种是带图片: // 简单的toast,不带图片的实现方法: ...
一个关于安卓toast和notification使用方法的Android源码,来自eoeandroid社区,对初学者或许是个帮助吧。
android之Toast用法详解(各种自定义Toast)实例
在前面的实例中,已经应用过Toast类来显示一个简单的提示框了。这次将对Toast进行详细介绍。Toast类用于在屏幕中显示一个消息提示框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一段时间后自动消失。...
本篇文章主要介绍了Android Toast的用法总结(五种用法),android toast几种使用方法 toast经常会用到,今天做个总结,特别是自定义toast的布局,值得一看。
开发android的同学可能会抱怨Toast设定显示的时长无效...本文使用方法三进行实现,难度不大,直接看代码吧. package com.open.toast; import android.content.Context; import android.graphics.Color; import andro
摘要:Java源码,Android,Toast,Android源码 Android Toast用法代码实例,建立属于你... Toast在Android系统中用于向用户显示一些帮助/提示,本源码是一个较基本的Toast用法实例,还望通过本例能加深您对Toast的理解。
本文实例讲述了Android Toast通知用法。分享给大家供大家参考,具体如下: Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。 1.默认用法 代码如下:Toast.makeText(getApplicationContext(), ...
Android 自定义 Toast 显示时间 实现代码: ... import android.content.... * 使用方法 * 1.先初始化类 MyToast myToast = new MyToast(this); * 2.显示消息 myToast.setText(要显示的内容); //设置要显示的内容 *
常规使用方法这里不做说明,继前一篇博客《Android中Toast全屏显示》 ,其中抛砖引玉的给出一个简单的实现Toast全屏显示的方法后,发现无法控制Toast的显示时长。虽然Toast中有setDuration(int duration)接口,但是...
最近学习的toast用法,包括自定义位置toast和带图片的toast
Android中定义了一个Toast对象,用以弹出一个窗口来给予用户帮助和提示,和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用户的焦点,在弹出Toast的时候,依然可以对之前的界面进行操作,我们在“”...
看到Toast有一个cancel()方法: 代码如下:void cancel() Close the view if it’s showing, or don’t show it if it isn’t showing yet. 做程序员的,基本一看api就知道,用这个可以取消上一个toast的显示,然后...
使用自定义Toast,首先我们需要添加一个布局文件,该布局文件的结构和Activity使用的布局文件结构一致,在该布局文件中我们需设计我们Toast的布局,例如: 代码如下: <?xml version=”1.0″ encoding=”utf-8″?&...