Android学习之Toast
来源:互联网 发布:中网数据创始人孙远根 编辑:程序博客网 时间:2024/04/26 13:13
Toast用于向用户显示一些帮助/提示。平时我们常用的全文字和图文混排两种风格,其实现将在下面介绍:
1 主Acitivty的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="纯文字的Toast" android:textSize="20sp" /> <Button android:id="@+id/btn_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="图文混排的Toast" android:textSize="20sp" /></LinearLayout>
2 主Activity的实现
package com.example.toast;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private Button btn_1 = null;private Button btn_2 = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);inital();setBtnOnClickListener();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}private void inital() {btn_1 = (Button) findViewById(R.id.btn_1);btn_2 = (Button) findViewById(R.id.btn_2);}private void setBtnOnClickListener() {btn_1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubshowToast(1, MainActivity.this, "今天天气挺不错的~", Toast.LENGTH_SHORT);}});btn_2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubshowToast(2, MainActivity.this, "考完试了,心情不错 啊!!!!!", Toast.LENGTH_SHORT);}});} /** * 显示toast * @param style 设置显示风格,1表示默认 2表示自定义 * @param context * @param text 设置需要显示的文字 * @param duration 设置需要显示的时长 */private void showToast(int style, Context context, String text, int duration) {switch (style) {case 1:Toast.makeText(context, text, duration).show();break;case 2://通过反射获取自定义的ViewView view = getLayoutInflater().inflate(R.layout.toast_item, null);TextView tv_toast = (TextView)view.findViewById(R.id.tv_toast);//设置自定义view中显示的文字tv_toast.setText(text);Toast toast = new Toast(context);//设置显示时长toast.setDuration(duration);//加载需要显示的自定义viewtoast.setView(view);//设置toast显示的位置toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//显示toasttoast.show();break;default:break;}}}
其中图文混排效果的布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:background="#C0C0C0" android:orientation="horizontal" > <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_toast" android:layout_height="wrap_content" android:layout_width="200dip" android:layout_marginLeft="20dip" android:text="考完试了,心情不错 啊!!!!!" android:textSize="18sp" /></LinearLayout>
3 最终效果如下
- Android学习之Toast
- Android学习笔记之Toast
- Android学习之Toast详解
- android学习之Toast(单击按钮显示Toast信息)
- ANdroid 学习 之 修改Toast的样式
- Android学习笔记之自定义Toast
- Android学习笔记之自定义Toast
- Android 之Toast讲解-android学习之旅(一)
- Android Toast 学习笔记
- Android的Toast介绍-android学习之旅(三十六)
- 自学Android系列 笔记2 Android 学习之toast
- Android基本之Toast
- Android之Toast
- Android之自定义Toast
- android 界面之Toast
- Android初学者之Toast
- Android之Toast
- android之【Toast】
- AWK命令使用 小结
- SZNOI d013题解
- TINYC写个8052虚拟机(1) Intel HEX文件格式转换为BIN。
- SZNOI d014题解
- windows 7下IE8当IE7使用 win8 下 ie10当ie7 ie8 ie9 使用
- Android学习之Toast
- JQuery Mobile入门——单选按钮组
- Learning ADSP-TS201系列--Flag Pins按键和LED灯
- SZNOI d015题解
- java内部类之见解
- TINYC写个8052虚拟机(2) Intel MCS-805x指令表.
- 百钱买百鸡
- 在js中for in的用法
- SZNOI d016题解