Android关于自定义的dialog

(2013-01-15 16:34:34)

转载

 

 

 

标签:

it

 

 

 

 首先在布局中写出你自己想要的dialog的样式Android关于自定义的dialog

由于我的图片需要平铺            android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@drawable/note_top"
           android:orientation="horizontal" >
       ,自己写了一个平铺的方法。所以在布局中看到的样子反而不正常。在手机上能正常显示。

 实现平铺的方法很简单
 public static void fixBackgroundRepeatY(View view) {   
       Drawable bg = view.getBackground();  
       if (bg != null) {        
              if (bg instanceof BitmapDrawable) {           
                BitmapDrawable bmp = (BitmapDrawable) bg;          
                bmp.mutate(); // make sure that we aren't sharing state anymore
                bmp.setTileModeXY(null,TileMode.REPEAT);       
               
      }
}

 

 接下来就开始写java代码了,第一步肯定是extends Dialog

直接贴代码

public class HPDialog extends Dialog {

 // 先调用构造方法在调用oncreate方法

 private static boolean isShow = true;
 private Context context;
 private String mYtitle;
 private String mYmsg;

 // private MyDialog myDialog;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
 }

 public HPDialog() {

  super(ClientEngine.getInstance().getBaseActivity());
  this.context = ClientEngine.getInstance().getBaseActivity();
 }

 public HPDialog(Context context, int theme) {
  super(context, theme);
  this.context = context;
 }

 public void setTitle(String title) {
  this.mYtitle = title;
 }

 public void setMsg(String msg) {
  this.mYmsg = msg;
 }
 @Override
 public void show() {
  super.show();
 }
 public static class Builder {

  private Context context;
  private String title;
  private String message;
  private String positiveButtonText;
  private String negativeButtonText;
  private View contentView;
  private DialogInterface.OnClickListener positiveButtonClickListener,
    negativeButtonClickListener;

  // private TextView msg=(TextView)findViewById(R.id.message);
  public Builder(Context context) {
   this.context = context;
  }

  public Builder setMessage(String message) {
   this.message = message;
   return this;
  }

  public Builder setMessage(int message) {
   this.message = (String) context.getText(message);
   return this;
  }

  public Builder setTitle(int title) {
   this.title = (String) context.getText(title);
   return this;
  }

  public Builder setTitle(String title) {
   this.title = title;
   return this;
  }

  public Builder setContentView(View v) {
   this.contentView = v;
   return this;
  }
  
  public Builder setPositiveButton(int positiveButtonText,
    DialogInterface.OnClickListener listener) {
   this.positiveButtonText = (String) context
     .getText(positiveButtonText);
   this.positiveButtonClickListener = listener;
   return this;
  }

  public Builder setPositiveButton(String positiveButtonText,
    DialogInterface.OnClickListener listener) {
   this.positiveButtonText = positiveButtonText;
   this.positiveButtonClickListener = listener;
   return this;
  }
  
  
  public boolean setCancelable(boolean cancelable){
   
   isShow = cancelable;
   return isShow;
  }

  public Builder setNegativeButton(int negativeButtonText,
    DialogInterface.OnClickListener listener) {
   this.negativeButtonText = (String) context
     .getText(negativeButtonText);
   this.negativeButtonClickListener = listener;
   return this;
  }
  
  public Builder setClickFunction(final Command[] commands) {
   if(commands != null)
   {
    if(commands[0]==null){
     this.setPositiveButton("知道了", new OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog,
        int which) {

      }
     });
    }else{
     this.setPositiveButton(commands[0].getLabel(), new OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog,
        int which) {
       commands[0].onClick();
      }
     });
    }
    if(commands[1]!=null){
     this.setNegativeButton(commands[1].getLabel(), new OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog,
        int which) {
       commands[1].onClick();
      }
     });
    }
   }
   return this;
  }

  public Builder setNegativeButton(String negativeButtonText,
    DialogInterface.OnClickListener listener) {
   this.negativeButtonText = negativeButtonText;
   this.negativeButtonClickListener = listener;
   return this;
  }

  public HPDialog show() {
   HPDialog dialog = create();
   dialog.show();
   return dialog;
  }
  
  public HPDialog create() {
   LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   
   // instantiate the dialog with the custom Theme
   final HPDialog dialog = new HPDialog(context, R.style.HPDialog);
   dialog.setCanceledOnTouchOutside(false);//android 4.0以上dialog点击其他地方也会消失false以后就只能点击按钮消失
   
   View layout = inflater.inflate(R.layout.dialog, null);
   
   ScrollView sv = (ScrollView) layout.findViewById(R.id.HpDialogScrollView);
   
   int width = context.getResources().getDrawable(R.drawable.note_top).getIntrinsicWidth();
   
   
   sv.getLayoutParams().width=width-30;
   
   if(message!=null && message.length()>160){
    
    sv.getLayoutParams().height=MainMenu.screenHeight*2/5;
    
   }
   
   dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
   // set the dialog title
   ((TextView)layout.findViewById(R.id.title)).setText(title);
   
   // set the confirm button
   if (positiveButtonText != null) {
    
    ((Button) layout.findViewById(R.id.dialog_button_ok)).setText(positiveButtonText);
    
    if (positiveButtonClickListener != null) {
     
     ((Button) layout.findViewById(R.id.dialog_button_ok)).setOnClickListener(new View.OnClickListener() {
      
        public void onClick(View v) {
         
         positiveButtonClickListener.onClick(dialog,DialogInterface.BUTTON_POSITIVE);
         dialog.dismiss();
        }
       });
    }
   } else {
    
    // if no confirm button just set the visibility to GONE
    layout.findViewById(R.id.dialog_button_ok).setVisibility(View.GONE);
   }
   if (negativeButtonText != null) {
    
    ((Button) layout.findViewById(R.id.dialog_button_cancel)).setText(negativeButtonText);
    if (negativeButtonClickListener != null) {
     
     
//     ((Button) layout.findViewById(R.id.dialog_button_ok)).
     
     ((Button) layout.findViewById(R.id.dialog_button_cancel)).setOnClickListener(new View.OnClickListener() {
      
        public void onClick(View v) {
         
         negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
         
         dialog.dismiss();
        }
       });
    }
   } else {
    
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ((Button) layout.findViewById(R.id.dialog_button_ok)).getLayoutParams();
   
    params.setMargins(30, 0, 30, 0);//这个只是根据我自己的需要调节按钮的位置。大家可以根据自己的需要来调节
    params.width = width-60;
    ((Button) layout.findViewById(R.id.dialog_button_ok)).setLayoutParams(params);
    
    // if no confirm button just set the visibility to GONE
    layout.findViewById(R.id.dialog_button_cancel).setVisibility(View.GONE);
   }
   
   // set the cancel button

   // set the content message
   
   if (message != null) {
    
    ((TextView) layout.findViewById(R.id.msg)).setText(message);
    
   } else if (contentView != null) {
    
    // if no message set
    // add the contentView to the dialog body
    
    ((LinearLayout) layout.findViewById(R.id.body)).removeAllViews();
    
    ((LinearLayout) layout.findViewById(R.id.body)).addView(
      contentView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
   }
   dialog.setContentView(layout);
   return dialog;
  }
 }
}

 

这样一个自定义的dialog就搞定了。用法和系统的一样。只是换了个显示样式

 

稳定

产品高可用性高并发

贴心

项目群及时沟通

专业

产品经理1v1支持

快速

MVP模式小步快跑

承诺

我们选择声誉

坚持

10年专注高端品质开发
  • 返回顶部