如何将文本视图数据从第一活动发送到第三活动

时间:2019-03-30 15:15:39

标签: java android android-activity

我正在开发一个项目,其中有3个活动。每个活动都有其自己的按钮,单击该按钮即可开始下一个活动。第一个活动有一个显示随机字符串的文本视图。当我在第一活动中按下按钮时,第二活动将开始。当我在第二活动中按下按钮时,第三活动开始。但是我想知道如何将随机字符串从第一活动发送到第三活动。

第一个活动:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class SelectRandomNumber extends AppCompatActivity {


    private Button generateStringBtn;
    private TextView randomOne;
    private TextView randomTwo;
    private TextView randomThree;


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

        generateStringBtn = (Button) findViewById(R.id.generateRandomBtn);
        randomOne = (TextView) findViewById(R.id.randomStringOne);
        randomTwo = (TextView) findViewById(R.id.randomStringTwo);
        randomThree = (TextView) findViewById(R.id.randomStringThree);

        generateStringBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                randomOne.setText(randomString(173));
                randomTwo.setText(randomString(173));
                randomThree.setText(randomString(173));

            }
        });

        randomOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendID_one();
            }
        });



    }

    public String randomString(int length){
        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for(int i = 0; i < length; i++){
            char c = chars[random.nextInt(chars.length)];
            stringBuilder.append(c);
        }
        return stringBuilder.toString();
    }

    public void sendID_one(){
        String message = randomOne.getText().toString();
        Intent check = new Intent(SelectRandomNumber.this, CheckCandidateID.class);
        check.putExtra("Extra_Message",message);
        startActivity(check);

    }
    public void sendID_Two(){
        String message = randomTwo.getText().toString();
        Intent check = new Intent(SelectRandomNumber.this, CheckCandidateID.class);
        check.putExtra("Extra_Message",message);
        startActivity(check);

    }
    public void sendID_Three(){
        String message = randomThree.getText().toString();
        Intent check = new Intent(SelectRandomNumber.this, CheckCandidateID.class);
        check.putExtra("Extra_Message",message);
        startActivity(check);
    }

    public void send(){
        Intent check = new Intent(SelectRandomNumber.this, Try.class);  //for sending data to third activity
        check.putExtra("Extra_Message_Send",randomOne.getText().toString());
        startActivity(check);
    }

}

第三项活动:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Try extends AppCompatActivity {

    private TextView tv;

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

        tv = (TextView) findViewById(R.id.test);

        String s = getIntent().getStringExtra("Extra_Message_Send");
        tv.setText(s);



    }
}

2 个答案:

答案 0 :(得分:1)

使用意图在活动之间传递数据。在您的第一次活动中,

Intent mIntent = new Intent(FirstActivity.this, ThirdActivity.class);
mIntent.putExtra("randomString", tv.getText().toString());

在第三项活动中,

String s= getIntent().getStringExtra("randomString");

答案 1 :(得分:1)

如果要从第二活动开始第三活动,则必须先在第二活动中发送字符串,然后在第三活动中发送。最好在第二个和第三个活动中发送所有三个字符串,这样您就可以来回往复:

String message1 = randomOne.getText().toString();
String message2 = randomTwo.getText().toString();
String message3 = randomThree.getText().toString();

check.putExtra("Extra_Message1",message1);
check.putExtra("Extra_Message2",message2);
check.putExtra("Extra_Message3",message3);

然后,您还必须将这些数据从第二活动传递到第三活动。