1. Toast
学习创建长短不一的Toast提示,并自定义Toast在屏幕上的位置以及Toast的外观。

1 package com.example.toastdemo; 2 3 import android.app.Activity; 4 import android.app.ActionBar; 5 import android.app.Fragment; 6 import android.os.Bundle; 7 import android.view.Gravity; 8 import android.view.LayoutInflater; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.widget.Button; 14 import android.widget.ImageView; 15 import android.widget.Toast; 16 import android.os.Build; 17 18 public class MainActivity extends Activity { 19 20 private Button btnShowToastShort, btnShowToastLong, btnShowToastImage; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 btnShowToastShort = (Button) findViewById(R.id.btnShowToast); 28 btnShowToastLong = (Button) findViewById(R.id.btnShowToastLong); 29 btnShowToastImage = (Button) findViewById(R.id.btnShowToastImage); 30 31 btnShowToastShort.setOnClickListener(new View.OnClickListener() { 32 33 @Override 34 public void onClick(View v) { 35 // 可以改变Toast显示的位置; 36 37 Toast shortToast = Toast.makeText(MainActivity.this, 38 "显示一个简短的Toast", Toast.LENGTH_SHORT); 39 shortToast.setGravity(Gravity.CENTER, 0, 0); 40 shortToast.show(); 41 } 42 }); 43 44 btnShowToastLong.setOnClickListener(new View.OnClickListener() { 45 46 @Override 47 public void onClick(View v) { 48 Toast.makeText(MainActivity.this, "显示一个较长的Toast", 49 Toast.LENGTH_LONG).show(); 50 } 51 }); 52 53 btnShowToastImage.setOnClickListener(new View.OnClickListener() { 54 55 // 显示图片之后,就不会显示文字。 56 // 若想显示其它,需要定义Layout 57 @Override 58 public void onClick(View v) { 59 Toast imageToast = Toast.makeText(MainActivity.this, 60 "显示一个图片的Toast", Toast.LENGTH_LONG); 61 ImageView imageView = new ImageView(MainActivity.this); 62 imageView.setImageResource(R.drawable.ic_launcher); 63 imageToast.setView(imageView); 64 imageToast.show(); 65 } 66 }); 67 68 } 69 70 }

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="com.example.toastdemo.MainActivity" 8 tools:ignore="MergeRootFrame" > 9 10 <Button 11 android:id="@+id/btnShowToast" 12 android:layout_width="fill_parent" 13 android:layout_height="wrap_content" 14 android:text="显示一个简短的Toast" /> 15 16 <Button 17 android:id="@+id/btnShowToastLong" 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:text="显示一个较长的Toast" /> 21 22 <Button 23 android:id="@+id/btnShowToastImage" 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:text="显示一个图片的Toast" /> 27 28 </LinearLayout>

1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.toastdemo" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="19" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.example.toastdemo.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27 </manifest>
2. Notification
学习创建Notification对象,为其指定标题、内容和图标,以及Notification的更新方法。

1 package com.example.notificationdemo; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.content.Context; 7 import android.os.Bundle; 8 import android.support.v4.app.NotificationCompat; 9 import android.support.v4.app.NotificationCompat.Builder; 10 import android.view.View; 11 import android.widget.Button; 12 13 public class MainActivity extends Activity { 14 15 public static final int NOTIFICATION_ID = 1234; 16 private Button btn1; 17 private int conter = 0; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 btn1 = (Button) findViewById(R.id.but1); 25 btn1.setOnClickListener(new View.OnClickListener() { 26 27 @Override 28 public void onClick(View v) { 29 conter++; 30 Builder builder = new NotificationCompat.Builder( 31 MainActivity.this); 32 builder.setSmallIcon(R.drawable.ic_launcher); 33 builder.setContentTitle("你已经创建" + conter + "个新消息了!"); 34 builder.setContentText("Notification~~~"); 35 36 Notification notification = builder.build(); 37 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 38 manager.notify(NOTIFICATION_ID, notification); 39 40 } 41 }); 42 43 } 44 45 }

1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.notificationdemo.MainActivity" 7 tools:ignore="MergeRootFrame" > 8 9 <Button 10 android:id="@+id/but1" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:text="创建一个提示" /> 14 15 </FrameLayout>

1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.notificationdemo" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="19" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.example.notificationdemo.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27 </manifest>