簡體   English   中英

在Android Studio 3.4中如何將字符串值從一個活動發送到另一個活動中時如何獲取所有值

[英]How Do i Get all the Values When Sending String Values from one Activity to Another in Android Studio 3.4

請設計一個Android應用程序,我想將(5)字符串值從一個活動發送到另一個活動以在不同的TextView中使用,我已經嘗試了幾乎可以在該主題上在線找到的所有代碼,但我一直只獲得一個值(我在putExtra()中發送的最后一個值)。 請我是Android Studio的新手,將不勝感激。

我已經使用putExtra()將一個數據發送到另一活動,並且它工作得很好,同時嘗試對多個數據執行相同的操作,但我一直只收到發送的數據之一。 我也嘗試過使用bundle對象,以從其他(接收)活動中接收數據。

我希望得到所有這些數據(intent.putExtra(“ surname”,“ Jerry”)。
intent.putExtra(“ middlename”,“ chris”)。 intent.putExtra(“ lastname”,“ Enema”))在另一項活動中,但我一直獨自獲得“ Enema”

這是我的代碼; //在firstActivity中

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

                String sFirstname = firstname.getText().toString();
                String sLastname = lastname.getText().toString();

                Intent intent = new Intent(MainActivity.this, ReceiveActivity.class);

                intent.putExtra("surname" ,sFirstname);
                intent.putExtra("lastname", sLastname);

                startActivity(intent);

            }
        });

//And In the second Activity

名字= findViewById(R.id.firstname); 姓= findViewById(R.id.firstname);

Intent intent = getIntent();
Bundle bundle = getIntent().getExtras();

String ssurname = bundle.getString("surname");
String slastname = bundle.getString("lastname");

firstname.setText(ssurname);
lastname.setText(slastname);

我不想使用ue getExtras所以您應該有兩個getExtra像這樣:

 Intent intent = getIntent();

 String ssurname = intent.getExtra("surname");
 String slastname = intent.getExtra("lastname");

 firstname.setText(ssurname);
 lastname.setText(slastname);

嘗試這個。 在第一個活動中:

String sFirstname = "Tope";
String sLastname = "Adebodun";

Intent theIntent = new Intent(MainActivity.this, ReceivingActivity.class);
theIntent.putExtra("firstname", sFirstname);
theIntent.putExtra("lastname", sLastname);

然后在第二個活動中,在onCreate方法中執行以下操作:

Intent intent = getIntent();
String thefirst = (String) intent.getExtras.getString("firstname");
String thelast = (String) intent.getExtras.getString("lastname");

暫無
暫無

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

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