我需要分享一个随机生成的短语

时间:2016-05-02 23:19:20

标签: java android android-studio mobile share

我需要一个字符串来分享我的代码,下面随机生成一个句子,不能共享,复制和粘贴所选短语。对不起我的英文

public class MainActivity extends ActionBarActivity {


    private TextView textViewNewId;
    private Button buttonNewId;

    private String[] frases = {
        "First phrase", "second phrase", "third phrase"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textViewNewId = (TextView) findViewById(R.id.textViewNewId);
        buttonNewId = (Button) findViewById(R.id.buttonNewId);

        buttonNewId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Random randomico = new Random();
                int numeroAleatorio = randomico.nextInt( frases.length );

                textViewNewId.setText( frases[ numeroAleatorio ]);
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vivew) {

                fab();
                // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                 //     .setAction("Action", null).show();
            }
        });
    }

    private void fab() { //sharing implementation here
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "AndroidSolved");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Now Learn Android with AndroidSolved clicke here to visit ");
        startActivity(Intent.createChooser(sharingIntent, "Share via"));


    }

result of my code

1 个答案:

答案 0 :(得分:0)

您可以将所选文本传递给您的意图并分享。我做了以下事情:

public static void shareOnSocials(Context context , String shareThings) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, shareThings);
        context.startActivity(Intent.createChooser(intent, "HOOORA"));
    }