本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


Toast通知是Android中最简单的消息通知。接下来展示如何使用吐司通知。

1. 新建一个工程,Toast。

2. main.xml中的代码。

<RelativeLayout 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" >    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:padding="@dimen/padding_medium"        android:text="@string/hello_world" /></RelativeLayout>

3. MainActivity.java中的代码。

public class MainActivity extends Activity {    private Button button;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button = (Button) findViewById(R.id.button);        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(getBaseContext(), "Hi, I am a Toast Message.",                        Toast.LENGTH_SHORT).show();            }        });    }}

4. 按F11在模拟器上面调试。点击中心的按钮,就会弹出吐司通知了。



更多相关文章

  1. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
  2. Android(安卓)中文 API (35) —— ImageSwitcher
  3. Android(安卓)通知Notification的两种实现方法
  4. Android点击通知栏,不启动新的activity
  5. android 通知 Notification
  6. Android自定义Toast 解决关闭通知 Toast无法弹出
  7. Android(安卓)APP 版本更新通知代码
  8. 【Android外文翻译 - 02】判断是否可以使用某个Intent
  9. android之PendingIntent使用

随机推荐

  1. 自定义菜单
  2. Android之AIDL跨进程抛异常的原理
  3. android耳机插入\拔出事件上报流程
  4. Android(安卓)GPS ——AGPS源码分析及配
  5. RecyclerView调用smoothScrollToPosition
  6. Android不同层次开启硬件加速的方式
  7. android lcd横竖屏幕配置
  8. Android(安卓)自定义View 画圆 画线
  9. android 中滑动九宫格开机密码
  10. Android(安卓)事件总线OTTO源码解析