常见的对话框、通知栏Notification

/**
 * 
 * 进度条和对话框 组件 都是可以在 子线程修改 ui组件的
 *
 */
public class MainActivity extends Activity {

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

    public void click1(View view){
        //对话框的创建器
        AlertDialog.Builder builder = new Builder(this);

        builder.setTitle("我是对话框");
        builder.setMessage("对话框显示的内容");
        // builder.setIcon(R.drawable.ic_launcher);//图标

        builder.setPositiveButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "确定被点击了", 0).show();
            }
        });

        builder.setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //什么都不写默认实现就是关闭掉对话框
            }
        });
        builder.setCancelable(false);
        builder.create().show();
    }

    /**
     * 单选对话框
     * @param view
     */
    public void click2(View view){
        //对话框的创建器
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("请选择您的性别");
        final String[] items = {"男","女","未知"};

        builder.setSingleChoiceItems(items, 2, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "您的性别:"+items[which], 0).show();
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 多选对话框
     * @param view
     */
    public void click3(View view){
        //对话框的创建器
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("请选择你最爱吃的水果");
        final String[] items={"苹果","梨","菠萝","香蕉","黄瓜"};
        final boolean[] result =new boolean[]{true,false,true,false,false};

        builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(getApplicationContext(), items[which]+isChecked, 0).show();
                result[which] = isChecked;
            }
        });
        builder.setPositiveButton("提交", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuffer sb = new StringBuffer();
                for(int i=0;i<result.length;i++){
                    if(result[i]){
                        sb.append(items[i]+",");
                    }
                }
                Toast.makeText(getApplicationContext(), "您选中了,"+sb.toString(), 0).show();
            }
        });
        //builder.create().show();

        builder.show();
    }

    //进度条对话框,不显示进度的
    public void click4(View view){
        ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("提醒");
        pd.setMessage("正在加载数据...请稍等。");
        pd.show();
    }
    //带进度的进度条对话框
    public void click5(View view){
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("提醒");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//进度条的样式
        pd.setMax(100);
        pd.setMessage("正在加载数据...请稍等。");
        pd.show();
        new Thread(){
            public void run() {
                for(int i = 0;i<100;i++){
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pd.setProgress(i);
                }
                pd.dismiss();//关闭对话框
            };
        }.start();
    }
}

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View view){
        //获取到手机系统里面的 通知管理器
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //实例化notification 表示通知的具体内容
        Notification notification = new Notification(
                R.drawable.notification, //图标
                "我是一个通知",//标题 
                System.currentTimeMillis());//时间

        //点击之后,自动消失
        notification.flags = Notification.FLAG_AUTO_CANCEL;


        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:110"));

        // PendingIntent 激活延期的意图 ,当点击通知栏信息时,打开相应的 activity 界面
        //Parcelable :两个进程间 通信时所用到的类
        //intent :打开的真实意图
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

        //显示的指定参数
        notification.setLatestEventInfo(this, "我是标题", "我是内容", contentIntent);

        nm.notify(0, notification);//发出通知
    }

    /**
     * 新版本的notification
     * @param view
     */
    @SuppressLint("NewApi")
    public void click2(View view){
         Notification noti = new Notification.Builder(this)
         .setContentTitle("我是标题")
         .setContentText("我是内容")
         .setSmallIcon(R.drawable.notification)//小图标,在右边显示

         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))//大图标,在左边显示
         .build();

         //获取到手机系统里面的 通知管理器
         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         nm.notify(0, noti);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值