【问题标题】:How can Pass data from first second third activity to fourth activity in android studio如何在android studio中将数据从第一个第二个第三个活动传递到第四个活动
【发布时间】:2020-01-13 05:32:09
【问题描述】:

`

MainActivity
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">


        <TextView
            android:layout_marginTop="30dp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:id="@+id/textView"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="What is your name?" />

        <EditText
            android:id="@+id/name"
            android:layout_width="250dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn1"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="Next"
            android:layout_marginTop="200dp"
            android:layout_gravity="center_horizontal"/>


<Button
            android:id="@+id/btnSummary"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="Open Summary"
            android:layout_marginTop="200dp"
            android:layout_gravity="center_horizontal"/>

    </LinearLayout>
   // SecondActitvity
   

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#8E44AD"
        android:orientation="vertical"
        tools:context=".SecondActivity">

        <TextView
            android:layout_marginTop="30dp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:id="@+id/textView"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Who is the best cricketer in the world?" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="80dp">


            <RadioGroup
                android:id="@+id/options"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >


            <RadioButton
            android:id="@+id/jacques"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Jacques Kallis"
            android:textSize="22sp"/>

        <RadioButton
            android:id="@+id/adnan"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Adnan"
            android:textSize="22sp"/>


        <RadioButton
            android:id="@+id/sachine"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sachine Tendulkar"
            android:textSize="22sp"/>

        <RadioButton
            android:id="@+id/virat"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Virat"
            android:textSize="22sp"/>
            </RadioGroup>
        </LinearLayout>
        <Button
            android:id="@+id/btn2"
            android:layout_width="250dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:background="#ECF0F1"
            android:text="Next"
            />
    </LinearLayout>
//ThirdActivity
   

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical"
        android:background="#7845"
        tools:context=".ThirdActivity">
        <TextView
            android:layout_marginTop="30dp"
            android:textStyle="bold"
            android:id="@+id/textView"
            android:textSize="22sp"
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="What are the colors in indian national flag?" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:layout_marginTop="10dp"
            android:text="Options:"
            android:textStyle="bold"
            android:textSize="22sp"
            android:paddingLeft="10dp"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

        <CheckBox
            android:id="@+id/checkBox"
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:textColor="#fff"
            android:textSize="22sp"
            android:text="White" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:textSize="22sp"
            android:text="Orage" />

        <CheckBox
            android:textSize="22sp"
            android:padding="5dp"
            android:id="@+id/checkBox3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:text="Green" />

        <CheckBox
            android:textSize="22sp"
            android:padding="5dp"
            android:id="@+id/checkBox4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:text="Yellow" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="250dp"
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="150dp"
                android:background="#ECF0F1"
                android:text="Next"
                />

    </LinearLayout>
    </LinearLayout>
//SummaryActivity
   

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textMsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textSize="22sp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="20dp"/>
    </LinearLayout>
//MainActivity.java


public class MainActivity extends AppCompatActivity {

Button button;
TextView tv;
EditText editText;
Context context;
Button buttonSummary;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv=findViewById(R.id.textMsg);
        editText = findViewById(R.id.name);
        button = findViewById(R.id.btn1);
        buttonSummary = findViewById(R.id.btnSummary);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), SecondActivity.class));
            }
        });

        buttonSummary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openSummaryActivity();
            }
        });
    }
    public void openSummaryActivity() {
        String text = editText.getText().toString();
        Intent intent = new Intent(getApplicationContext(), SummaryActivity.class);
        intent.putExtra("KEY", text);
        startActivity(intent);
    }}

SummaryActivity.java

public class SummaryActivity extends AppCompatActivity {


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

        Intent intent = getIntent();
        String text = intent.getStringExtra("KEY");
        TextView textView = (TextView) findViewById(R.id.textMsg);
        textView.setText(text);

    }
}

`我有 4 个活动。第一个带有输入文本的活动。

选择最佳板球运动员的第二个活动,使用单选按钮。

使用复选框的第三个活动选择选项。

在第四个活动中,我需要显示活动 1 2 3 在第四个活动中选择的数据的所有内容。

总结

你好“姓名:”,

以下是选择的答案:

谁是世界上最好的板球运动员?

答案:“”

国旗的颜色是什么?

答案:“Commo 分开”

【问题讨论】:

标签: android android-studio android-intent android-activity sharedpreferences


【解决方案1】:

你应该像这样使用 Singletone 类,

public class InfoSingleton {
private static final InfoSingleton ourInstance = new InfoSingleton();
private String name;
private String bestCricketer;

public static InfoSingleton getInstance() {
    return ourInstance;
}

private InfoSingleton() {
}

public static InfoSingleton getOurInstance() {
    return ourInstance;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getBestCricketer() {
    return bestCricketer;
}

public void setBestCricketer(String bestCricketer) {
    this.bestCricketer = bestCricketer;
}
}

【讨论】:

    【解决方案2】:

    您可以使用意图将数据从一个活动传递到另一个活动。

    将数据从 Activity1 传递到 Activity2

    public class Activity1 extends Activity {
    
    // write this code on button click or event that moving from Activity1 to Activity2
    Intent intent = new Intent(Activity1.this, Activity2.class);
    intent.putExtra("ans1", ans1Value);
    startActivity(intent);
    
    }
    

    访问Activity2中的数据并传递给Activity3

        public class Activity2 extends Activity {
         @Override
            protected void onCreate(Bundle savedInstanceState) {
        String ans1Value = getIntent().getStringExtra("ans1"); //Answer from Activity1 
        }
    
    // write this code on button click or event that moving from Activity2 to Activity3
    
     Intent intent = new Intent(Activity2.this, Activity3.class);
        intent.putExtra("ans1", ans1Value);
        intent.putExtra("ans2", ans2Value);
        startActivity(intent);
    
        }
    

    访问Activity3中的数据并传递给Activity4

    public class Activity3 extends Activity {
             @Override
                protected void onCreate(Bundle savedInstanceState) {
            String ans1Value = getIntent().getStringExtra("ans1");
            String ans2Value = getIntent().getStringExtra("ans2");  //Answer from Activity2 
            }
    
        // write this code on button click or event that moving from Activity3 to Activity4
    
         Intent intent = new Intent(Activity3.this, Activity4.class);
            intent.putExtra("ans1", ans1Value);
            intent.putExtra("ans2", ans2Value);
            intent.putExtra("ans3", ans3Value); //Answer from Activity3 
            startActivity(intent);
    
            }
    

    在第四个活动中显示数据

    public class Activity4 extends Activity {
                 @Override
                    protected void onCreate(Bundle savedInstanceState) {
                String ans1Value = getIntent().getStringExtra("ans1");
                String ans2Value = getIntent().getStringExtra("ans2");
                String ans3Value = getIntent().getStringExtra("ans3");
                //Show this information on Fourth Activity4
                }
    }
    

    【讨论】:

    • 我已经更新了我的代码。仍然无法正常工作。我希望将文本传递给摘要活动。
    • 请问您遇到什么问题
    • @jabeenakkalkot 我已经编辑了您的代码,现在当您单击 MainActivity 中的摘要按钮时,它将从 EditText 获取文本并显示在 SummaryActivity 中。这只是为了让您的代码开始工作。您的代码无法正常工作的原因是您在 EditText 上设置了单击侦听器,当您单击 EditText 时,它会移动到带有空字符串的 SummaryActivity,而 SummaryActivity 没有任何显示。
    • 首先我在 MainActivity 中没有摘要按钮。在 MainActivity 我有一个 textview 有问题你的名字和编辑文本从哪里我需要将我的文本传递给最后一个活动,即摘要活动和下一个按钮移动到第二个活动。在 MainActivity 我添加了我的代码:Intent intent = new Intent(getApplicationContext(), SecondActivity.class);字符串文本=editText.getText().toString(); intent.putExtra("KEY", text);开始活动(意图);这是我的摘要活动代码: Intent intent = getIntent(); String text = intent.getStringExtra("KEY");textView.setText(text);
    • 是的,我知道这只是例如,在您之前的代码中,您将点击侦听器放在 EditText 上,这是错误的,您应该使用按钮或使用键盘事件来处理它。
    【解决方案3】:

    您可以使用 Intents 在活动之间传递数据

    Intent intent = new Intent(Activitya.this, Activityb.class);
            intent.putExtra("NAME", "your value");
            startActivity(intent);
    

    下一个活动

    String name= getIntent().getStringExtra("NAME");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多