package com.example.administrator.imooc;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button setDate,single,muti;
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
init();
}
public void init(){
setDate=(Button) findViewById(R.id.btn_setting);
single=(Button) findViewById(R.id.btn_single);
muti=(Button) findViewById(R.id.btn_muti);
setDate.setOnClickListener(
this);
single.setOnClickListener(
this);
muti.setOnClickListener(
this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_setting:
showDatePickerDialog();
break;
case R.id.btn_single:
showSingleListDialog();
break;
case R.id.btn_muti:
showMutiListDialog();
break;
}
}
public void showDatePickerDialog(){
Calendar c=Calendar.getInstance();
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)+
"月"+(dayOfMonth)+
"日",Toast.LENGTH_SHORT).show();
}
},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
}
public void showSingleListDialog(){
AlertDialog.Builder builder=
new AlertDialog.Builder(
this);
builder.setTitle(
"性别选择");
builder.setIcon(android.R.drawable.btn_star);
final String[] sex=getResources().getStringArray(R.array.sex);
builder.setSingleChoiceItems(R.array.sex, -
1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(MainActivity.
this,
"您的性别是:"+sex[which],Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.create().show();
}
public void showMutiListDialog(){
AlertDialog.Builder builder=
new AlertDialog.Builder(
this);
builder.setTitle(
"选择你的爱好");
builder.setIcon(android.R.drawable.btn_star);
final String[] loves=getResources().getStringArray(R.array.loves);
final boolean[] checkedItems=
new boolean[loves.length];
builder.setMultiChoiceItems(R.array.loves, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which,
boolean isChecked) {
checkedItems[which]=isChecked;
}
});
builder.setPositiveButton(
"确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
StringBuilder sb=
new StringBuilder();
for(
int i=
0;i<checkedItems.length;i++){
if(checkedItems[i]){
sb.append(loves[i]);
sb.append(
"\n");
}
}
Toast.makeText(MainActivity.
this,
"您的爱好是:"+sb.toString(),Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
}
转载请注明原文地址: https://ju.6miu.com/read-22607.html