【Android(安卓)开发教程】Toast通知
16lz
2021-12-04
本章节翻译自《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在模拟器上面调试。点击中心的按钮,就会弹出吐司通知了。
更多相关文章
- Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
- Android(安卓)中文 API (35) —— ImageSwitcher
- Android(安卓)通知Notification的两种实现方法
- Android点击通知栏,不启动新的activity
- android 通知 Notification
- Android自定义Toast 解决关闭通知 Toast无法弹出
- Android(安卓)APP 版本更新通知代码
- 【Android外文翻译 - 02】判断是否可以使用某个Intent
- android之PendingIntent使用