如何使用Android实现短信、微信、微博分享功能

  介绍

这篇文章主要介绍了如何使用Android实现短信、微信、微博分享功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。

在纠结了几天的图表功能之后,我开始开发一个新的功能,即分享内容到短信、微信、微博等渠道,对应的我有一个简单的任务:

<李>

在工具栏写分享的按钮

<李>

绘制一个Android的分享页面

<李>

编写短信分享示例

<李>

编写社交分享

在这一天,我只完成了前面的三部分。

<强>工具栏上的分享按钮

工具栏在主要还是靠ImageView来绘制右上角的分享按钮:

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; android.support.v7.widget.Toolbar  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,xmlns:工具栏=癶ttp://schemas.android.com/apk/res-auto"   ,xmlns:工具=癶ttp://schemas.android.com/tools"   ,android: id=癅 + id/toolbar"   ,android: layout_width=癿atch_parent"   ,android: layout_height=? attr/actionBarSize"   ,android:背景=? attr/colorPrimaryDark"   ,android:重力=癱enter"祝辞   & lt; TextView   android:才能id=癅 + id/toolbar_title"   android:才能layout_width=皐rap_content"   android:才能layout_height=皐rap_content"   android:才能layout_gravity=癱enter"   android才能:文本=皒xx",/比;   & lt; ImageView   android:才能能见度=癷nvisible"   android:才能id=癅 + id/share"   android:才能layout_width=皐rap_content"   android:才能layout_height=皐rap_content"   android:才能paddingEnd=癅dimen/length_24"   android:才能paddingStart=癅dimen/length_16"   android:才能paddingTop=癅dimen/length_16"   android:才能paddingBottom=癅dimen/length_16"   android:才能layout_gravity=皉ight"   android: src=https://www.yisu.com/zixun/才能“@drawable/share_icon”   工具:忽视=" RtlHardcoded "/>   

然后在加载到数据的时候,将这个元素变为可见:

share.setVisibility (View.VISIBLE);

<强>短信分享示例

在实现UI之前,我先写了一个简单的分享功能:

@OnClick (R.id.share)   void  shareAction (), {   ,BaseShare  smsShare =, ShareFactory.create (“SMS");   ,String  text =, information.getTitle (), +,“:“, +, information.getTitle ();   ,smsShare.share(,,文本);   }

随后将其重构为简单的工厂模式:

public  static  BaseShare  getShareType (String 类型),{   ,switch (类型),{   case 才能“SMS":   ,,return  new  SMSShare ();   case 才能“WEIBO":   ,,return  new  WeiboShare ();   case 才能“MOMENTS":   ,,return  new  MomentsShare ();   case 才能“WECHAT":   ,,return  new  WechatShare ();   ,}   ,return 零;   }

对应于不同的分享类型,都有不同的类来做相应的处理。

<强>使用对话框绘制底部分享

在最开始的时候,我使用的是对话框来绘制底部的布局:

void  showShareDialog (), {   ,Dialog  bottomDialog =, new 对话框(这个,,R.style.BottomDialog);   ,View  contentView =, LayoutInflater.from(这).inflate (R.layout.bottom_share, null);   ,bottomDialog.setContentView (contentView);   ,ViewGroup.LayoutParams  layoutParams =, contentView.getLayoutParams ();=,,layoutParams.width  getresource () .getDisplayMetrics () .widthPixels;   ,contentView.setLayoutParams (layoutParams);   ,bottomDialog.getWindow () .setGravity (Gravity.BOTTOM);   ,bottomDialog.setCanceledOnTouchOutside(真正的);   ,bottomDialog.getWindow () .setWindowAnimations (R.style.BottomDialog_Animation);   ,bottomDialog.show ();   以前,}

然后简单地了解了一下动画效果:

& lt; style  name=癇ottomDialog"比;   ,& lt; item  name=癮ndroid: windowNoTitle"祝辞true   ,& lt; item  name=癮ndroid: windowBackground"祝辞@android:颜色/transparent   & lt;/style>   & lt; style  name=癇ottomDialog.Animation",父母=癆nimation.AppCompat.Dialog"比;   ,& lt; item  name=癮ndroid: windowEnterAnimation"祝辞@anim/translate_dialog_in   ,& lt; item  name=癮ndroid: windowExitAnimation"祝辞@anim/translate_dialog_out

如何使用Android实现短信、微信、微博分享功能