如何使用Android實(shí)現(xiàn)短信、微信、微博分享功能

這篇文章主要介紹了如何使用Android實(shí)現(xiàn)短信、微信、微博分享功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

電白網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),電白網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為電白千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的電白做網(wǎng)站的公司定做!

在糾結(jié)了幾天的圖表功能之后,我開始開發(fā)一個(gè)新的功能。即分享內(nèi)容到短信、微信、微博等渠道,對應(yīng)的我有一個(gè)簡單的 Task:

  • 在 Toolbar 寫分享的按鈕

  • 繪制一個(gè) Android 的分享頁面

  • 編寫短信分享示例

  • 編寫社交分享

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

Toolbar 上的分享按鈕

在 Toolbar 主要還是靠 ImageView 來繪制右上角的分享按鈕:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:toolbar="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="?attr/actionBarSize"
 android:background="?attr/colorPrimaryDark"
 android:gravity="center">
 <TextView
  android:id="@+id/toolbar_title"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:text="xxx" />
 <ImageView
  android:visibility="invisible"
  android:id="@+id/share"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:paddingEnd="@dimen/length_24"
  android:paddingStart="@dimen/length_16"
  android:paddingTop="@dimen/length_16"
  android:paddingBottom="@dimen/length_16"
  android:layout_gravity="right"
  android:src="@drawable/share_icon"
  tools:ignore="RtlHardcoded" />
</android.support.v7.widget.Toolbar>

然后在加載到數(shù)據(jù)的時(shí)候,將這個(gè)元素變?yōu)榭梢姡?/p>

share.setVisibility(View.VISIBLE);

短信分享示例

在實(shí)現(xiàn) UI 之前,我先寫了一個(gè)簡單的分享功能:

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

隨后將其重構(gòu)為簡單的工廠模式:

public static BaseShare getShareType(String type) {
 switch (type) {
  case "SMS":
   return new SMSShare();
  case "WEIBO":
   return new WeiboShare();
  case "MOMENTS":
   return new MomentsShare();
  case "WECHAT":
   return new WechatShare();
 }
 return null;
}

對應(yīng)于不同的分享類型,都有不同的類來做相應(yīng)的處理。

使用 Dialog 繪制底部分享

在最開始的時(shí)候,我使用的是 Dialog 來繪制底部的布局:

void showShareDialog() {
 Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
 View contentView = LayoutInflater.from(this).inflate(R.layout.bottom_share, null);
 bottomDialog.setContentView(contentView);
 ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
 layoutParams.width = getResources().getDisplayMetrics().widthPixels;
 contentView.setLayoutParams(layoutParams);
 bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
 bottomDialog.setCanceledOnTouchOutside(true);
 bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
 bottomDialog.show();
 }

然后簡單地了解了一下動畫效果:

<style name="BottomDialog">
 <item name="android:windowNoTitle">true</item>
 <item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog">
 <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item>
 <item name="android:windowExitAnimation">@anim/translate_dialog_out</item>
</style>

對應(yīng)的動畫文件:

translate_dialog_in:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="300"
 android:fromXDelta="0"
 android:fromYDelta="100%"
 android:toXDelta="0"
 android:toYDelta="0">
</translate>

translate_dialog_out:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="300"
 android:fromXDelta="0"
 android:fromYDelta="0"
 android:toXDelta="0"
 android:toYDelta="100%">
</translate>

但是繪制的時(shí)候,出現(xiàn)了一些問題,即 Dialog 在最上面,隨后改用 BottomSheetDialog 來繪制。

使用 BottomSheetDialog 繪制分享菜單

對應(yīng)的邏輯變得更加簡單了。

void showShareDialog() {
 final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(DetailActivity.this);
 View dialogView = LayoutInflater.from(InformationDetailActivity.this).inflate(R.layout.bottom_share, null);
 dialogView.findViewById(R.id.cancel_share).setOnClickListener(view -> {
  bottomSheetDialog.dismiss();
 });
 bottomSheetDialog.setContentView(dialogView);
 bottomSheetDialog.show();
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用Android實(shí)現(xiàn)短信、微信、微博分享功能”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)頁題目:如何使用Android實(shí)現(xiàn)短信、微信、微博分享功能
標(biāo)題路徑:http://www.muchs.cn/article48/ijsiep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司品牌網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化