DAY one 2019年8月30日
一》 Dialog对话框
常用的对话框
普通 | |||
---|---|---|---|
普通 | AlertDialog.Builder() | ||
选择 | 单选 | 多选 | AlertDialog.Builder() |
Context | 时间 | 日期 | Date(Time)PickerDialog |
进度条 | 水平 | 圆圈 | ProgressDialog |
自定义框 | |||
自定义 | AlertDialog.Builder() | setView() |
框架-普通
//TODO 普通对话框
public void normal_dialog(){
//TODO 1:构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//TODO 2:设置属性
更改区
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle(“雷猴啊”);//设置标题
builder.setMessage(“你是猪”);//设置内容
//设置确定按钮
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
//设置取消按钮
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了no",Toast.LENGTH_SHORT).show();
}
});
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
}
选择
单选
final String[] items={"男人","女人","不想"};
//TODO 设置单选列表
//TODO 参数一 列表 参数二 默认选中下表 参数三 事件监听
builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选中烦恼的"+items[which], Toast.LENGTH_SHORT).show();
}
});
多选
final String[] items={"佩奇","苏溪","乔治","亨利"};
final boolean[] flags={true,true,true,false};
//TODO 1:构建着
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//TODO 2:画画
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<flags.length;i++){
if(flags[i]){
Toast.makeText(MainActivity.this,"是"+items[i],Toast.LENGTH_SHORT).show();
}
}
}
});
自定义框
先写个布局
//TODO 1:构建着
AlertDialog.Builder builder=new AlertDialog.Builder(this);
View view=LayoutInflater.from(this).inflate(R.layout.布局名字,null);
//设置自定义布局
builder.setView(view);
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
时间/日期
Calendar calendar=Calendar.getInstance();//日历对象
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();//吐个司
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
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();
}
},calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
进度条
//TODO 1:实例化对象
final ProgressDialog dialog=new ProgressDialog(this);
//TODO 2:设置属性
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置水平样式
//dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置圆圈
dialog.setMax(100);//大小
dialog.setMessage("正在下载");
//TODO 3:显示
dialog.show();
定时器
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
执行的内容
},0,1000);
//0秒后执行,每隔1000毫秒执行一次
自定义
写个布局
MainActivity
public class MainActivity extends AppCompatActivity {
private Button yes;
private Button no;
private String titleStr;//从外界设置的title文本
private String messageStr;//从外界设置的消息文本
private TextView titleTV;//消息标题文本
private TextView message;//消息提示文本
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void s(View view) {
MyDialog myDialog = new MyDialog(this);
myDialog.setTitleStr("贪玩蓝月");
myDialog.setContentStr("是兄弟就来砍我");
myDialog.setCancelStr("别了");
myDialog.setCourseStr("好的");
myDialog.setCancelOnClickListener(new MyDialog.yesOnClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "确认被点击", Toast.LENGTH_SHORT).show();
}
});
myDialog.setCourseOnClickListener(new MyDialog.noOnClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "取消被点击", Toast.LENGTH_SHORT).show();
}
});
myDialog.show();
}
}
自定义类继承dialog
public class MyDialog extends Dialog {
private TextView title;
private TextView content;
private Button cancel;
private Button course;
private String titleStr;
private String contentStr;
private String cancelStr;
private String courseStr;
private yesOnClickListener yes;
private noOnClickListener no;
public MyDialog(@NonNull Context context) {
super(context);
}
public void setTitleStr(String titleStr) {
this.titleStr = titleStr;
}
public void setContentStr(String contentStr) {
this.contentStr = contentStr;
}
public void setCancelStr(String cancelStr) {
this.cancelStr = cancelStr;
}
public void setCourseStr(String courseStr) {
this.courseStr = courseStr;
}
public void setCancelOnClickListener(MyDialog.yesOnClickListener yesOnClickListener) {
this.yes = yesOnClickListener;
}
public void setCourseOnClickListener(MyDialog.noOnClickListener noOnClickListener) {
this.no = noOnClickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ovo);
title = findViewById(R.id.title);
content = findViewById(R.id.content);
cancel = findViewById(R.id.yes);
course = findViewById(R.id.no);
title.setText(titleStr);
content.setText(contentStr);
cancel.setText(cancelStr);
course.setText(courseStr);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yes.onClick();
}
});
course.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
no.onClick();
dismiss();
}
});
}
public interface yesOnClickListener {
public void onClick();
}
public interface noOnClickListener {
public void onClick();
}
}