簡體   English   中英

如何從 xml 中的 TextView 獲取文本並使用共享按鈕共享?

[英]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。

這將完成你的工作。

我會將第三個TextView更改為實際按鈕。 但是,如果你不想這樣做,你可以setOnClickListener第三TextView的和onClick()調用getText.toString()的第一和第二textviews。 並將它們連接起來。 最后發起分享意向,分享到不同的社交媒體。 是做到一點的解決方案。 使用第一個解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM