自定义对话框,登陆,时间,日期对话框

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);
    }
}
java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wang.testapp2.TestActivity5"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="一般对话框"
        android:onClick="bt1_OnClick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义对话框"
        android:onClick="bt4_OnClick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆对话框"
        android:onClick="bt5_OnClick"/>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="日期对话框"
        android:onClick="bt6_OnClick"/>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="时间对话框"
        android:onClick="bt7_OnClick"/>


</LinearLayout>
layout文件

posted @ 2016-05-13 21:54  1011042043  阅读(160)  评论(0编辑  收藏  举报