android AlertDialog布局 ——2

来源:互联网 发布:android studio mac 编辑:程序博客网 时间:2024/05/17 08:23

之前写了一篇android 关于AlertDialog 布局的博文,上次的布局是用setContentView()来进行布局,并强调了一些代码的编写顺序,今天我用setview对alertdialong进行布局。

代码如下:

 

Java代码  收藏代码
  1. /** 
  2.      * 打开保存对话框框 
  3.      */  
  4.     private void openSaveDialog()  
  5.     {  
  6.         savePhotoDialog = new AlertDialog.Builder(SaveAndShareActivity.this);  
  7.         LayoutInflater factory = LayoutInflater.from(SaveAndShareActivity.this);  
  8.         View view = factory.inflate(R.layout.save_dialog_layout, null);  
  9.         editText = (EditText) view.findViewById(R.id.EditTextPhotoName);  
  10.         savePhotoDialog.setIcon(R.drawable.icon_save);  
  11.         savePhotoDialog.setTitle("保存图片");  
  12.         savePhotoDialog.setView(view);  
  13.         savePhotoDialog.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  14.             public void onClick(DialogInterface dialog, int whichButton)  
  15.             {  
  16.                 savePhotoName = editText.getText().toString();  
  17.                 if (savePhotoName != null)  
  18.                 {  
  19.                     Toast.makeText(SaveAndShareActivity.this, savePhotoName, Toast.LENGTH_SHORT)  
  20.                             .show();  
  21.                     savePhotoDialog.create().dismiss();  
  22.                 }  
  23.                 else  
  24.                 {  
  25.                     Toast.makeText(SaveAndShareActivity.this"文件名不能为空!", Toast.LENGTH_SHORT)  
  26.                             .show();  
  27.                 }  
  28.             }  
  29.         });  
  30.         savePhotoDialog.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  31.             public void onClick(DialogInterface dialog, int whichButton)  
  32.             {  
  33.                 savePhotoDialog.create().dismiss();  
  34.             }  
  35.         });  
  36.         savePhotoDialog.create().show();  
  37.     }  

 

布局文件:

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="260dip"  
  4.     android:layout_height="200dip"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/TextViewPhotoName"  
  10.         android:layout_width="200dip"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="@string/save_photo_name"  
  13.         android:textColor="#fff"  
  14.         android:textSize="20sp" />  
  15.   
  16.     <EditText  
  17.         android:id="@+id/EditTextPhotoName"  
  18.         android:layout_width="200dip"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="@string/default_photo_name" />  
  21.   
  22. </LinearLayout>  
 

这些代码摘自我项目的一部分,大家捡一些有用的信息,ok!

 

注:个人见解,可提意见或建议,勿扔板砖,谢谢!