1.基础的对话框AlerDialog.Builder
2.时间对话框TimePickerDialog
3.日期对话框DatePickerDilog
获取当前时间Calendar calendar=Calendar.getInstance();
4.进度条ProgressDialog
5.自定义多选框
package com.example.cishidai;import android.app.DatePickerDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.app.TimePickerDialog;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.DatePicker;import android.widget.TimePicker;import android.widget.Toast;import java.util.Calendar;import java.util.jar.Manifest;public class MainActivity extends AppCompatActivity { private Button b1; private Button b2; private Button b3; private Button b4; private Button b5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=findViewById(R.id.b1); b2=findViewById(R.id.b2); b3=findViewById(R.id.b3); b4=findViewById(R.id.b4); b5=findViewById(R.id.b5); //按钮一最基本确定取消 b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("标题"); builder.setIcon(R.mipmap.ic_launcher_round); builder.setMessage("你要沉着冷静!"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "sweet", Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "go out!", Toast.LENGTH_SHORT).show(); } });// builder.create().show(); AlertDialog dialog=builder.create(); dialog.setCanceledOnTouchOutside(true); dialog.show(); } }); //时间 b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, hourOfDay+"", Toast.LENGTH_SHORT).show(); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } Toast.makeText(MainActivity.this, minute+"", Toast.LENGTH_SHORT).show(); } },13,47,true); timePickerDialog.show(); } }); //日期 //获取当前时间 Calendar calendar=Calendar.getInstance(); final int year = calendar.get(Calendar.YEAR); final int month = calendar.get(Calendar.MONTH); final int day = calendar.get(Calendar.DAY_OF_MONTH); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DatePickerDialog datePickerDialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Toast.makeText(MainActivity.this, year+" "+(month+1)+" "+day, Toast.LENGTH_SHORT).show(); } },year,month,day); datePickerDialog.show(); } }); //进度条 b4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ProgressDialog dialog=new ProgressDialog(MainActivity.this); dialog.setTitle("进度条"); dialog.setIcon(R.mipmap.ic_launcher); dialog.setMax(100); dialog.setProgress(20);// dialog.setIndeterminate(true); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.show(); } }); //自定义多选框 b5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("自定义多选"); builder.setMessage("爱好"); View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.ziduo,null); builder.setView(view); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "很好", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "连个爱好都没有,废了", Toast.LENGTH_SHORT).show(); } }); builder.show(); } }); } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦