java - 如何从 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 中当前存在的数据并通过不同的平台共享
解决方案
创建一个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);
}
});
检查此链接了解更多信息。
推荐阅读
- javascript - 在饼图 Highcharts 中显示数据表边框
- python - Gekko 优化
- amazon-s3 - 为 Amazon S3 存储桶键创建索引
- https - A帧的未来,https?
- python - 调试强化学习模型 (MsPacman)
- html - 单击时将标签内的字符串插入其他代码行
- javascript - Google Web 应用程序 - 从默认 html 文件生成另一个 html 并将值从默认 html 传递到新 html
- javascript - 在 node.js 上向 azure 函数添加中间件函数
- ruby-on-rails - 具有基本身份验证的 Webhook 控制器的 RSpec 测试
- c++ - push_back 通过自创对象获取最后一个元素的向量