Android对话框,进度条,,时间,日期对话框,自定义对话框


普通对话框:


AlertDialog继承自Dialog(所有对话框的父类)
基本对话框都是继承自Dialog

方法1:
this代表当前对象,如果dialog在匿名内部类中,要写(类名.this)才可以,否则报错,例如,
AlertDialog.Builder(MainActivity.this)

package com.example.syx.bb;

import android.app.AlertDialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class bb extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bb);
        //设置普通对话框
        //this代表当前对象,如果dialog在匿名内部类中,要写(类名.this)才可以,否则报错,例如,
        //AlertDialog.Builder(MainActivity.this) 
        new AlertDialog.Builder(this)
        //设置提示文字
                .setMessage("您是否选择退出")
                //设置对话框标题
                .setTitle("dialog对话框")
                //设置对话框图标
                .setIcon(R.mipmap.ic_launcher)
  //设置对话框添加确定按钮,第一个参数设置按钮提示信息,第二个设置是否监听,没有监听,就是null
                .setPositiveButton("确定",null)
   //设置对话框添加取消按钮,第一个参数设置按钮提示信息,第二个设置是否监听,没有监听,就是null
                .setNegativeButton("取消",null)
                //        设置是否可以通过点击对话框外区域或返回按键关闭对话框
        builder.setCancelable(false);
                //创建对话框
                .create()
                //显示对话框
                .show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_bb, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

方法二:

package com.example.syx.bb;

import android.app.AlertDialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class bb extends AppCompatActivity{
    //声明对象
    private AlertDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bb);
        //设置普通对话框
        dialog=new AlertDialog.Builder(this)
                .setMessage("您是否选择退出")
                .setTitle("dialog对话框")
                .setIcon(R.mipmap.ic_launcher)
                .setPositiveButton("确定",null)
                .setNegativeButton("取消",null)
                .create();
               dialog .show();
  //**********************************************************************
//以上这种写法
//.setMessage("您是否选择退出")
 // .setTitle("dialog对话框")
// .setIcon(R.mipmap.ic_launcher)
  //等等。。。。
  //上面的属性返回值都为创建的dialog对象
 //**********************************************************************
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_bb, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

**效果图:
在这里插入图片描述


单选对话框:


 //设置单选对话框:
        //生成对话框
        new AlertDialog.Builder(this)
                .setTitle("单选对话框,请选择性别")
                .setIcon(R.mipmap.ic_launcher)
                .setSingleChoiceItems(new String[]{"男", "女"}, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("确定",null)
                .show();


多选对话框,进度条,消息对话框


package com.example.syx.duihuakuangdemo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.preference.DialogPreference;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //声明对象
    //private AlertDialog dialog;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //设置普通对话框
        //声明对象,全局变量,
//        dialog=new AlertDialog.Builder(this)
//       // 标题
//                .setTitle("普通对话框")
//        //对话框logo
//                .setIcon(R.mipmap.ic_launcher)
//        //对话框提示信息
//                .setMessage("您确定退出吗")
//        //确定按钮,第一个参数,按钮提示信息,第二个参数,是否设置事件监听,
//                .setPositiveButton("确定",null)
//        //取消按钮,第一个参数,按钮提示信息,第二个参数,是否设置事件监听,
//                .setPositiveButton("取消",null)
//        //创建对话框,不声明对话框对象不用调用,
//                .create();
//      //  显示对话框
//        dialog.show();




        //设置单选对话框:
        //不声明对象不用调用创建对话框方法  .creat();
        //也不用.setMassage(),否则被选择的信息不显示
//       new AlertDialog.Builder(this)
//               .setTitle("您的性别为")
//               .setIcon(R.mipmap.ic_launcher)
//      //  第一个参数建立数组,供选择的选项,第二个参数设置默认选择,如果设置默认不选择,把第二个参数设置为-1,第三个参数,设置点击事件
//               .setSingleChoiceItems(new String[]{"男", "女"}, 0, new DialogInterface.OnClickListener() {
//                   @Override
//                   public void onClick(DialogInterface dialog, int which) {
//
//                   }
//               })
//               .setPositiveButton("确定",null)
//               .show();






        //设置多选对话框
//        new AlertDialog.Builder(this)
//                .setTitle("您的喜好")
//                .setIcon(R.mipmap.ic_launcher)
//                //设置多选,第一个参数建立数组存放多选的选项,第二个参数,设置默认选项,默认都不选可以设置为null,第三个参数是设置监听。
//               //如果都设置为默认选择,就把第二个参数设置为,new boolean[]{true,true,true,true},其中数组里的值一定要和设置的选项相等或多,
//                    //例如下面就是默认选择前两个默认选择,其他默认不选
//                .setMultiChoiceItems(new String[]{"唱歌", "跳舞", "绘画", "书法", "游泳"}, new boolean[]{true,true,false,false,false},null)
//                .setPositiveButton("确定",null)
//                .show();





        //设置进度条
        //声明变量,全局变量,不不声明直接创建,可能会报错
//        progressDialog=new ProgressDialog(this);
//        progressDialog .setTitle("进度条");
//        progressDialog .setIcon(R.mipmap.ic_launcher);
//        //设置提示信息
//        progressDialog  .setMessage("正在下载请等候");
//        //设置进度条,为水平显示,,,如果参数为,ProgressDialog.STYLE_SPINNER,则进度条为圆形进度条
//        progressDialog  .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//        progressDialog.show();


        //消息对话框
        //toast.makeText(,,,)第一个参数为当前类对象,第二个参数为提示信息,第三个参数设置提示信息时间长短Toast.LENGTH_SHORT短,最后一定要调用show()方法,要不然toast不显示
       // Toast.makeText(this, "hello,易烊千玺", Toast.LENGTH_LONG).show();





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

自定义对话框:


先创建一个xml文件存放布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
    android:background="#CDC9C9"
   >
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:background="#ffffff"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="自定义对话框"
            android:id="@+id/dialog_zdy"
            android:layout_marginBottom="20dp"
            android:background="#0080ff"
            android:textSize="25sp"
            android:gravity="center"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/msg"
                android:layout_marginBottom="25dp"
                android:layout_marginTop="25dp"
                android:gravity="center"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:layout_gravity="bottom"
            android:gravity="center"
           >
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bt_ok"
                android:layout_marginLeft="20dp"
                android:background="#ff8000"
                android:textSize="25sp"
                android:text="确定"
                />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bt_cancel"
                android:textSize="25sp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="#d0d0d0"
                android:text="取消"
                />

        </LinearLayout>
    </LinearLayout>

</FrameLayout>

创建一个mydialog给事件给xml文件里的控件建立事件监听:

package com.example.syx.bb;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.syx.bb.R;

public class mydialog extends Dialog {
    private TextView msg;
    private Button bt_ok;
    private Button bt_cancel;
    private String dialogname;

    public mydialog(Context context,String dialogname) {
        super(context);
        this.dialogname=dialogname;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去除边框
        //一定要引入自定义的my_dialog.xml文件,否则报错
        setContentView(R.layout.my_dialog);
        msg=(TextView)findViewById(R.id.msg);
        bt_ok=(Button)findViewById(R.id.bt_ok);
        bt_cancel=(Button)findViewById(R.id.bt_cancel);
        msg.setText(dialogname);
        bt_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }
}

最后引用mydialog:

package com.example.syx.duihuakuangdemo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.preference.DialogPreference;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


      
//实例化创建的mydialog类,并显示。
        mydialog my=new mydialog(this,"我是自定义dialog");
        my.show();





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


时间对话框:


xml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bt_time"
    android:text="点击选择时间"
    />

java

 bt_time =findViewById(R.id.bt_time);

        bt_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前日期
                //单例模式,设计模式的一种  静态方法
                Calendar c = Calendar.getInstance();
                TimePickerDialog tpd=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Toast.makeText(MainActivity.this,hourOfDay+":"+minute,Toast.LENGTH_SHORT).show();
                    }
                },c.get(Calendar.HOUR),c.get(Calendar.MINUTE),true);
                //点击其他部分不消失
                tpd.setCancelable(false);
                tpd.show();
            }
        });
    }

日期对话框:


xml:

  <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击选择日期"
        android:id="@+id/bt_date"
       />

java


bt_date =findViewById(R.id.bt_date);
 bt_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前日期
                //单例模式,设计模式的一种  静态方法
                Calendar c=Calendar.getInstance();
                DatePickerDialog dpd=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));
//                点击其他部分不消失
                dpd.setCancelable(false);
                dpd.show();
            }
        });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值