简体   繁体   English

如何从 xml 中的 TextView 获取文本并使用共享按钮共享?

[英]How to take text from TextView in xml and share using a share button?

It seems as this question was asked before but I did not find the answer that I am looking for.之前似乎有人问过这个问题,但我没有找到我正在寻找的答案。 I have two TextViews in .xml file first TextView is for Title and the second one is for Url.我在.xml file有两个 TextViews,第一个 TextView 用于标题,第二个用于 Url。 These two TextViews take data from realtime firebase database so the Title and the Url varies every time.这两个 TextView 从实时 firebase 数据库中获取数据,因此 Title 和 Url 每次都不同。 Below the TextViews I have a share Button.在 TextViews 下面我有一个共享按钮。 When this button is clicked I want it to take data from two textViews and share via different socials like Gmail, facebook and others.单击此按钮时,我希望它从两个 textViews 获取数据并通过 Gmail、facebook 等不同的社交网络进行共享。

My .xml file looks like:我的.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"/>

So every time Url and Title changes and the Share button should be able to take the data that is currently there in the TextViews and share via different platforms因此,每次 Url 和 Title 更改时,Share 按钮都应该能够获取 TextViews 中当前存在的数据并通过不同平台共享

Create a Intent for sharing text and startActivity with that Intent创建一个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);

        }
    });

Check this link for more.查看此链接了解更多信息。

So you should do this operation inside OnClick of your button ie所以你应该在你的按钮的 OnClick 里面做这个操作,即

     public void onClick(View view)
     {
       String Title = textViewTitle.getText().toString().trim();
       String Url = textViewUrl.getText().toString().trim();

       \\ Share Button Logic Goes Here
     }

and remember to add -并记得添加 -

   btnShare.setOnClickListener(this);

and class should implement View.OnClickListener.和类应该实现 View.OnClickListener。

This will do your job.这将完成你的工作。

I would change the third TextView to an actual button.我会将第三个TextView更改为实际按钮。 But if you do not want to do that, you can setOnClickListener on the third TextView and onClick() call getText.toString() on the first and second textviews.但是,如果你不想这样做,你可以setOnClickListener第三TextView的和onClick()调用getText.toString()的第一和第二textviews。 and concatenate them.并将它们连接起来。 Finally launch a share intent to share to different social media.最后发起分享意向,分享到不同的社交媒体。 Here is the solution to do just that. 是做到一点的解决方案。 Use first solution.使用第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM