简体   繁体   中英

How to share a randomly viewing text?

I want the user to be able to share what is randomly viewing text from a set of texts but it seems very complicated. I tried to search for an answer but everyone talks about sharing images.

Here's my Java code:

public class Second extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        final TextView generatedtxt = (TextView) findViewById(R.id.generatedtxt);
        Typeface custom_font = Typeface.createFromAsset(getAssets (), "fonts/1.ttf");

        generatedtxt.setTypeface(custom_font);

        Button generatebtn = (Button) findViewById(R.id.generatebtn);

        final String[] gn = {"text1", "text2", "text3", "text4", "text5"};

        generatebtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                int rando = (int) (Math.random() *5);
                generatedtxt.setText(gn[rando]);

            }
        });


    }

}

and this is my XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="@drawable/bg2"
android:paddingTop="@dimen/activity_vertical_margin" >

<Button
    android:id="@+id/generatebtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/sharebtn"
    android:text="@string/generatebtn" />

<Button
    android:id="@+id/sharebtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/sharebtn" />

<TextView
    android:id="@+id/generatedtxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/generatebtn"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="@string/gt"
    android:textSize="40sp" />

Can someone help me please?

You just need to create an ACTION_SEND intent and put your text as the EXTRA_TEXT extra.

int rando = (int) (Math.random() *5);
String text = gn[rando];

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
startActivity(sendIntent);

Please check Sending Text Content in the Android documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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