Android中的互动交流 之 Dialog

一、Dialog

本来按照书上的打了一遍,发现有showDialog()方法,但是有过时警告。。。才发现这个方法已经被DialogFragment给替代了。。。无语了。。。发展太快了啊。现在又4.x了,我写了个alertdialog发现取消居然在左边,Google太反人类了吧,这太不习惯了。。。

恩言归正传,这个总结主要是dialog的几种用法。

1、只有确定和取消的dialog

protected void dialog() {
			 AlertDialog.Builder builder = new Builder(MainActivity.this);
			 builder.setMessage("确认退出吗?").setTitle("提示").setPositiveButton("确认", new DialogInterface.OnClickListener() {
				 public void onClick(DialogInterface dialog, int which) {
					 dialog.dismiss();
					 MainActivity.this.finish();
					 }
				 })
			 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
				 public void onClick(DialogInterface dialog, int which) {
					 dialog.dismiss();
					 }
				 })
			 .create().show();
			 }



2、3个button

protected void dialog() {
		AlertDialog dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.btn_star)
				.setTitle("调查").setMessage("你喜欢coding吗?")
				.setPositiveButton("很喜欢",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(MainActivity.this, "我很喜欢coding。",Toast.LENGTH_LONG).show(); }})
				.setNegativeButton("不喜欢", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(MainActivity.this, "我不喜欢coding。", Toast.LENGTH_LONG).show();}})
				.setNeutralButton("一般", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(MainActivity.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG).show();}}).create();
		dialog.show();
	
	 }







3、可以输入文字的dialog

protected void dialog() {
			new AlertDialog.Builder(this).setTitle("请输入").setIcon(
				    android.R.drawable.ic_dialog_info).setView(
			        new EditText(this)).setPositiveButton("确定", null)
				    .setNegativeButton("取消", null).show();
			 }



4、单选框

protected void dialog() {
			new AlertDialog.Builder(this).setTitle("单选框").setIcon(
				     android.R.drawable.ic_dialog_info).setSingleChoiceItems(
				     new String[] { "Item1", "Item2" }, 0,
				     new DialogInterface.OnClickListener() {
				      public void onClick(DialogInterface dialog, int which) {
				       dialog.dismiss();}
				    }).setNegativeButton("取消", null).show();
			 }



5、复选框

protected void dialog() {
			new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
				     new String[] { "Item1", "Item2" }, null, null)
				     .setPositiveButton("确定", null)
				     .setNegativeButton("取消", null).show();
			 }



6、自定义

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:background="#ffffffff" 
    android:orientation="horizontal" 
    android:id="@+id/dialog" >
   <TextView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/name" 
    android:text="姓名:" />
   <EditText 
   android:layout_height="wrap_content"
   android:layout_width="wrap_content" 
   android:id="@+id/etname" 
   android:minWidth="100dip" />
</LinearLayout>

protected void dialog() {
	    LayoutInflater inflater = getLayoutInflater();
		   View layout = inflater.inflate(R.layout.layout,
		     (ViewGroup) findViewById(R.id.dialog));
		   new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
		     .setPositiveButton("确定", null)
		     .setNegativeButton("取消", null).show();
			 }



7、日期选择

protected void dialog() {
		Calendar calendar = Calendar.getInstance();
        DatePickerDialog.OnDateSetListener dateListener =   
            new DatePickerDialog.OnDateSetListener() {  
                @Override  
                public void onDateSet(DatePicker datePicker,   
                        int year, int month, int dayOfMonth) {  
                	Toast.makeText(MainActivity.this,
                            year + "年" + (month+1) + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show();  
                }
            };  
        DatePickerDialog dlg = new DatePickerDialog(
                MainActivity.this,dateListener,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dlg.show();
        }



8、时间选择

protected void dialog() {
		Calendar calendar = Calendar.getInstance();
        TimePickerDialog.OnTimeSetListener timeListener =   
            new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                    Toast.makeText(MainActivity.this,
                            hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();  
                }  
               
            };  
        TimePickerDialog dlg = new TimePickerDialog(
                MainActivity.this,timeListener,
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),true);
        dlg.show();
        }





参考文章:

http://android.tgbus.com/Android/tutorial/201107/359812.shtml

http://blog.csdn.net/wuwo333/article/details/7666158

& my out-of-time textbook    -。=

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值