专1.12.8.弹出框
##普通弹出框
public void btn2(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
builder.setMessage("内容");
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();
}
##时间弹出框
public void btn1(View view) {
Calendar instance = Calendar.getInstance();
TimePickerDialog timePickerDialog= new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),false);
timePickerDialog.show();
}
##日期弹出框
public void btn(View view) {
//获取手机当前时间
Calendar instance = Calendar.getInstance();
DatePickerDialog datePickerDialog= new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"年"+(month+1)+"月"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MARCH),instance.get(Calendar.HOUR_OF_DAY));
datePickerDialog.show();
}
##多选弹出框
public void btn3(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
final String[] s=new String[]{"吃饭","睡觉","学习"};
boolean[] b=new boolean[]{false,false,true};
builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
##倒计时进度条弹出框
public void btn3(View view) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("标题");
progressDialog.setMax(100);
progressDialog.setProgress(10);
progressDialog.setSecondaryProgress(20);
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
final Timer timer = new Timer();
progressDialog.show();
timer.schedule(new TimerTask() {
int i = 0;
@Override
public void run() {
i += 10;
progressDialog.setProgress(i);
if (i>=100){
progressDialog.dismiss();
timer.cancel();
}
}
}, 0, 1000);
progressDialog.show();}
##单选弹出框
public void btn1(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
final String[] s=new String[]{"吃饭","睡觉","学习"};
builder.setSingleChoiceItems(s, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
###自定义弹出改标题内容
public MainActivity2(@NonNull Context context) {
super(context);
}
private String msg;
private String til;
public void setMsg(String msg) {
this.msg = msg;
}
public void setTil(String til) {
this.til = til;
}
private void initView() {
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
title.setText(til);
message.setText(msg);
}
public void btn6(View view) {
MainActivity2 mainActivity2 = new MainActivity2(this);
mainActivity2.setMsg("lyqlyqlyq");
mainActivity2.setTil("xlwxlwxlw");
##自定义弹出框取消确定点击事件
//定义接口
public interface NoOnClick{
void click();
}
private NoOnClick noOnClick;
public void setNoOnClick(NoOnClick noOnClick) {
this.noOnClick = noOnClick;
}
public interface YesOnclick{
void click();
}
private YesOnclick yesOnclick;
public void setYesOnclick(YesOnclick yesOnclick) {
this.yesOnclick = yesOnclick;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "onClick: ");
dismiss();
noOnClick.click();
}
});
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "onClick:");
dismiss();
yesOnclick.click();
}
});
}
public void btn6(View view) {
MainActivity2 mainActivity2 = new MainActivity2(this);
mainActivity2.setMsg("lyqlyqlyq");
mainActivity2.setTil("xlwxlwxlw");
mainActivity2.setNoOnClick(new MainActivity2.NoOnClick() {
@Override
public void click() {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
mainActivity2.setYesOnclick(new MainActivity2.YesOnclick() {
@Override
public void click() {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
mainActivity2.show();
}