進階篇-用戶界面:12.android中的 Toast(吐司) 和 Notification(通知)

1.Toastandroid

android中的Toast是一個很是友好方便的用戶界面交互形式,經過彈出一個吐司來警告、提示用戶。app

Toast的系統默認形式很是簡單ide

Toast.makeText(MainActivity.this, "This is a normal toast!",Toast.LENGTH_SHORT);
              

效果就是從屏幕的下方彈出一個吐司。提示 this is a normal toast!學習

固然咱們也能夠自定義toast的彈出位置甚至彈出的內容不只只能夠是文字,其餘的控件也能夠,自定義代碼以下:ui

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LinearLayout ll = new LinearLayout(MainActivity.this);

                TextView tv = new TextView(MainActivity.this);
                tv.setText("this is a textview");
                ImageView imv = new ImageView(MainActivity.this);
                imv.setImageResource(R.mipmap.ic_launcher);
                ll.addView(tv);
                ll.addView(imv);

                Toast t = Toast.makeText(MainActivity.this, "This is a normal toast!", Toast.LENGTH_SHORT);
                t.setGravity(Gravity.CENTER,0,0);   //set the position of the toast
                t.setView(ll);
                t.show();
            }
        });
    }
}

效果是彈出的toast內容包括一段文字和一張圖片。this

2.Notification(通知)spa

咱們平時用的應用天天不停的給咱們發通知,這個通知就是Notification。code

Notification的使用方法。orm

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.Gravity;
import android.view.View;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public static final int NOTIFIVATION_ID = 1200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0,  new Intent(MainActivity.this,MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NotificationCompat.Builder builer = new NotificationCompat.Builder(MainActivity.this);
                builer.setContentIntent(pi);  //set notifiction onClick method
                builer.setSmallIcon(R.mipmap.ic_launcher);
                builer.setContentTitle("You have a new message!");
                builer.setContentText("This is a message!You've made an notification!");
                Notification notification = builer.build();
                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                manager.notify(NOTIFIVATION_ID,notification);
            }
        });

    }
}

此爲安卓用戶界面大體知識的完結……接下來的時間學習簡單的安卓系統功能!blog

相關文章
相關標籤/搜索