如何分享随机查看文字?

时间:2014-06-14 06:21:45

标签: java android sdk adt share-button

我希望用户能够分享从一组文本中随机查看文本的内容,但这看起来非常复杂。我试图寻找答案,但每个人都在谈论分享图像。

这是我的Java代码:

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

            }
        });


    }

}

这是我的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" />

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您只需创建一个ACTION_SEND意图并将您的文字作为EXTRA_TEXT附加内容。

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

请在Android文档中查看Sending Text Content