Dialog的几种方式

来源:互联网 发布:mac微信文件保存位置 编辑:程序博客网 时间:2024/05/09 08:48
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class NotifyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify);
    }
    
    int count ;
    /**
     * android对用户通知方式
     *     1、Toast
     */
    /**
     * @param view
     */
    public void onMyClick(View view){
        int id = view.getId() ;
        switch(id){
        case R.id.btn_toast:
            //显示多个
//            showToast();
            //显示一个
            showToastOnly() ;
            break ;
        case R.id.btn_dialog:
            //警告对话框
            showAlertDialog();
            //进度条对话框
//            showProgressDialog();
            break ;
        case R.id.btn_notification:
            //状态栏通知
            showNotificationWithAPI3() ;
//            showNotificationWithAPI16();
            break ;
        }
    }

    private void showNotificationWithAPI3() {
        //1.创建Notification对象
        Notification notification = new Notification(R.drawable.ic_launcher, "提示", System.currentTimeMillis()) ;
        //2.设置数据
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotifyActivity.class), 0) ;
        notification.setLatestEventInfo(this, "标题", "内容", pIntent) ;
        //3.显示
        //3.1获取系统的状态栏通知服务
        NotificationManager notificatioManager
            = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE) ;
        //3.2显示我们的Notification
        notificatioManager.notify(1000, notification) ;
    }

    @SuppressLint("NewApi")
    /*private void showNotificationWithAPI21() {
        //这里是新的创建方式:
        //1.创建Builder对象
        Notification.Builder builder = new Notification.Builder(this) ;
        //2.设置属性
        builder
        .setTicker("Ticker")
        .setContentText("内容文字")
        .setContentTitle("内容标题");
        
        //创建一个PendingIntent,持有一个Intent对象,当我们调用send方法时,这个Intent才会去启动某个系统组件。
        //要启动哪种系统组件,我们需要在创建PendingIntent时,调用对应的方法,创建PendingIntent.
        Intent intent = new Intent(this, NotifyActivity.class) ;
        *//**
         * 参数1: context
         * 参数2: 无用
         * 参数3:Intent
         * 参数4:依据PendingIntent的4中常量值,选择一种处理标志
         * *//*
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) ;
        //将pendingintent设置给notification
        builder.setContentIntent(pIntent) ;
        
        //3.创建Notification对象
        Notification notification = builder.build() ;
        //4.显示
        //4.1获取系统的状态栏通知服务
        NotificationManager notificatioManager
            = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE) ;
        //4.2显示我们的Notification
        notificatioManager.notify(1000, notification) ;
    }*/

    private void showProgressDialog() {
        //1.创建对象
        ProgressDialog dialog = new ProgressDialog(this) ;
        //2.设置属性
        dialog.setTitle("警告") ;
        dialog.setMessage("______");
        //3.显示
        dialog.show() ;
    }


    private void showAlertDialog() {
        //使用AlterDialog
        //1.创建Builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this) ;
        //2.通过Builder对象,创建AlertDialog对象
        builder
        .setTitle("标题")
        .setMessage("这里是内容")
        .setPositiveButton("确定", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("是");
            }
        })
        .setNeutralButton("不知道", null)
        .setNegativeButton("不是", null);
        //创建AlertDialog
        AlertDialog dialog = builder.create() ;
        //显示AlertDialog
        dialog.show() ;
        //将显示的AlertDialog消失
//            dialog.dismiss() ;
    }
    
    
    private void showToast() {
        //通过静态方法获取对象
        Toast toast = Toast.makeText(NotifyActivity.this, "我是土司 NO."+(count++), Toast.LENGTH_SHORT) ;
        //显示此通知
        toast.show() ; //这里系统会使用一个队列来管理将要显示的Toast,如果已经有一个Toast在显示,其他的Toast需要
                       //依次等待在队列中,然后先进先出的一个一个显示。
    }
    
    private Toast toast ;
    private void showToastOnly() {
        //通过静态方法获取对象
        if(toast==null)
            toast = Toast.makeText(NotifyActivity.this, "我是土司 NO."+(count++), Toast.LENGTH_SHORT) ;
        
        //对已经创建好的Toast进行操作
        toast.setText("我是土司 NO."+(count++)) ;
        //显示此通知
        toast.show() ; //这里系统会使用一个队列来管理将要显示的Toast,如果已经有一个Toast在显示,其他的Toast需要
                       //依次等待在队列中,然后先进先出的一个一个显示。
    }
}

0 0
原创粉丝点击