对话框

AlertDialog分4种,分别为普通、单选、多选、输入文本

ProgressDialog

CharacterPickerDialog

DatePicker

TimePicker

在drawable添加三张图片:

bg.png


line.png

e.jpg

在colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="transparent">#00000000</color>
</resources>

在styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.


        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!-- 边框 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsTranslucent">false</item>
        <!-- 半透明 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 无标题 -->
        <item name="android:windowBackground">@color/transparent</item>
        <!-- 背景透明 -->
        <item name="android:backgroundDimEnabled">false</item>
        <!-- 模糊 -->
    </style>

</resources>

整体布局:activity_main.xml

<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:orientation="vertical"
    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.dialog.MainActivity" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:orientation="vertical">

            <Button
                android:id="@+id/common"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通" />

            <Button
                android:id="@+id/radio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="单选" />

            <Button
                android:id="@+id/check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="多选" />

            <Button
                android:id="@+id/input"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="输入文字" />

            <Button
                android:id="@+id/progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="progressDialog" />

            <Button
                android:id="@+id/charactor_picker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="characterPickerDialog" />

            <Button
                android:id="@+id/date_picker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="datePicker" />

            <Button
                android:id="@+id/time_picker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="timePicker" />
            <Button 
                android:id="@+id/my_dialog"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="自定义对话框"/>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

input.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/input_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

mydialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/myBackgroud"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/myPicture"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_marginLeft="30dp"
                android:paddingTop="10dp"
                android:src="@drawable/e" />

            <TextView
                android:id="@+id/myQuestion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="请问你需要服务吗?" />
        </LinearLayout>

        <EditText
            android:id="@+id/my_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="30dp" >

            <requestFocus />
        </EditText>

        <ImageView
            android:id="@+id/line"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/line" />
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp">
            <Button 
	            android:id="@+id/yes"
	            android:layout_width="0dp"
	            android:layout_height="wrap_content"
	            android:layout_weight="1"
	            android:text="确认"/>
	        <Button 
	            android:id="@+id/no"
	            android:layout_width="0dp"
	            android:layout_height="wrap_content"
	            android:layout_weight="1"
	            android:text="取消"/>
        </LinearLayout>        
    </LinearLayout>
</LinearLayout>

java代码:MyCallback.java

package com.example.dialog;

public interface MyCallback {
	public void onok(String str);
}

MyDialog.java

package com.example.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyDialog extends Dialog {
	private Context context;
	private Button bt1,bt2;
	private EditText et;
	private MyCallback myCallback;
	public MyDialog(Context context,int theme) {
		super(context,theme);
		this.context=context;
	}
	public void bind(MyCallback myCallback){
		this.myCallback=myCallback;
	}
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.mydialog);
	et=(EditText) findViewById(R.id.my_et);
	bt1=(Button) findViewById(R.id.yes);
	bt2=(Button) findViewById(R.id.no);
	bt1.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Toast.makeText(context, "好的", 500).show();
			String str=et.getText().toString();
			myCallback.onok(str);
			MyDialog.this.dismiss();
		}
	});
	bt2.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			MyDialog.this.dismiss();
		}
	});
}
}

MainActivity.java

package com.example.dialog;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.text.method.CharacterPickerDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;

public class MainActivity extends Activity implements OnClickListener, OnDateSetListener, OnTimeSetListener,MyCallback {
	private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn,charactorPickerBtn,datePickerBtn,timePickerBtn,myBtn;
	private String str;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		commonBtn = (Button) findViewById(R.id.common);
		radioBtn = (Button) findViewById(R.id.radio);
		checkBtn = (Button) findViewById(R.id.check);
		inputBtn = (Button) findViewById(R.id.input);
		progressBtn = (Button) findViewById(R.id.progress);
		charactorPickerBtn=(Button) findViewById(R.id.charactor_picker);
		datePickerBtn=(Button) findViewById(R.id.date_picker);
		timePickerBtn=(Button) findViewById(R.id.time_picker);
		myBtn=(Button) findViewById(R.id.my_dialog);
		
		commonBtn.setOnClickListener(this);
		radioBtn.setOnClickListener(this);
		checkBtn.setOnClickListener(this);
		inputBtn.setOnClickListener(this);
		progressBtn.setOnClickListener(this);
		charactorPickerBtn.setOnClickListener(this);
		datePickerBtn.setOnClickListener(this);
		timePickerBtn.setOnClickListener(this);
		myBtn.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.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();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		AlertDialog.Builder builder = new Builder(this);
		switch (v.getId()) {
		case R.id.common:
			//普通
			builder.setTitle("普通对话框");
			builder.setMessage("确认删除吗?");
			builder.setIcon(R.drawable.ic_launcher);
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Log.d("lhm", "确定了");
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Log.d("lhm", "取消了");
					dialog.dismiss();
				}
			});
			builder.create().show();
			break;
			//单选
		case R.id.radio:
			builder.setTitle("单选对话框");
			builder.setIcon(R.drawable.ic_launcher);
			builder.setSingleChoiceItems(new String[] { "大学", "高中", "初中", "小学" }, 1,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
							case 0:
								str = "大学";
								break;
							case 1:
								str = "高中";
								break;
							case 2:
								str = "初中";
								break;
							case 3:
								str = "小学";
								break;
							default:
								break;
							}
							Log.d("lhm", str);
						}
					});
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Log.d("lhm", "确定了");
				}
			});
			builder.create().show();
			break;
			//多选
		case R.id.check:
			builder.setTitle("多选对话框");
			builder.setIcon(R.drawable.ic_launcher);
			final boolean[] checked = new boolean[] { true, true, true, false };
			builder.setMultiChoiceItems(new String[] { "大学", "高中", "初中", "小学" }, checked,
					new OnMultiChoiceClickListener() {
						String str1, str2, str3, str4;

						@Override
						public void onClick(DialogInterface dialog, int which, boolean isChecked) {
							checked[which] = isChecked;
							Log.d("lhm", Arrays.toString(checked));
						}
					});

			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				String str1,str2,str3,str4;
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (checked[0]) {
						str1 = "大学";
					}else {
						str1 = "";
					}
					if (checked[1]) {
						str2 = "高中";
					}else {
						str2 = "";
					}
					if (checked[2]) {
						str3 = "初中";
					}else {
						str3 = "";
					}
					if (checked[3]) {
						str4 = "小学";
					}else {
						str4 = "";
					}
					str=str1+str2+str3+str4;
					Log.d("lhm", "确定了"+str);
				}
			});
			builder.create().show();
			break;
			//输入文本
		case R.id.input:
			builder.setTitle("输入文本对话框");
			builder.setIcon(R.drawable.ic_launcher);
			LayoutInflater inflater=LayoutInflater.from(this);
			View view=inflater.inflate(R.layout.input, null);
			final EditText et=(EditText) view.findViewById(R.id.input_tv);
			builder.setView(view);
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					str=et.getText().toString();
					Log.d("lhm", "确定了"+str);
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Log.d("lhm", "取消了");
					dialog.dismiss();
				}
			});
			builder.create().show();
			break;
			//进度条
		case R.id.progress:
			final ProgressDialog progressDialog=new ProgressDialog(this);
			progressDialog.setTitle("进度条对话框");
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setIndeterminate(false);
			progressDialog.show();
			new Thread(){
				@Override
				public void run() {
					int progressNum=progressDialog.getProgress();
					while (progressNum<=100) {
						progressDialog.setProgress(progressNum);
						try {
							Thread.sleep(1000);
							progressNum+=10;
							super.run();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					progressDialog.cancel();
				};
			}.start();
			break;
		case R.id.charactor_picker:
			EditText et1=new EditText(this);
			et1.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
			final String options="0123456789ABCDEF";
			CharacterPickerDialog cpd= new CharacterPickerDialog(this, new View(this), null,options,false){
				public void onClick (View v){
					Log.d("lhm", "你按下了"+((Button)v).getText().toString()); 
	            	dismiss(); 
				}
			};
			cpd.show();
			break;
		case R.id.date_picker:
			//创建一个日历引用c,通过静态方法从制定时区,获得日期实例
			Calendar c=Calendar.getInstance(Locale.CANADA);
			Date myDate=new Date();
			c.setTime(myDate);
			int year=c.get(Calendar.YEAR);
			int month=c.get(Calendar.MONTH);
			int day=c.get(Calendar.DAY_OF_MONTH);
			DatePickerDialog dpd=new DatePickerDialog(this, this, year, month, day);
			dpd.show();
			break;
		case R.id.time_picker:
			Calendar c1=Calendar.getInstance(Locale.CANADA);
			Date myDate1=new Date();
			c1.setTime(myDate1);
			int hour=c1.get(Calendar.HOUR_OF_DAY);
			int minute=c1.get(Calendar.MINUTE);
			int second=c1.get(Calendar.SECOND);
			TimePickerDialog tpd=new TimePickerDialog(this, this, hour, minute, true);
			tpd.show();
			break;
		case R.id.my_dialog:
			MyDialog myDialog=new MyDialog(MainActivity.this,R.style.dialog);
			myDialog.bind(this);
			myDialog.show();
			break;
		default:
			break;
		}
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
		Log.d("lhm",year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
	}

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		Log.d("lhm",hourOfDay+":"+minute);
	}

	@Override
	public void onok(String str) {
		Log.d("lhm",str);
	}
}

效果图:

单选:

多选:

       

输入文字:

ProgressDialog:

CharacterPickerDialog:

DatePicker:

TimePicker:

自定义对话框:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值