自定義透明樣式Dialog以及對多輸入框進行是否爲空的判斷

自定義本身的Dialog類,經過接口實現dialog上的控件的點擊事件 java

package see.ilifeactivity; android

import ilife.see.R;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; app

public class Num_Dialog extends AlertDialog implements OnClickListener{
 
 private Button ok,cancel;
 private myDialogListener listener;
 public Num_Dialog(Context context, int theme ,myDialogListener listener) {
     super(context, theme);
     this.listener = listener;
 }
 public interface myDialogListener{
  public void onClik(View View);
 }
 
 
 public void onClick(View v) {
        listener.onClik(v);
    }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.number_limit);
  init();
 }
 private void init() {
  ok=(Button)findViewById(R.id.ok_btn);
  cancel=(Button)findViewById(R.id.cancel_btn);
  ok.setOnClickListener(this);
  cancel.setOnClickListener(this);
  // TODO Auto-generated method stub
  
 }
} ide


 下面的類是調用Dialog類,實現接口 佈局

 

package see.ilifeactivity; ui

import ilife.see.R;
import see.ilifeactivity.MyDialog.OnDialogClickListener;
import see.ilifeactivity.Num_Dialog.myDialogListener;
import see.mainindex.ui.activity.SlidingActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast; this

public class OrganizeActivity extends Activity implements OnDialogClickListener,myDialogListener
{
 private Button finish_btn,back;
 private ImageView organize_add_img;
 private EditText act_name_et,act_time_et,act_address_et,act_type_et,act_number_et,act_info_et;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.organize_act);
  init();
 } spa

 private void init() {
  // TODO Auto-generated method stub
  findViews();
  finish_btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent it = new Intent(OrganizeActivity.this,SlidingActivity.class);
    startActivity(it);
   }
  }); .net

  organize_add_img.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  
   MyDialog dialog = new MyDialog(OrganizeActivity.this, R.style.MyDialog);
                    
   dialog.setContentView(R.layout.photo_dialog); 
   dialog.setCanceledOnTouchOutside(true);
   
   dialog.setOnDialogClickListener(OrganizeActivity.this);
   
   //設置點擊Dialog外部任意區域關閉Dialog
   dialog.show(); 
   
  }
 });
  act_number_et.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
   
    MyDialog dialog = new MyDialog(OrganizeActivity.this, R.style.MyDialog);                      
    dialog.setContentView(R.layout.number_limit); 
    dialog.setCanceledOnTouchOutside(true);
    
    dialog.setOnDialogClickListener(OrganizeActivity.this);
    
    //設置點擊Dialog外部任意區域關閉Dialog
    dialog.show(); 
    
   }
  });
  act_type_et=(EditText)findViewById(R.id.act_type_et);
      act_type_et.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     final CharSequence[] items = { "娛樂興趣", "交友聯誼", "學術社團","其餘" }; xml

     // 建立builder

     AlertDialog.Builder builder = new AlertDialog.Builder(

     OrganizeActivity.this);

     builder.setTitle("請選擇活動類型") // 標題

     .setIcon(R.drawable.activity_icon) // icon

     .setCancelable(false) // 不響應back按鈕

     .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

     @Override
     
     public void onClick(DialogInterface dialog, int which) {
      
     act_type_et.setText(items[which]);
                   
     Toast.makeText(OrganizeActivity.this,
     
     "選擇了" + items[which], Toast.LENGTH_SHORT)
     
     .show();
     }
     
     });
     
     builder.setPositiveButton("ok", null);
     
     builder.setNegativeButton("cencel",null);

     builder.create().show();
     
    }
   });
      finish_btn=(Button)findViewById(R.id.finish_btn);
      finish_btn.setOnClickListener(new MyListener(this));
      act_number_et.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub

    //調用上面的類
     Num_Dialog dialog=new Num_Dialog(OrganizeActivity.this,R.style.MyDialog,new myDialogListener(){

      @Override
      public void onClik(View v) {
       // TODO Auto-generated method stub
       switch(v.getId()){
       case R.id.ok_btn:
        Intent it=new Intent(OrganizeActivity.this,EditActivity.class);
        startActivity(it);
        break;
       case R.id.cancel_btn:
        break;
       case R.id.num_limit_et:
        break;
       
       }
       
      }
      
     });
     dialog.show(); 
    }
   });
     
 }
 @Override
 public void OnClick(View v) {
  // TODO Auto-generated method stub
  int id = v.getId();
  switch(id)
  {
  case R.id.photographing:
   Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
   startActivityForResult(getImageByCamera, 1);
   break;
  case R.id.local:
   Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
   getImage.addCategory(Intent.CATEGORY_OPENABLE);
   getImage.setType("image/jpeg");
   startActivityForResult(getImage, 0);
   break;
  case R.id.finish_btn:
   
  }
  
  
 }
 
 
 private void findViews(){
  finish_btn = (Button)findViewById(R.id.finish_btn);
  back = (Button)findViewById(R.id.back);
  organize_add_img=(ImageView)findViewById(R.id.org_head_img);
  act_name_et=(EditText)findViewById(R.id.act_name_et);
  act_time_et=(EditText)findViewById(R.id.act_time_et);
  act_address_et=(EditText)findViewById(R.id.act_address_et);
  act_type_et=(EditText)findViewById(R.id.act_type_et);
  act_number_et=(EditText)findViewById(R.id.number_limit_et);
  act_info_et=(EditText)findViewById(R.id.act_info_et);
  
 }

 @Override
 public void onClik(View View) {
  // TODO Auto-generated method stub
  
 }
 
}
//該類爲驗證輸入框是否爲空,且有相應提示
class MyListener extends java.lang.Object implements Button.OnClickListener 

 Activity act; 
  public MyListener(Activity act) 
  { 
      this.act = act; 
  } 
  private boolean ValidateEditText(int id, String name) 
  {   EditText et=(EditText)act.findViewById(id);
      String str=et.getText().toString();
      if(id!=R.id.act_info_et){
      if (isEmpty(str)) 
      {  
         new AlertDialog.Builder(act)
         .setTitle("Android 提示")
         .setMessage(name+"不能爲空")
         .show();
          return false; 
      }
      return true; 
      }
      else{
       if (isEmpty(str)||tooLong(str)) 
          {  
           new AlertDialog.Builder(act)
           .setTitle("Android 提示")
           .setMessage(name+"不能爲空也不能多於150字")
           .show();
              return false; 
          }
          return true;
      }
  } 

     
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (!ValidateEditText(R.id.act_name_et, "用戶名")) 
          return; 
      if (!ValidateEditText(R.id.act_time_et, "活動時間"))           
          return; 
      if (!ValidateEditText(R.id.act_address_et, "活動地點"))
          return; 
      if (!ValidateEditText(R.id.act_type_et, "活動性質"))        
          return; 
      if (!ValidateEditText(R.id.num_limit_et, "活動人數上限"))      
          return; 
      if (!ValidateEditText(R.id.act_info_et, "活動簡介"))      
          return; 
     
     
  } 
 
 //驗證是否爲空的方法
  public  boolean isEmpty(String str) {

      if (str == null || str.trim().length() == 0) {

          return true;

      } else {

          return false;

      }


 }
  //此方法爲驗證簡介字數是否大於150字
  public boolean tooLong(String str){
   if(str.trim().length()>150)
    
    return true;

   else
    
    return false;
   
  }
 

 

dialog的佈局文件

number_limit.xml

<?xml version="1.0" encoding="UTF-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

     

        <TextView
            android:id="@+id/num_limit_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="51dp"
            android:text="活動人數上限"
            android:textAppearance="?android:attr/textAppearanceLarge" />

         <EditText
             android:id="@+id/num_limit_et"
             android:layout_width="210dp"
             android:layout_height="wrap_content"
             android:layout_alignParentLeft="true"
             android:layout_alignParentRight="true"
             android:hint="不能超過羣組人數上限"
             android:layout_below="@+id/num_limit_tv" />

         <Button
             android:id="@+id/ok_btn"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignLeft="@+id/num_limit_tv"
             android:layout_below="@+id/num_limit_et"
             android:layout_marginTop="16dp"
             android:text="肯定" />

         <Button
             android:id="@+id/cancel_btn"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignBaseline="@+id/ok_btn"
             android:layout_alignBottom="@+id/ok_btn"
             android:layout_marginLeft="24dp"
             android:layout_toRightOf="@+id/num_limit_tv"
             android:text="取消" />

    </RelativeLayout>

最後在styles.xml文件中加入如下代碼

<style name="MyDialog" parent=" @android :Theme.Dialog">         <item name="android:windowFrame">@null</item>         <item name="android:windowNoTitle">true</item>         <!-- //對話框無標題-->         <item name="android:windowBackground">@null</item>         <!-- //此對話框的背景  -->         <!-- 設置背景遷安透明 @color/transparent -->         <item name="android:windowIsFloating">true</item>         <!-- //對話框是否浮動-->         <item name="android:windowContentOverlay">@null</item>         <!-- //對話框是否有遮蓋-->         <item name="android:backgroundDimEnabled">false</item>     </style>     <style name="Theme.Transparent" parent="android:Theme">         <item name="android:windowBackground">@null</item>         <!-- //此對話框的背景  -->         <item name="android:windowIsTranslucent">true</item>         <!-- //對話框是否透明-->         <item name="android:windowContentOverlay">@null</item>         <!-- //對話框是否有遮蓋-->         <item name="android:windowNoTitle">true</item>         <!-- //對話框無標題-->         <item name="android:windowIsFloating">true</item>         <!-- //對話框是否浮動-->         <item name="android:backgroundDimEnabled">false</item>  </style>   <style name="notitle" parent="android:Theme.Light">   <item name="android:windowNoTitle">true</item>   <item name="android:background">@color/transparent</item>   <item name="android:cacheColorHint">@color/transparent</item>e  </style>
相關文章
相關標籤/搜索