Dayone_Dialog
package com.example.day01_dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
//普通对话框
public class MainActivity01 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main01);
//1.构建者
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
//2.设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("静静再睡会");//设置标题
builder.setMessage("我觉得这个想法很好");//设置内容
//设置确定按钮
builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity01.this, "你点击了同意o~~~", Toast.LENGTH_SHORT).show();
}
});
//设置取消按钮
builder.setNegativeButton("不同意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity01.this, "你点击了不同意", Toast.LENGTH_SHORT).show();
}
});
//3.使用创造者创建对话框
AlertDialog dialog = builder.create();
//4.显示
dialog.show();
}
}
package com.example.day01_dialog;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;
//设置圆形进度条对话框
public class MainActivity_Cricle extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__cricle);
final ProgressDialog dialog=new ProgressDialog(this);
dialog.setProgress(100);
dialog.setMessage("正在加载请稍后");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//开始显示
dialog.show();
//开始设置定时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if(progress==100){
dialog.dismiss();//对话框消失
timer.cancel();//定时完成了
}
dialog.setProgress(progress+=20);
}
},0,1000);
}
}
package com.example.day01_dialog;
import android.content.ClipData;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
//单选对话框
public class MainActivity_danxuan extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_danxuan);
//1.构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//2.设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("静静再睡会");//标题
//设置同意监听事件
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_danxuan.this, "你点击了ok", Toast.LENGTH_SHORT).show();
}
});
//设置取消监听事件
builder.setNegativeButton("cacel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_danxuan.this, "你点击了取消o~~", Toast.LENGTH_SHORT).show();
}
});
//开始设置单选列表
final String[] items={"维凯哥哥","静静","启航","奇骏"};
//参数一 列表;参数二 默认选中下标; 参数三 事件监听
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { //which是选中的哪一个下标的意思
Toast.makeText(MainActivity_danxuan.this, "你选中的靓仔是"+items[which], Toast.LENGTH_SHORT).show();
}
});
//3.使用建造者创建对话框
AlertDialog dialog = builder.create();
// 4.显示
dialog.show();
}
}
package com.example.day01_dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
//多选框
public class MainActivity_duoxuan extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_duoxuan);
final String[] item={"静静","小红","小黑","小花"};
final boolean[] flag={true,false,true,false};
//1.构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("静静在睡会");//设置标题
//设置同意
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i <flag.length ; i++) {
if(flag[i]){
Toast.makeText(MainActivity_duoxuan.this, "是"+item[i], Toast.LENGTH_SHORT).show();
}
}
}
});
//设置取消不同意
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_duoxuan.this, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
//设置多选列表
builder.setMultiChoiceItems(item, flag, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flag[which]=isChecked;
}
});
//使用建造者创建对话框
AlertDialog dialog = builder.create();
dialog.show();
}
}
package com.example.day01_dialog;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;
//水平进度条对话框
public class MainActivity_progress extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_progress);
//1.实例化对象
final ProgressDialog dialog=new ProgressDialog(this);
//2.设置属性
// 设置进度为一百
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(100);
//设置总进度为100
dialog.setMax(100);
dialog.setMessage("正在下载");
//3.显示
dialog.show();
//4.模拟定时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if(progress==100){
dialog.dismiss();//消失
timer.cancel();
}
dialog.setProgress(progress+=20);
}
},0,1000);//0秒后执行,每隔1000毫秒执行一次
}
}
package com.example.day01_dialog;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
//日期选择对话框
public class MainActivity_Time extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__time);
Calendar calendar=Calendar.getInstance();//创建日历对象
//设置点击事件
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
/*
@param context 上下文
* @param listener 监听器
* @param year 默认的年
* @param month 默认的月
* @param dayOfMonth 默认的日
*/
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity_Time.this, ""+year+"——"+(month+1)+"——"+dayOfMonth,Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
package com.example.day01_dialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
//时间选择对话框
public class MainActivity_timeSelect extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_time_select);
//创建日历对象
Calendar instance = Calendar.getInstance();
//1.实例化对象 2.shoow
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity_timeSelect.this, ""+hourOfDay+"_"+minute, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),true).show();
}
}
package com.example.day01_dialog;
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.ImageView;
public class MainActivity_zidingyi extends AppCompatActivity {
private ImageView iv;
//自定义
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_zidingyi);
iv = (ImageView) findViewById(R.id.iv);
//0.获取视图
View view= LayoutInflater.from(this).inflate(R.layout.activity_main_zidingyi,null);
//设置自定义布局
ImageView imageView=view.findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity_zidingyi.this, "sdrtyu", Toast.LENGTH_SHORT).show();
}
});
//构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setView(view);
//使用创建者创建对话框
AlertDialog dialog = builder.create();
dialog.show();
}
}
来源:CSDN
作者:jingingbuzai
链接:https://blog.csdn.net/jingingbuzai/article/details/103722630