- 浏览: 87119 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (56)
- Flex (10)
- Android SQLite (3)
- Android (16)
- ListView (1)
- Android 多线程 (1)
- Android Google Maps (1)
- Flex 基础 (1)
- Android ImageView (0)
- Android WebService (0)
- Android 杂项 (2)
- Android 主题与样式 (1)
- Android 数据库 (1)
- Android 短信 (2)
- Ubuntu 11.04 for 64 (2)
- SharedPreferences (2)
- manifest (1)
- 杂项 (9)
- Android 网络 (1)
最新评论
-
DUANLESINIAN:
楼主配置文件讲的不清不楚,应该是这样的:<applica ...
在任意位置获取应用程序Context -
andytang_bin:
我QQ 912180974
解决 Android 下载中文文件名问题 -
andytang_bin:
还是不行。 能Q上联系吗?!
解决 Android 下载中文文件名问题
Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明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;
}
}
}
2.main,xml
<?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>
3.custom.xml
<?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>
发表评论
-
Android开发之ListView 适配器(Adapter)优化
2011-05-14 15:56 1015ListView的Adapter的作用如下图所示: Adap ... -
Android上面TreeView效果
2011-05-14 11:17 1508应该说很多的操作系统上面都提供了TreeView空间,实现树 ... -
Android软键盘学习InputMethodManager
2011-05-14 11:14 4090当我们在Android提供的EditText中单击的时候,会自 ... -
Android WakeLock
2011-05-12 22:50 1413在Android中,申请WakeLock可以让你的进程持续执行 ... -
Android中的Handler使用总结
2011-04-21 16:37 688在Android的UI开发中,我们经常会使用Handler来控 ... -
Android多线程及异步处理问题 【转载】
2011-04-17 23:53 20141、问题提出 1)为何需要多线程? 2)多线程如何实现? 3) ... -
EditText的属性说明
2011-04-14 11:47 905EditText的属性很多,这 ... -
Android 系统自带的图片
2011-04-14 11:43 1330This is a list of resources in ... -
Android Check GPS is Enabled or Not
2011-04-13 21:21 0在 Android 中, 我们可以使用 LocationMan ... -
Android ListView with Searchbox Sort items
2011-04-13 16:11 0这里我们有一个搜索框, 当它里面的内容与列表项匹配时... ... -
Android ListView Multiple Choice Example
2011-04-13 14:28 0在 Andoird 中 ListView 通常用来显示一个列表 ... -
TextView 不用 ScrollViewe 也可以滚动
2011-04-12 16:13 1036具体步骤: 第一步: xml 文件 <Text ... -
Activity...
2011-04-10 17:03 1115一、常用类: 1. Activity 是最基本的类,它 ... -
Android2.1中的 drawable(hdpi,ldpi,mdpi) 的区别
2011-03-24 21:37 957在之前的版本中,只有一个drawable,而2.1版本中有dr ... -
ADB常用命令
2011-03-24 21:36 967ADB(Android Debug Bridge)是A ... -
在任意位置获取应用程序Context
2011-03-24 21:35 1701在 Android 程序中访问资源时需要提供 Cont ... -
如何控制Android不锁屏
2011-03-22 14:51 2124锁定屏幕对于移动终端来说是非常有必要的,但是对于机顶盒产品 ... -
Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
2011-03-19 18:23 4182SharedPreferences是Android中存储简单数 ...
相关推荐
### Android Toast 大全(五种情形) #### 一、概览 在Android开发中,`Toast`是一种轻量级的提示方式,主要用于快速显示简短的信息,如操作结果、临时提示等。它不会阻塞UI线程,也不需要用户进行任何交互即可...
通过上述五种不同情形的介绍,可以看出Android Toast的使用非常灵活,可以根据不同的需求场景选择合适的Toast使用方式,从而提升应用的用户体验。在实际开发中,开发人员可以根据具体的应用需求,选择合适的Toast...
下面我们将详细讲解如何实现标题中提到的五种`Toast`情形。 1. **默认效果**: 默认的`Toast`样式是系统预设的,只需调用`makeText()`方法并传入上下文、显示文本和持续时间即可。如: ```java Toast.makeText...
在Android开发中,`...以上就是Android中`Toast`的五种常见用法,包括默认样式、自定义位置、带图片、完全自定义布局以及在其他线程中显示。理解并掌握这些用法,能让你在开发中更灵活地使用`Toast`来提升用户体验。
scratch少儿编程逻辑思维游戏源码-皮博冒险者.zip
少儿编程scratch项目源代码文件案例素材-这是之前下载的测试.zip
scratch少儿编程逻辑思维游戏源码-汽车冲突.zip
scratch少儿编程逻辑思维游戏源码-梦幻岛 3D.zip
scratch少儿编程逻辑思维游戏源码-收集水果.zip
炫酷蓝色响应式投稿说明源码.zip
机器学习算法与应用大作业-基于预处理的小麦品种的分类和聚类源码+数据+使用说明.zip是个人经导师指导并认可通过的高分设计项目,评审分98分。主要针对计算机相关专业的正在做大作业的学生和需要项目实战练习的学习者,可作为课程设计、期末大作业。。内容来源于网络分享,如有侵权请联系我删除。
vs2019_Qt5.12.12编译好的ffmpeg库,因为是c接口,别的版本理论也可以用
scratch少儿编程逻辑思维游戏源码-欧力多.zip
少儿编程scratch项目源代码文件案例素材-越野运动员.zip
GUI开发_CMake_MSVC_CLion_ElaWidgetTools_跨平台界面组件库_简化项目结构_降低上手难度_提供完整编译环境配置_支持Windows11开发_快速集成
scratch少儿编程逻辑思维游戏源码-时间先生.zip
少儿编程scratch项目源代码文件案例素材-爪猫使命召唤.zip
内容管理系统_SpringBootThymeleaf双引擎解析_梦想家CMS开源建站系统_面向政府企业组织快速搭建展示型网站如企业官网技术博客信息门户等解决建站成本高周期长问题
健康监测与运动数据自动化_云函数部署与定时任务管理_乐心健康APP账号绑定与步数同步_通过腾讯云函数和青龙面板实现自动刷步并同步至蚂蚁森林获取每日296g能量的智能脚本系统_适用于
动态雨滴玻璃掉落个人主页源码.rar