[英]How to share picture or text from fragment to Facebook using android studio?
[英]How to take text from TextView in xml and share using a share button?
之前似乎有人問過這個問題,但我沒有找到我正在尋找的答案。 我在.xml file
有兩個 TextViews,第一個 TextView 用於標題,第二個用於 Url。 這兩個 TextView 從實時 firebase 數據庫中獲取數據,因此 Title 和 Url 每次都不同。 在 TextViews 下面我有一個共享按鈕。 單擊此按鈕時,我希望它從兩個 textViews 獲取數據並通過 Gmail、facebook 等不同的社交網絡進行共享。
我的.xml file
如下所示:
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_marginTop="15dp"
android:text="Title"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/url"
android:layout_marginTop="15dp"
android:text="url"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_share"
android:layout_marginTop="15dp"
android:text="Share"/>
因此,每次 Url 和 Title 更改時,Share 按鈕都應該能夠獲取 TextViews 中當前存在的數據並通過不同平台共享
創建一個Intent
以與該Intent
共享文本和startActivity
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = textViewTitle.getText().toString();
String uriStr = textViewUri.getText().toString();
String shareString = title + ", " + uriStr;
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareString);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
查看此鏈接了解更多信息。
所以你應該在你的按鈕的 OnClick 里面做這個操作,即
public void onClick(View view)
{
String Title = textViewTitle.getText().toString().trim();
String Url = textViewUrl.getText().toString().trim();
\\ Share Button Logic Goes Here
}
並記得添加 -
btnShare.setOnClickListener(this);
和類應該實現 View.OnClickListener。
這將完成你的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.