从零开始学android<Toast土司提示组件.二十.>
2014-08-15 10:40
519 查看
在系统之中,通过对话框可以对用户的某些操作进行提示,但是在Android平台之中也提供了另外一套更加友好的提示界面效果,而且这种界面在提示用户的时候不会打断用户的正常操作,这种对话框可以通过Toast组件实现。
Toast是一个以简单提示信息为主要显示操作的组件,在Android之中android.widget.Toast的继承结构如下所示:
java.lang.Object
↳ android.widget.Toast
常用的方法:
XMl文件配置
JAVA文件配置
原始提示效果:
自定义toast显示
Toast组件较为简单主要是为了对用户的操作进行友好的提示,在开发当中较常用到,希望大家务必掌握
下节预报:ListView数据列表显示组件
Toast是一个以简单提示信息为主要显示操作的组件,在Android之中android.widget.Toast的继承结构如下所示:
java.lang.Object
↳ android.widget.Toast
常用的方法:
No. | 方法及常量 | 类型 | 描述 |
1 | public static final int LENGTH_LONG | 常量 | 显示时间长 |
2 | public static final int LENGTH_SHORT | 常量 | 显示时间短 |
3 | public Toast(Context context) | 普通 | 创建Toast对象 |
4 | public static Toast makeText(Context context, int resId, int duration) | 普通 | 创建一个Toast对象,并指定显示文本资源的ID,和信息的显示时间 |
5 | public static Toast makeText(Context context, CharSequence text, int duration) | 普通 | 创建一个Toast对象,并指定显示文本资源,和信息的显示时间 |
6 | public void show() | 普通 | 显示信息 |
7 | public void setDuration(int duration) | 普通 | 设置显示的时间 |
8 | public void setView(View view) | 普通 | 设置显示的View组件 |
9 | public void setText(int resId) | 普通 | 设置显示的文字资源ID |
10 | public void setText(CharSequence s) | 普通 | 直接设置要显示的文字 |
11 | public void setGravity(int gravity, int xOffset, int yOffset) | 普通 | 设置组件的对齐方式 |
12 | public View getView() | 普通 | 取得内部包含的View组件 |
13 | public int getXOffset() | 普通 | 返回组件的X坐标位置 |
14 | public int getYOffset() | 普通 | 返回组件的Y坐标位置 |
15 | public void cancel() | 普通 | 取消显示 |
XMl文件配置
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#DAAA" android:orientation="horizontal" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" android:src="@drawable/kill" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="自定义的提示" /> </LinearLayout></span>
JAVA文件配置
<span style="font-size:14px;">package com.example.toast; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button button1, button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // 原始的提示 Toast.makeText(MainActivity.this, "自定义的提示框", Toast.LENGTH_SHORT) .show(); } }); // 为button设置单击事件 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub View view = LayoutInflater.from(MainActivity.this).inflate( R.layout.linearlayout, null);// 将Layout布局转换为View对象 Toast toast = new Toast(MainActivity.this);// 创建Toast提示 toast.setGravity(Gravity.CENTER, 0, 0);// 设置Toast提示的位置 toast.setView(view);// 将布局增加到Toast组件当中 toast.show();// 显示提示 } }); } } </span>
原始提示效果:
自定义toast显示
Toast组件较为简单主要是为了对用户的操作进行友好的提示,在开发当中较常用到,希望大家务必掌握
下节预报:ListView数据列表显示组件
相关文章推荐
- 从零开始学android<AnalogClock与DigitalClock时钟组件.三十一.>
- 从零开始学android<SeekBar滑动组件.二十二.>
- 从零开始学android<Menu菜单组件.三十.>
- 从零开始学android<RatingBar评分组件.二十三.>
- 从零开始学android<ListView数据列表显示组件.二十一.>
- 从零开始学android<Mediaplayer播放器组件(播放音频).四十八.>
- 从零开始学android<Bitmap图形组件.四十七.>
- 从零开始学android<EditText组件的使用.六.>
- 从零开始学android<gallery拖拉组件.二十七.>
- 从零开始学android<chronometer计时器组件.三十二.>
- 从零开始学android<Button组件的使用.五.>
- 从零开始学android<TabHost标签组件.二十九.>
- 从零开始学android<ImageSwitcher图片切换组件.二十六.>
- 从零开始学android<Spinner下拉列表框的使用.九.>
- 从零开始学android<ScrollView滚动视图.十八.>
- Android UI设计之<五>自定义DrawView组件,实现数字签名效果
- 从零开始学android<AutoCompleteTextView随笔提示文本框.十九.>
- 从零开始学android<Tablelayout表格布局.十五.>
- 从零开始学android<FrameLayout帧布局.十四.>
- 从零开始学android<CheckBox多选按钮的使用.八.>