`

Android自定义Dialog

 
阅读更多

 

主activity

 

  1. package com.su.testcustomdialog;  
  2.    
  3. import com.su.testcustomdialog.MyDialog.Dialogcallback;  
  4.    
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.    
  11. public class CustomDialogActivity extends Activity {  
  12.         private TextView textView;  
  13.    
  14.         /** Called when the activity is first created. */  
  15.         @Override  
  16.         public void onCreate(Bundle savedInstanceState) {  
  17.                 super.onCreate(savedInstanceState);  
  18.                 setContentView(R.layout.main);  
  19.                 textView = (TextView) findViewById(R.id.textView11);  
  20.                 Button button = (Button) findViewById(R.id.button1);  
  21.                 button.setOnClickListener(new View.OnClickListener() {  
  22.                         @Override  
  23.                         public void onClick(View v) {  
  24.                                 MyDialog myDialog = new MyDialog(CustomDialogActivity.this);  
  25.                                 myDialog.setContent("哥来自Activity");  
  26.                                 myDialog.setDialogCallback(dialogcallback);  
  27.                                 myDialog.show();  
  28.    
  29.                         }  
  30.                 });  
  31.         }  
  32. /** 
  33.  * 设置mydialog需要处理的事情 
  34.  */  
  35.         Dialogcallback dialogcallback = new Dialogcallback() {  
  36.    
  37.                 @Override  
  38.                 public void dialogdo(String string) {  
  39.                         textView.setText("哥來自Dialog: " + string);  
  40.    
  41.                 }  
  42.         };  
  43.    
  44. }  


然后是MyDialog的核心了

 


 

  1. package com.su.testcustomdialog;  
  2. import android.app.Dialog;  
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.widget.EditText;  
  7. import android.widget.TextView;  
  8. /** 
  9.  * 自定义dialog 
  10.  * @author sfshine 
  11.  * 
  12.  */  
  13. public class MyDialog {  
  14.    
  15.         Context context;  
  16.         Dialogcallback dialogcallback;  
  17.         Dialog dialog;  
  18.         Button sure;  
  19.         TextView textView;  
  20.         EditText editText;  
  21.         /** 
  22.          * init the dialog 
  23.          * @return 
  24.          */  
  25.         public MyDialog(Context con) {  
  26.                 this.context = con;  
  27.                 dialog = new Dialog(context, R.style.dialog);  
  28.                 dialog.setContentView(R.layout.dialog);  
  29.                 textView = (TextView) dialog.findViewById(R.id.textview);  
  30.                 sure = (Button) dialog.findViewById(R.id.button1);  
  31.                 editText = (EditText) dialog.findViewById(R.id.editText1);  
  32.    
  33.                 sure.setOnClickListener(new View.OnClickListener() {  
  34.    
  35.                         @Override  
  36.                         public void onClick(View v) {  
  37.                                 dialogcallback.dialogdo(editText.getText().toString());  
  38.                                 dismiss();  
  39.    
  40.                         }  
  41.                 });  
  42.    
  43.         }  
  44.         /** 
  45.          * 设定一个interfack接口,使mydialog可以處理activity定義的事情 
  46.          * @author sfshine 
  47.          * 
  48.          */  
  49.         public interface Dialogcallback {  
  50.                 public void dialogdo(String string);  
  51.         }  
  52.    
  53.         public void setDialogCallback(Dialogcallback dialogcallback) {  
  54.                 this.dialogcallback = dialogcallback;  
  55.         }  
  56.    
  57.         /** 
  58.          * @category Set The Content of the TextView 
  59.          * */  
  60.         public void setContent(String content) {  
  61.                 textView.setText(content);  
  62.         }  
  63.    
  64.         /** 
  65.          * Get the Text of the EditText 
  66.          * */  
  67.         public String getText() {  
  68.                 return editText.getText().toString();  
  69.         }  
  70.    
  71.         public void show() {  
  72.                 dialog.show();  
  73.         }  
  74.    
  75.         public void hide() {  
  76.                 dialog.hide();  
  77.         }  
  78.    
  79.         public void dismiss() {  
  80.                 dialog.dismiss();  
  81.         }  
  82.    
  83. }  


dialog的布局

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_margin="30.0dp"  
  6.     android:orientation="vertical"  
  7.     android:padding="10dip" >  
  8.    
  9.     <!-- 这里如果使用android:layout_width="5000dip"设置一个极大的值 系统就会 -->  
  10.    
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_margin="30.0dp"  
  15.         android:gravity="center"  
  16.         android:text="自定义Dialog"  
  17.         android:textColor="#F0F"  
  18.         android:textSize="20dip" />  
  19.    
  20.     <TextView  
  21.         android:id="@+id/textview"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content" />  
  24.    
  25.     <EditText  
  26.         android:id="@+id/editText1"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:ems="10" >  
  30.     </EditText>  
  31.    
  32.     <Button  
  33.         android:id="@+id/button1"  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_gravity="right"  
  37.         android:text="確定" />  
  38.    
  39. </LinearLayout>  


style 文件

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <style name="dialog" parent="@android:style/Theme.Dialog">  
  5.         <item name="android:windowFrame">@null</item>  
  6.         <item name="android:windowIsFloating">true</item>  
  7.         <item name="android:windowIsTranslucent">false</item>  
  8.         <item name="android:windowNoTitle">true</item>  
  9.         <item name="android:background">#FFF</item>  
  10.         <item name="android:windowBackground">@android:color/transparent</item>  
  11.         <item name="android:backgroundDimEnabled">true</item>  
  12.     </style>  
  13.    
  14. </resources>  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics