成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

鴻蒙開源三方組件--強大的彈窗庫XPopup組件

開源
文章由鴻蒙社區產出,想要了解更多內容請前往:51CTO和華為官方戰略合作共建的鴻蒙技術社區https://harmonyos.51cto.com

[[396595]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

1. 介紹

​XPopup是一個彈窗庫,可能是Harmony平臺最好的彈窗庫。它從設計的時候就本著實用的原則,兼顧了美觀和優雅的交互。用戶都喜歡自然舒適的UI和交互,希望XPopup能帶給你一些幫助或者驚喜!

2. 效果一覽

相關效果演示可以點擊鏈接前往原文章進行觀看:

https://harmonyos.51cto.com/posts/4198

3. 依賴

  1. allprojects{ 
  2.     repositories{ 
  3.         mavenCentral() 
  4.     } 
  5. implementation 'io.openharmony.tpc.thirdlib:XPopup:1.0.3' 

4. 如何使用

4.1 內置彈窗的使用

4.1.1 顯示確認和取消對話框

  1. new XPopup.Builder(getContext()).asConfirm("我是標題""我是內容"
  2.         new OnConfirmListener() { 
  3.             @Override 
  4.             public void onConfirm() { 
  5.                 toast("click confirm"); 
  6.             } 
  7.         }) 
  8.         .show(); 

4.1.2 顯示待輸入框的確認和取消對話框

  1. new XPopup.Builder(getContext()).asInputConfirm("我是標題""請輸入內容。"
  2.                             new OnInputConfirmListener() { 
  3.                                 @Override 
  4.                                 public void onConfirm(String text) { 
  5.                                     toast("input text: " + text); 
  6.                                 } 
  7.                             }) 
  8.                             .show(); 

4.1.3 顯示中間彈出的列表彈窗

  1. new XPopup.Builder(getContext()) 
  2.                             //.maxWidth(600) 
  3.                             .asCenterList("請選擇一項", new String[]{"條目1""條目2""條目3""條目4"}, 
  4.                             new OnSelectListener() { 
  5.                                 @Override 
  6.                                 public void onSelect(int position, String text) { 
  7.                                     toast("click " + text); 
  8.                                 } 
  9.                             }) 
  10.                             .show(); 

4.1.4 顯示中間彈出的加載框

  1. new XPopup.Builder(getContext()) 
  2.                             .asLoading("正在加載中"
  3.                             .show(); 

4.1.5 顯示從底部彈出的列表彈窗

  1. new XPopup.Builder(getContext()) 
  2.         .asBottomList("請選擇一項", new String[]{"條目1""條目2""條目3""條目4""條目5"}, 
  3.                 new OnSelectListener() { 
  4.                     @Override 
  5.                     public void onSelect(int position, String text) { 
  6.                         toast("click " + text); 
  7.                     } 
  8.                 }) 
  9.         .show(); 

4.1.6 顯示依附于某個Component或者某個點的彈窗

  1. new XPopup.Builder(getContext()) 
  2.         .atView(component)  // 依附于所點擊的Component,內部會自動判斷在上方或者下方顯示 
  3.         .asAttachList(new String[]{"分享""編輯""不帶icon"}, 
  4.                 new int[]{ResourceTable.Media_icon, ResourceTable.Media_icon}, 
  5.                 new OnSelectListener() { 
  6.                     @Override 
  7.                     public void onSelect(int position, String text) { 
  8.                         toast("click " + text); 
  9.                     } 
  10.                 }) 
  11.         .show(); 

如果是想依附于某個Component的觸摸點,則需要先watch該Component,然后當單擊或長按觸發的時候去顯示:

  1. Component component = findComponentById(ResourceTable.Id_btnShowAttachPoint); 
  2. // 必須在事件發生前,調用這個方法來監視View的觸摸 
  3. final XPopup.Builder builder = new XPopup.Builder(getContext()).watchView(component); 
  4. component.setLongClickedListener(new LongClickedListener() { 
  5.     @Override 
  6.     public void onLongClicked(Component component) { 
  7.         builder.asAttachList(new String[]{"置頂""復制""刪除"}, null
  8.                 new OnSelectListener() { 
  9.                     @Override 
  10.                     public void onSelect(int position, String text) { 
  11.                         toast("click " + text); 
  12.                     } 
  13.                 }) 
  14.                 .show(); 
  15.     } 
  16. }); 

asAttachList方法內部是對AttachPopupView的封裝,如果你的布局不是列表,可以繼承AttachPopupView實現自己想要的布局。AttachPopupView會出現在目標的上方或者下方,如果你想要出現在目標的左邊或者右邊(像微信朋友圈那樣點贊的彈窗),可以繼承HorizontalAttachPopupView,然后編寫你的布局即可。

最簡單的示例如下:

  1. public class CustomAttachPopup extends HorizontalAttachPopupView { 
  2.     public CustomAttachPopup(Context context) { 
  3.         super(context, null); 
  4.     } 
  5.     @Override 
  6.     protected int getImplLayoutId() { 
  7.         return ResourceTable.Layout_custom_attach_popup; 
  8.     } 
  9.     @Override 
  10.     protected void onCreate() { 
  11.         super.onCreate(); 
  12.         findComponentById(ResourceTable.Id_tv_zan).setClickedListener(new ClickedListener() { 
  13.             @Override 
  14.             public void onClick(Component component) { 
  15.                 ToastUtil.showToast(getContext(), "贊"); 
  16.                 dismiss(); 
  17.             } 
  18.         }); 
  19.         findComponentById(ResourceTable.Id_tv_comment).setClickedListener(new ClickedListener() { 
  20.             @Override 
  21.             public void onClick(Component component) { 
  22.                 ToastUtil.showToast(getContext(), "評論"); 
  23.                 dismiss(); 
  24.             } 
  25.         }); 
  26.     }    // 設置狀態欄的高度,用以修正自定義位置彈窗的高度 
  27.     @Override 
  28.     protected int setStatusBarHeight() { 
  29.         return 130; 
  30.     }} 

4.1.7 顯示大圖瀏覽彈窗

  1. // 當你點擊圖片的時候執行以下代碼: 
  2. // 多圖片場景(你有多張圖片需要瀏覽) 
  3. new XPopup.Builder(getContext()).asImageViewer(image, position, list, 
  4.                             new OnSrcViewUpdateListener() { 
  5.                                 @Override 
  6.                                 public void onSrcViewUpdate(ImageViewerPopupView popupView, int position) { 
  7.                                     // pager更新當前顯示的圖片 
  8.                                     // 當啟用isInfinite時,position會無限增大,需要映射為當前ViewPager中的頁 
  9.                                     int realPosi = position % list.size(); 
  10.                                     pager.setCurrentPage(realPosi, false); 
  11.                                 } 
  12.                             }, new ImageLoader()).show(); 
  13. // 單張圖片場景 
  14. new XPopup.Builder(getContext()) 
  15.                 .asImageViewer(image, url, new ImageLoader()) 
  16.                 .show();// 圖片加載器,XPopup不負責加載圖片,需要你實現一個圖片加載器傳給我,這里以Glide和OkGo為例(可直接復制到項目中): 
  17. class ImageLoader implements XPopupImageLoader { 
  18.         @Override 
  19.         public void loadImage(int position, String url, Image imageView) { 
  20.             // 一進入頁面就加載圖片的話,需要加點延遲 
  21.             context.getUITaskDispatcher().delayDispatch(new Runnable() { 
  22.                 @Override 
  23.                 public void run() { 
  24.                     Glide.with(context) 
  25.                             .load(url) 
  26.                             .diskCacheStrategy(DiskCacheStrategy.ALL
  27.                             .into(image); 
  28.                 } 
  29.             }, 50); 
  30.         } 
  31.         // 必須實現這個方法,用來下載圖片。可參照下面的實現,內部保存圖片會用到。如果你不需要保存圖片這個功能,可以返回null。 
  32.         @Override 
  33.         public File getImageFile(Context context, String url) { 
  34.             try { 
  35.                 return OkGo.<File>get(url).tag(this).converter(new FileConvert()).adapt().execute().body(); 
  36.             } catch (Exception e) { 
  37.                 LogUtil.error(TAG, e.getMessage()); 
  38.             } 
  39.             return null
  40.         } 
  41.     } 

如果你用的不是Glide,請參考去實現即可。

4.1.8 關閉彈窗

先拿到彈窗對象,以Loading彈窗為例,其他也是一樣:

  1. BasePopupView popupView = new XPopup.Builder(getContext()) 
  2.         .asLoading("正在加載中"
  3.         .show(); 

執行消失:

  1. //有四個消失方法可供選擇: 
  2. popupView.dismiss(); 
  3.  //立即消失 
  4. popupView.delayDismiss(300); 
  5.  //延時消失,有時候消失過快體驗可能不好,可以延時一下 
  6. popupView.smartDismiss();  
  7. //會等待彈窗的開始動畫執行完畢再進行消失,可以防止接口調用過快導致的動畫不完整。 
  8. popupView.dismissWith({});  
  9. //消失動畫執行完之后執行某些邏輯 

我們在項目中經常會點擊某個按鈕然后關閉彈窗,接著去做一些事。比如:點擊一個按鈕,關閉彈窗,然后開啟一個界面,要知道彈窗的關閉是有一個動畫過程的,上面的寫法會出現彈窗還沒有完全關閉,就立即跳頁面,界面有一種頓挫感;而且在設備資源不足的時候,還可能造成丟幀。所以很多時候不推薦直接使用dismiss()方法,除非你關閉完彈窗后面沒有任何邏輯執行。

為了得到最佳體驗,您可以等dismiss動畫完全結束去執行一些東西,而不是立即就執行。可以這樣做:

  1. popupView.dismissWith(new Runnable() { 
  2.     @Override 
  3.     public void run() { 
  4.         // 跳轉到新頁面 
  5.     }}); 

每個彈窗本身也有onShow()和onDismiss()的生命周期回調,可以根據需要使用。

還有這樣一種場景:彈窗show()完之后,你的邏輯執行完畢,然后調用dismiss()。但是你的邏輯執行過快,可能導致彈窗的show動畫還沒有執行完就直接dismiss了,界面上的感覺并不好。這個時候推薦使用smartDismiss()方法,這個方法能保證彈窗的show動畫執行完再關閉彈窗。

4.1.9 復用項目已有布局

如果你項目中已經有寫好的彈窗布局,而想用XPopup提供的動畫和交互能力,也是完全沒有問題的。目前支持設置自定義布局的彈窗有:

Confirm彈窗,就是確認和取消彈窗

  • 帶輸入框的Confirm彈窗
  • Loading彈窗
  • 帶列表的Attach彈窗,Center彈窗和Bottom彈窗

假設,你想使用XPopup的Confirm彈窗,但布局想用自己的,只需要這樣設置一下,其他不用動即可:

  1. new XPopup.Builder(getContext()) 
  2.         .asConfirm(null"您可以復用項目已有布局,來使用XPopup強大的交互能力和邏輯封裝,彈窗的布局完全由你自己控制。\n" + 
  3.                         "需要注意的是:你自己的布局必須提供一些控件Id,否則XPopup找不到控件。"
  4.                 "關閉""XPopup牛逼"
  5.                 new OnConfirmListener() { 
  6.                     @Override 
  7.                     public void onConfirm() { 
  8.                         toast("click confirm"); 
  9.                     } 
  10.                 }, nullfalse, ResourceTable.Layout_my_confim_popup)//綁定已有布局 
  11.         .show(); 

這樣布局就是您自己的了,動畫和交互XPopup會幫你完成。但是需要注意的是,你自己提供的布局必須包含一些id,否則XPopup無法找到控件;id必須有,控件你可以隨意組合與擺放。具體如下:

  • Confirm彈窗必須包含的Text以及id有:tv_title,tv_content,tv_cancel,tv_confirm
  • 帶輸入框的Confirm彈窗在Confirm彈窗基礎上需要增加一個id為et_input的TextField
  • Loading彈窗,如果你想顯示一個Loading文字說明,則必須有一個id為tv_title的Text;如果不需要文字說明,則沒要求
  • 帶列表的彈窗會多一個bindItemLayout()方法用來綁定item布局
  • 其他不在多說,看bindLayout方法說明,會說明要求哪些id

每種內置彈窗的bindLayout和bindItemLayout的要求都在方法說明上,無需記憶,用的時候查看下方法的說明即可。

4.2 自定義彈窗

當你自定義彈窗的時候,需要根據需求選擇繼承CenterPopupView,BottomPopupView,AttachPopupView/HorizontalAttachPopupView,DrawerPopupView,PartShadowPopupView,FullScreenPopupView,PositionPopupView其中之一。

每種彈窗的功能和使用場景如下:

  • CenterPopupView:中間彈窗的彈窗,比如:確認取消對話框,Loading彈窗等,如果不滿意默認的動畫效果,可以設置不同的動畫器
  • BottomPopupView:從底部彈出的彈窗,比如:從底部彈出的分享彈窗,知乎的從底部彈出的評論彈窗,抖音從底部彈出的評論彈窗。這種彈窗帶有智能的嵌套滾動和手勢拖動
  • AttachPopupView/HorizontalAttachPopupView:Attach彈窗是需要依附于某個點或者某個Component來顯示的彈窗;其中AttachPopupView會出現在目標的上方或者下方。如果希望想要微信朋友圈點贊彈窗那樣的效果,出現在目標的左邊或者右邊,則需要繼承 HorizontalAttachPopupView來做
  • DrawerPopupView:從界面的左邊或者右邊彈出的像DrawerLayout那樣的彈窗,Drawer彈窗本身是橫向滑動的,但對PageSlider和ScrollView等橫向滑動控件做了兼容,在彈窗內部可以放心使用它們
  • FullScreenPopupView:全屏彈窗,看起來和Ability 一樣。該彈窗其實是繼承Center彈窗進行的一種實現,可以設置任意的動畫器
  • ImageViewerPopupView:大圖瀏覽彈窗
  • PositionPopupView:自由定位彈窗,如果你想讓彈窗顯示在左上角,或者右上角,或者任意位置,并且不需要依附任何Component,此時你需要它

自定義彈窗只有2個步驟:

一:根據自己的需求編寫一個類繼承對應的彈窗

二:重寫getImplLayoutId()返回彈窗的布局,在onCreate中像Ability那樣編寫你的邏輯即可

注意:自定義彈窗本質是一個自定義控件,但是只需重寫一個參數的構造,其他的不要重寫,所有的自定義彈窗都是這樣。

4.2.1 自定義Center彈窗

  1. class CustomPopup extends CenterPopupView { 
  2.     //注意:自定義彈窗本質是一個自定義控件,但是只需重寫一個參數的構造,其他的不要重寫,所有的自定義彈窗都是這樣 
  3.     public CustomPopup(Context context) { 
  4.         super(context, null); 
  5.     } 
  6.     // 返回自定義彈窗的布局 
  7.     @Override 
  8.     protected int getImplLayoutId() { 
  9.         return ResourceTable.Layout_custom_popup; 
  10.     } 
  11.     // 執行初始化操作,比如:findComponentById,設置點擊,或者任何你彈窗內的業務邏輯 
  12.     @Override 
  13.     protected void onCreate() { 
  14.         super.onCreate(); 
  15.         findComponentById(ResourceTable.Id_tv_close).setClickedListener(new ClickedListener() { 
  16.             @Override 
  17.             public void onClick(Component component) { 
  18.                 dismiss(); // 關閉彈窗 
  19.             } 
  20.         }); 
  21.     } 
  22.     // 設置最大寬度,看需要而定 
  23.     @Override 
  24.     protected int getMaxWidth() { 
  25.         return super.getMaxWidth(); 
  26.     } 
  27.     // 設置最大高度,看需要而定 
  28.     @Override 
  29.     protected int getMaxHeight() { 
  30.         return super.getMaxHeight(); 
  31.     } 
  32.     // 設置自定義動畫器,看需要而定 
  33.     @Override 
  34.     protected PopupAnimator getPopupAnimator() { 
  35.         return super.getPopupAnimator(); 
  36.     } 
  37.     // 彈窗的寬度,用來動態設定當前彈窗的寬度,受getMaxWidth()限制 
  38.     protected int getPopupWidth() { 
  39.         return 0; 
  40.     } 
  41.     // 彈窗的高度,用來動態設定當前彈窗的高度,受getMaxHeight()限制 
  42.     protected int getPopupHeight() { 
  43.         return 0; 
  44.     }} 

注意:Center彈窗的最大寬度默認是屏幕寬度的0.8,如果你自定義布局的寬度是寫死的,有可能超出屏幕寬度的0.8,如果你不想被最大寬度限制,只需要重寫方法:

  1. @Overrideprotected int getMaxWidth() { 
  2.     return 0; 
  3.    //返回0表示不限制最大寬度 

使用自定義彈窗:

  1. new XPopup.Builder(getContext()) 
  2.         .asCustom(new CustomPopup(getContext())) 
  3.         .show(); 

4.2.2 自定義Attach彈窗

  1. public class CustomAttachPopup2 extends AttachPopupView { 
  2.     public CustomAttachPopup2(Context context) { 
  3.         super(context, null); 
  4.     } 
  5.     @Override 
  6.     protected int getImplLayoutId() { 
  7.         return ResourceTable.Layout_custom_attach_popup2; 
  8.     } 
  9.     // 設置狀態欄的高度,用以修正自定義位置彈窗的高度 
  10.     @Override 
  11.     protected int setStatusBarHeight() { 
  12.         return 130; 
  13.     }} 

4.2.3 自定義DrawerLayout類型彈窗

  1. public class CustomDrawerPopupView extends DrawerPopupView { 
  2.     public CustomDrawerPopupView(Context context) { 
  3.         super(context, null); 
  4.     } 
  5.     @Override 
  6.     protected int getImplLayoutId() { 
  7.         return ResourceTable.Layout_custom_list_drawer; 
  8.     } 
  9.     @Override 
  10.     protected void onCreate() { 
  11.         super.onCreate(); 
  12.         findComponentById(ResourceTable.Id_btn).setClickedListener(new ClickedListener() { 
  13.             @Override 
  14.             public void onClick(Component component) { 
  15.                 toast("nothing!!!"); 
  16.             } 
  17.         }); 
  18.     }} 

使用自定義的DrawerLayout彈窗:

  1. new XPopup.Builder(getContext()) 
  2.         .popupPosition(PopupPosition.Right)//右邊 
  3.         .asCustom(new CustomDrawerPopupView(getContext())) 
  4.         .show(); 

4.2.4 自定義Bottom類型的彈窗

自定義Bottom類型的彈窗會比較常見,默認Bottom彈窗帶有手勢交互和嵌套滾動;如果您不想要手勢交互可以調用enableDrag(false)方法關閉。

請注意:彈窗的寬高是自適應的,大部分情況下都應該將彈窗布局的高設置為match_content;除非你希望得到一個高度撐滿的彈窗。

Demo中有一個模仿知乎評論的實現,代碼如下:

  1. public class ZhihuCommentPopup extends BottomPopupView { 
  2.     ListContainer listContainer; 
  3.     public ZhihuCommentPopup(Context context) { 
  4.         super(context, null); 
  5.     } 
  6.     @Override 
  7.     protected int getImplLayoutId() { 
  8.         return ResourceTable.Layout_custom_bottom_popup; 
  9.     } 
  10.     @Override 
  11.     protected void onCreate() { 
  12.         super.onCreate(); 
  13.         listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer); 
  14.         ArrayList<String> strings = new ArrayList<>(); 
  15.         for (int i = 0; i < 30; i++) { 
  16.             strings.add(""); 
  17.         } 
  18.         EasyProvider commonAdapter = new EasyProvider<String>(getContext(), strings, ResourceTable.Layout_adapter_zhihu_comment) { 
  19.             @Override 
  20.             protected void bind(ViewHolder holder, String itemData, final int position) {} 
  21.         }; 
  22.         listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() { 
  23.             @Override 
  24.             public void onItemClicked(ListContainer listContainer, Component component, int position, long id) { 
  25.                 dismiss(); 
  26.             } 
  27.         }); 
  28.         listContainer.setItemProvider(commonAdapter); 
  29.     } 
  30.     // 最大高度為Window的0.7 
  31.     @Override 
  32.     protected int getMaxHeight() { 
  33.         return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f); 
  34.     }} 

4.2.5 自定義全屏彈窗

  1. public class CustomFullScreenPopup extends FullScreenPopupView { 
  2.     public CustomFullScreenPopup(Context context) { 
  3.         super(context, null); 
  4.     } 
  5.     @Override 
  6.     protected int getImplLayoutId() { 
  7.         return ResourceTable.Layout_custom_fullscreen_popup; 
  8.     } 
  9.     @Override 
  10.     protected void onCreate() { 
  11.         super.onCreate(); 
  12.         // 初始化 
  13.     }} 

4.2.6 自定義ImageViewer彈窗

目前大圖瀏覽彈窗支持在上面添加任意自定義布局和背景顏色,做法是寫一個類繼承ImageViewerPopupView彈窗,然后重寫布局即可。

代碼如下:

  1. public class CustomImagePopup extends ImageViewerPopupView { 
  2.     public CustomImagePopup(Context context) { 
  3.         super(context, null); 
  4.     } 
  5.     @Override 
  6.     protected int getImplLayoutId() { 
  7.         return ResourceTable.Layout_custom_image_viewer_popup; 
  8.     }} 

由于是自定義的大圖瀏覽彈窗,就要用自定義彈窗的方式來開啟了:

  1. // 自定義的彈窗需要用asCustom來顯示,之前的asImageViewer這些方法當然不能用了。 
  2. CustomImagePopup viewerPopup = new CustomImagePopup(getContext()); 
  3. // 自定義的ImageViewer彈窗需要自己手動設置相應的屬性,必須設置的有srcView,url和imageLoader。 
  4. viewerPopup.setSingleSrcView(image2, url2); 
  5. viewerPopup.setXPopupImageLoader(new ImageLoader()); 
  6. new XPopup.Builder(getContext()) 
  7.         .asCustom(viewerPopup) 
  8.         .show(); 

4.2.7 自定義Position彈窗

  1. public class QQMsgPopup extends PositionPopupView { 
  2.     public QQMsgPopup(Context context) { 
  3.         super(context, null); 
  4.     } 
  5.     @Override 
  6.     protected int getImplLayoutId() { 
  7.         return ResourceTable.Layout_popup_qq_msg; 
  8.     }} 

自由定位彈窗,默認是顯示在屏幕的左上角,你可以通過offsetX()和offsetY()來控制顯示位置,如果你希望水平居中,可以用isCenterHorizontal(true)選項來做到。

  1. new XPopup.Builder(getContext()) 
  2.         .popupAnimation(PopupAnimation.ScaleAlphaFromCenter) 
  3.         .isCenterHorizontal(true
  4.         .offsetY(200) 
  5.         .asCustom(new QQMsgPopup(getContext())) 
  6.         .show(); 

4.3 自定義動畫

自定義動畫已經被設計得非常簡單,動畫和彈窗是無關的;這意味著你可以將動畫設置給內置彈窗或者自定義彈窗。繼承PopupAnimator,實現3個方法:

  • 如何初始化動畫
  • 動畫如何開始
  • 動畫如何結束

比如:自定義一個旋轉的動畫:

  1. class RotateAnimator extends PopupAnimator { 
  2.   @Override 
  3.   public void initAnimator() { 
  4.       targetView.setScaleX(0.0f); 
  5.       targetView.setScaleY(0.0f); 
  6.       targetView.setAlpha(0.0f); 
  7.       targetView.setRotation(360.0f); 
  8.   } 
  9.   @Override 
  10.   public void animateShow() { 
  11.       targetView.createAnimatorProperty().rotate(0.0f).scaleX(1.0f).scaleY(1.0f).alpha(1.0f).setDuration(getDuration()).start(); 
  12.   } 
  13.   @Override 
  14.   public void animateDismiss() { 
  15.       targetView.createAnimatorProperty().rotate(720.0f).scaleX(0.0f).scaleY(0.0f).alpha(0.0f).setDuration(getDuration()).start(); 
  16.   }} 

使用自定義動畫:

  1. new XPopup.Builder(getContext()) 
  2.         .customAnimator(new RotateAnimator()) 
  3.         .asConfirm("演示自定義動畫""當前的動畫是一個自定義的旋轉動畫,無論是自定義彈窗還是自定義動畫,已經被設計得非常簡單;這個動畫代碼只有6行即可完成!"null
  4.         .show(); 

不想要動畫:

  1. new XPopup.Builder(getContext()) 
  2.         .customAnimator(new EmptyAnimator(null)) 
  3.         .asConfirm("演示自定義動畫""當前的動畫是一個自定義的旋轉動畫,無論是自定義彈窗還是自定義動畫,已經被設計得非常簡單;這個動畫代碼只有6行即可完成!"null
  4.         .show(); 

4.4 常用設置

4.4.1 全局設置

1.設置主色調 默認情況下,XPopup的主色為灰色,主色作用于Button文字,TextField邊框和光標,Check文字的顏色上。 主色調只需要設置一次即可,可以放在啟動頁中。

  1. XPopup.setPrimaryColor(getColor(ResourceTable.Color_colorPrimary)); 

2.設置全局的動畫時長 默認情況下,彈窗的動畫時長為360毫秒。你可以通過下面的方法進行修改:

  1. XPopup.setAnimationDuration(200); // 傳入的時長最小為0,動畫的時長會影響除Drawer彈窗外的所有彈窗 

4.4.2 常用設置

所有的設置如下,根據需要使用:

  1. new XPopup.Builder(getContext()) 
  2.         .isDestroyOnDismiss(true) //是否在消失的時候銷毀資源,默認false。如果你的彈窗對象只使用一次,非常推薦設置這個,可以杜絕內存泄漏。如果會使用多次,千萬不要設置 
  3.         .dismissOnBackPressed(true) //按返回鍵是否關閉彈窗,默認為true 
  4.         .dismissOnTouchOutside(true) //點擊外部是否關閉彈窗,默認為true 
  5.         .autoOpenSoftInput(true) //是否彈窗顯示的同時打開輸入法,只在包含輸入框的彈窗內才有效,默認為false 
  6.         .popupAnimation(PopupAnimation.ScaleAlphaFromCenter) //設置內置的動畫 
  7.         .customAnimator(null) //設置自定義的動畫器 
  8.         .popupPosition(PopupPosition.Right) //手動指定彈窗出現在目標的什么位置,對Attach和Drawer類型彈窗生效 
  9.         .positionByWindowCenter(false) //默認是false,是否以屏幕中心進行定位,默認是false,為false時根據Material范式進行定位,主要影響Attach系列彈窗 
  10.         .offsetX(-10) //彈窗在x方向的偏移量        .offsetY(-10) //彈窗在y方向的偏移量 
  11.         .maxWidth(10) //設置彈窗的最大寬度,如果重寫彈窗的getMaxWidth(),以重寫的為準 
  12.         .maxHeight(10) //設置彈窗的最大高度,如果重寫彈窗的getMaxHeight(),以重寫的為準 
  13.         .isCenterHorizontal(true) //是否和目標水平居中,比如:默認情況下Attach彈窗依靠著目標的左邊或者右邊,如果isCenterHorizontal為true,則與目標水平居中對齊 
  14.         .isRequestFocus(false) //默認為true,默認情況下彈窗會搶占焦點,目的是為了響應返回按鍵按下事件;如果為false,則不搶焦點 
  15.         .enableDrag(true) //是否啟用拖拽,默認為true,目前對Bottom和Drawer彈窗有用 
  16.         .isDarkTheme(true)  //是否啟用暗色主題        .borderRadius(10)  //為彈窗設置圓角,默認是15,對內置彈窗生效 
  17.         .autoDismiss(false) //操作完畢后是否自動關閉彈窗,默認為true;比如點擊ConfirmPopup的確認按鈕,默認自動關閉;如果為false,則不會關閉 
  18.         .setPopupCallback(new SimpleCallback() { //設置顯示和隱藏的回調 
  19.             @Override            public void onCreated(BasePopupView basePopupView) { 
  20.                 // 彈窗內部onCreate執行完調用 
  21.             } 
  22.             @Override 
  23.             public void beforeShow(BasePopupView basePopupView) { 
  24.                 // 每次show之前都會執行 
  25.             } 
  26.             @Override 
  27.             public void onShow(BasePopupView basePopupView) { 
  28.                 // 完全顯示的時候執行 
  29.             } 
  30.             @Override 
  31.             public void onDismiss(BasePopupView basePopupView) { 
  32.                 // 完全隱藏的時候執行 
  33.             } 
  34.             // 如果你自己想攔截返回按鍵事件,則重寫這個方法,返回true即可 
  35.             @Override 
  36.             public boolean onBackPressed(BasePopupView basePopupView) { 
  37.                 new ToastDialog(getContext()).setText("我攔截的返回按鍵,按返回鍵XPopup不會關閉了").show(); 
  38.                 return true; //默認返回false 
  39.             } 
  40.             //監聽彈窗拖拽,適用于能拖拽的彈窗 
  41.             @Override 
  42.             public void onDrag(BasePopupView popupView, int value, float percent,boolean upOrLeft) { 
  43.             } 
  44.         }) 
  45.         .asXXX() //所有的設置項都要寫在asXXX()方法調用之前 

5. 下載鏈接

5.1 IDE下載鏈接

https://developer.harmonyos.com/cn/develop/deveco-studio#download

5.2 源碼鏈接

https://gitee.com/openharmony-tpc/XPopup

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

[[396596]]

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2021-08-09 10:24:49

鴻蒙HarmonyOS應用

2021-08-02 14:54:50

鴻蒙HarmonyOS應用

2021-04-29 14:32:24

鴻蒙HarmonyOS應用

2021-03-10 15:03:40

鴻蒙HarmonyOS應用

2021-08-03 12:47:58

鴻蒙HarmonyOS應用

2021-01-27 10:04:46

鴻蒙HarmonyOS動畫

2021-04-28 09:56:44

鴻蒙HarmonyOS應用

2021-03-24 09:30:49

鴻蒙HarmonyOS應用

2021-08-04 14:16:41

鴻蒙HarmonyOS應用

2021-08-03 10:07:41

鴻蒙HarmonyOS應用

2021-03-05 09:58:50

鴻蒙HarmonyOS開源

2021-11-17 15:37:43

鴻蒙HarmonyOS應用

2021-08-26 16:07:46

鴻蒙HarmonyOS應用

2021-07-06 18:21:31

鴻蒙HarmonyOS應用

2021-04-08 14:57:52

鴻蒙HarmonyOS應用

2021-04-20 15:06:42

鴻蒙HarmonyOS應用

2021-08-05 15:06:30

鴻蒙HarmonyOS應用

2021-08-30 17:55:58

鴻蒙HarmonyOS應用

2021-03-03 09:42:26

鴻蒙HarmonyOS圖片裁剪

2021-03-01 14:00:11

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 综合二区 | 天天躁日日躁狠狠躁白人 | 国产精品亚洲一区二区三区在线 | 国产美女一区二区三区 | 久久成人18免费网站 | 久久精品国产一区二区三区不卡 | 国产乱码精品一区二区三区忘忧草 | 成人高清在线视频 | 黄色片视频免费 | 91xx在线观看| 免费在线性爱视频 | 91精品入口蜜桃 | 欧洲尺码日本国产精品 | 亚州综合一区 | 久久岛国 | 午夜影院黄 | 亚洲一区二区免费 | 国产 91 视频| 欧美精三区欧美精三区 | 中文字幕成人免费视频 | 6080yy精品一区二区三区 | 日韩免费在线观看视频 | 成人在线视频网站 | 99亚洲综合 | 国产精品视频在线免费观看 | 国产精品高清在线 | 国产精品一区二区三区在线 | 久久91精品 | 欧美视频一区二区三区 | 欧日韩不卡在线视频 | 精品国产一区二区三区观看不卡 | 蜜桃视频在线观看免费视频网站www | 亚洲视频在线播放 | 亚洲欧洲一区二区 | 一区二区视频在线 | 国产在线精品一区二区三区 | 国产在线麻豆精品入口 | 日本一道本视频 | 国产成人精品免高潮在线观看 | 久久精品一级 | 久久亚洲一区二区 |