package com.example.wang.testapp2;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class TestActivity5 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test5);
}
//一般对话框
public void bt1_OnClick(View v)
{
AlertDialog alertDialog=new AlertDialog.Builder(this)
.setTitle("确认对话框")
.setMessage("你确实要删除么?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "执行删除,which=" + which,
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "取消删除,which=" + which,
Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "普通按钮,which=" + which,
Toast.LENGTH_SHORT).show();
}
})
.show();
}
//自定义对话框
public void bt4_OnClick(View v)
{
// //方式1-layout文件加载器
// LayoutInflater layoutInflater=getLayoutInflater();
//
// View view=layoutInflater.inflate(R.layout.dialoglayout, null);
// //方式2-View的静态方法
// View view=View.inflate(this, R.layout.dialoglayout, null);
//方式3-java代码
ImageView view=new ImageView(this);
view.setImageResource(R.drawable.asc);
new AlertDialog.Builder(this)
.setTitle("自定义对话框")
.setView(view)
.setPositiveButton("确定", null)
.show();
}
//登陆对话框
public void bt5_OnClick(View v)
{
//加载layout文件
final View view=View.inflate(this,R.layout.dialog_login,null);
new AlertDialog.Builder(this)
.setTitle("登陆对话框")
.setView(view)
.setPositiveButton("登陆", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//转换
AlertDialog al = (AlertDialog) dialog;
//获取登陆的输入信息
EditText et_1 = (EditText) al.findViewById(R.id.et_1);
Toast.makeText(TestActivity5.this, "用户名=" + et_1.getText(),
Toast.LENGTH_SHORT).show();
EditText et_2 = (EditText) view.findViewById(R.id.et_2);
//验证并提示
}
})
.setNegativeButton("取消", null)
.show();
}
//日期
public void bt6_OnClick(View v)
{
//当前日期 日历
Calendar c = Calendar.getInstance();
DatePickerDialog dp=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(TestActivity5.this,year+"-"+monthOfYear+"-"+dayOfMonth,
Toast.LENGTH_SHORT).show();
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
dp.setCancelable(false);
dp.show();
}
public void bt7_OnClick(View v)
{
Calendar c = Calendar.getInstance();
TimePickerDialog tp=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(TestActivity5.this,hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
tp.show();
tp.setCanceledOnTouchOutside(false);
}
}