代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button4 = (Button) findViewById(R.id.button4);
}
public void click(View view) {
switch (view.getId()){
case R.id.button1:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.pdx);
builder.setTitle("普通对话框");
builder.setMessage("这是内容");
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
break;
case R.id.button2:
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setIcon(R.mipmap.pdx);
builder1.setTitle("这是单选框");
final String[] str = new String[]{"上","下","左,","右"};
builder1.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了:"+str[which], Toast.LENGTH_SHORT).show();
}
});
builder1.show();
break;
case R.id.button3:
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setIcon(R.mipmap.pdx);
builder2.setTitle("这是多选框");
final String[] strings = new String[]{"上","下","左,","右"};
final boolean[] booleans = new boolean[]{true,false,false,false};
builder2.setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, "你选择了:"+strings[which], Toast.LENGTH_SHORT).show();
}
});
builder2.show();
break;
case R.id.button4:
AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
builder3.setTitle("这是个图片");
View inflate = LayoutInflater.from(this).inflate(R.layout.activity_dialog, null);
ImageView imageView = inflate.findViewById(R.id.imageView);
builder3.setView(inflate);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "你点击了图片", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder3.create();
alertDialog.show();
break;
case R.id.button5:
Calendar instance1 = Calendar.getInstance();
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();
}
},instance1.get(Calendar.HOUR),instance1.get(Calendar.MINUTE),true).show();
break;
case R.id.button6:
Calendar instance = 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+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();
break;
case R.id.button7:
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setMessage("加载中");
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
private int i=0;
@Override
public void run() {
if(i>=100){
progressDialog.dismiss();
timer.cancel();
}
progressDialog.setProgress(i+=10);
}
},0,1000);
break;
case R.id.button8:
final ProgressDialog progressDialog1 = new ProgressDialog(this);
progressDialog1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog1.setMax(100);
progressDialog1.setMessage("加载中");
progressDialog1.show();
final Timer timer1 = new Timer();
timer1.schedule(new TimerTask() {
private int s=0;
@Override
public void run() {
if(s>=100){
progressDialog1.dismiss();
timer1.cancel();
}
progressDialog1.setProgress(s+=10);
}
},0,1000);
break;
}
}