android自定义dialog

自定义 Dialog 

[功能] 
android 提供给我们的只有2种Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特点:1. 不是 Activity 2. 开销比 Activity 小得多 
鉴于以上的优点 我们就有定制自己Dialog 的需求 

[原理] 
1. android 系统提供了一个class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先调用系统的"super.onCreate(savedInstanceState)" 其就会保证调用这个method. 
2. 至于 Dialog 界面的定制 可以写一个xml 文件 然后 在 "void onCreate(Bundle)" 通过 "setContentView()" 来使之生效 
3. Dialog 使用问题: 1. 弹出:show() 2. 取消:dismiss() 


[代码] 
1. 创建一个 Dialog . 
Java代码   收藏代码
  1. public class CustomDialog extends Dialog {  
  2.   
  3.     public CustomDialog(Context context) {  
  4.         super(context);  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.       
  8.      protected void onCreate(Bundle savedInstanceState){  
  9.          super.onCreate(savedInstanceState);  
  10.            
  11.          setContentView(R.layout.custom_dialog);  
  12.          setTitle("Custom Dialog");  
  13.   
  14.          TextView text = (TextView)findViewById(R.id.text);  
  15.          text.setText("Hello, this is a custom dialog!");  
  16.          ImageView image = (ImageView)findViewById(R.id.image);  
  17.          image.setImageResource(R.drawable.sepurple);  
  18.            
  19.          Button buttonYes = (Button) findViewById(R.id.button_yes);  
  20.          buttonYes.setHeight(5);  
  21.          buttonYes.setOnClickListener(new Button.OnClickListener(){  
  22.   
  23.                 public void onClick(View v) {  
  24.                     // TODO Auto-generated method stub  
  25.                     dismiss();  
  26.                       
  27.                 }  
  28.             });  
  29.          Button buttonNo = (Button) findViewById(R.id.button_no);  
  30.          buttonNo.setSingleLine(true);  
  31.          buttonNo.setOnClickListener(new Button.OnClickListener(){  
  32.   
  33.                 public void onClick(View v) {  
  34.                     // TODO Auto-generated method stub  
  35.                     dismiss();  
  36.                       
  37.                 }  
  38.             });  
  39.      }  
  40.        
  41.      //called when this dialog is dismissed  
  42.      protected void onStop() {  
  43.          Log.d("TAG","+++++++++++++++++++++++++++");  
  44.      }  
  45.        
  46.   
  47. }  



2. Dialog 的使用: 
Java代码   收藏代码
  1. public class CustomDialogUsage extends Activity {  
  2.     CustomDialog cd;  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.           
  9.         cd = new CustomDialog(this);  
  10.           
  11.           
  12.         Button buttonYes = (Button) findViewById(R.id.main_button);  
  13.         buttonYes.setOnClickListener(new OnClickListener(){  
  14.   
  15.             public void onClick(View v) {  
  16.                 // TODO Auto-generated method stub  
  17.                 cd.show();  
  18.                   
  19.             }  
  20.         });  
  21.   
  22.     }  
  23.       
  24. }  


3. Dialog 的界面定制. 
Java代码   收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.               android:id="@+id/layout_root"  
  3.               android:orientation="horizontal"  
  4.               android:layout_width="fill_parent"  
  5.               android:layout_height="fill_parent"  
  6.               android:padding="10dp"  
  7.               >  
  8.     <ImageView android:id="@+id/image"  
  9.                android:layout_width="wrap_content"  
  10.                android:layout_height="fill_parent"  
  11.                android:layout_marginRight="10dp"  
  12.                />  
  13.     <LinearLayout   
  14.         android:orientation="vertical"  
  15.         android:layout_width="fill_parent"   
  16.         android:layout_height="wrap_content"  
  17.         android:padding="5px"   
  18.         >  
  19.     <TextView android:id="@+id/text"  
  20.               android:layout_width="wrap_content"  
  21.               android:layout_height="fill_parent"  
  22.               android:textColor="#FFF"  
  23.               />  
  24.     <LinearLayout   
  25.         android:orientation="horizontal"  
  26.         android:layout_width="fill_parent"   
  27.         android:layout_height="wrap_content"  
  28.         android:padding="5px"   
  29.         >  
  30.     <Button android:id="@+id/button_yes"  
  31.               android:layout_width="wrap_content"  
  32.               android:layout_height="wrap_content"  
  33.               android:text=" Yes "  
  34.               android:gravity="center"  
  35.               />  
  36.     <Button android:id="@+id/button_no"  
  37.               android:layout_width="wrap_content"  
  38.               android:layout_height="wrap_content"  
  39.               android:text=" No "  
  40.               android:gravity="center"  
  41.               />  
  42.     </LinearLayout>  
  43.     </LinearLayout>  
  44. </LinearLayout>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值