• <kbd id="qyk40"></kbd>
  • <strike id="qyk40"></strike><samp id="qyk40"><pre id="qyk40"></pre></samp>

     

    Android關(guān)于自定義的dialog

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

    轉(zhuǎn)載

     

     

     

    標(biāo)簽:

    it

     

     

     

     首先在布局中寫出你自己想要的dialog的樣式Android關(guān)于自定義的dialog

    由于我的圖片需要平鋪            android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:background="@drawable/note_top"
               android:orientation="horizontal" >
           ,自己寫了一個(gè)平鋪的方法。所以在布局中看到的樣子反而不正常。在手機(jī)上能正常顯示。

     實(shí)現(xiàn)平鋪的方法很簡(jiǎn)單
     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 {

     // 先調(diào)用構(gòu)造方法在調(diào)用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點(diǎn)擊其他地方也會(huì)消失false以后就只能點(diǎn)擊按鈕消失
       
       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);//這個(gè)只是根據(jù)我自己的需要調(diào)節(jié)按鈕的位置。大家可以根據(jù)自己的需要來調(diào)節(jié)
        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;
      }
     }
    }

     

    這樣一個(gè)自定義的dialog就搞定了。用法和系統(tǒng)的一樣。只是換了個(gè)顯示樣式

     

    穩(wěn)定

    產(chǎn)品高可用性高并發(fā)

    貼心

    項(xiàng)目群及時(shí)溝通

    專業(yè)

    產(chǎn)品經(jīng)理1v1支持

    快速

    MVP模式小步快跑

    承諾

    我們選擇聲譽(yù)

    堅(jiān)持

    10年專注高端品質(zhì)開發(fā)
    • 返回頂部
    人人妻人人做人人爽精品| 成人国内精品视频在线观看| 国产午夜精品一区二区| 日韩人妻潮喷中文在线视频| 国产精品第44页| 久久精品无码中文字幕| 精品欧洲AV无码一区二区男男 | 久久久国产精品网站| 久热精品视频第一页| re99热久久这里只有精品| 欧美日韩精品一区二区在线视频| 国产在线精品99一卡2卡| 欧洲国产成人精品91铁牛tv| 国产日产欧产精品精品蜜芽| 国产精品机视频大陆| 国产乱码精品一区二区三区中| 91精品国产福利在线观看麻豆| 99久久久国产精品免费牛牛四川| 无码国产精品一区二区免费模式| 久久精品人人槡人妻人人玩| 国产精品免费一区二区三区四区| 中文精品久久久久人妻不卡| 成人区人妻精品一区二区不卡视频| 51午夜精品免费视频| 国产色精品vr一区区三区| 亚洲A∨精品一区二区三区| 四虎国产精品成人| 香蕉依依精品视频在线播放 | 国产999精品2卡3卡4卡| 久久精品国产一区二区三区| 国产精品 猎奇 另类视频| 国产成人精品综合在线观看| 国产精品成人无码久久久久久| 亚洲日韩中文在线精品第一 | 青青青国产精品一区二区| 老司机67194精品线观看| 99视频精品在线| 亚洲精品亚洲人成在线观看麻豆| 亚洲国产精品免费在线观看| 国产精品成人在线| 欧美精品久久久久a片一二三区 |