首页 > 解决方案 > 如何从 xml 中的 TextView 获取文本并使用共享按钮进行共享?

问题描述

似乎之前有人问过这个问题,但我没有找到我正在寻找的答案。我在.xml file第一个 TextView 中有两个 TextView 用于标题,第二个用于 Url。这两个 TextViews 从实时 firebase 数据库中获取数据,因此 Title 和 Url 每次都不同。在 TextViews 下面我有一个分享按钮。单击此按钮时,我希望它从两个 textView 中获取数据并通过 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 中当前存在的数据并通过不同的平台共享

标签: javaandroid

解决方案


创建一个Intent用于共享文本并startActivity与之共享Intent

    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);

        }
    });

检查此链接了解更多信息。


推荐阅读