首页 > 解决方案 > 如何处理同一活动的多个意图?

问题描述

好的,我只是想让多个意图将数据传递到一个活动中,该活动会将所有用户输入汇总为一个易于阅读的表格。

这是一个模拟学校申请表。

以下代码可以正常工作,但会产生两项活动:一项显示学生的性别,一项显示学生的姓名。如何让它在同一个活动中同时显示?把我的头发拉出来!!

学生信息活动的 XML:

<android.support.constraint.ConstraintLayout 
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="@android:color/background_light"
tools:context=".studentInfoActivity">

<TextView
    android:id="@+id/textView4"
    android:layout_width="143dp"
    android:layout_height="42dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="90dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:gravity="center"
    android:text="Student Full Name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="143dp"
    android:layout_height="42dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:gravity="center"
    android:text="Gender"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />

reviewScreenActivity 的 XML:

<android.support.constraint.ConstraintLayout 
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"
tools:context=".reviewScreenActivity">

<TextView
    android:id="@+id/textView15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="SUMMARY:"
    android:textSize="30sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/summaryStudentFamilyName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="150dp"
    android:layout_marginRight="150dp"
    android:text="The student's name is: "
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView15" />

<TextView
    android:id="@+id/summaryStudentGender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="68dp"
    android:layout_marginEnd="150dp"
    android:layout_marginRight="150dp"
    android:text="The student's gender is: "
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView15" />

<TextView
    android:id="@+id/studentFullNameReviewScreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="85dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/summaryStudentFamilyName"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/studentGenderReviewScreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/summaryStudentGender"
   app:layout_constraintTop_toBottomOf="@+id/studentFullNameReviewScreen" />
</android.support.constraint.ConstraintLayout>

studentInfoActivity的Java代码:

public class studentInfoActivity extends AppCompatActivity {

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

    //TAKEN FROM https://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-list.
    //AUTHOR: NICOLAS TYLER
    //get the spinner from the xml.
    Spinner dropdown = findViewById(R.id.genderSpinner);
    //create a list of items for the spinner.
    String[] items = new String[]{"Male", "Female", "Other"};
    //create an adapter to describe how the items are displayed, adapters are used in several places in android.
    //There are multiple variations of this, but this is the basic variant.
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
    //set the spinners adapter to the previously created one.
    dropdown.setAdapter(adapter);

    //TAKEN FROM https://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-list.
    //AUTHOR: NICOLAS TYLER
    //get the spinner from the xml.
    Spinner atsiDropdown = findViewById(R.id.atisSpinner);
    //create a list of items for the spinner.
    String[] atsiItems = new String[]{"Yes", "No"};
    //create an adapter to describe how the items are displayed, adapters are used in several places in android.
    //There are multiple variations of this, but this is the basic variant.
    ArrayAdapter<String> atsiAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, atsiItems);
    //set the spinners adapter to the previously created one.
    atsiDropdown.setAdapter(atsiAdapter);
}
public void createCaregiverInfoSplash(View v){
    //Send Student Full Name
    EditText studentFullNameInput = (EditText) findViewById(R.id.studentFullNameInput);
    String studentFullName = studentFullNameInput.getText().toString();
    Intent studentIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
    studentIntent.putExtra("studentFullName", studentFullName);
    startActivity(studentIntent);

    //Send Student Gender
    Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
    String studentGender = genderSpinner.getSelectedItem().toString();
    Intent genderIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
    genderIntent.putExtra("studentGender", studentGender);
    startActivity(genderIntent);
}

}

reviewScreenActivity的Java代码:

public class reviewScreenActivity extends AppCompatActivity {

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

    //Student Full Name reciever
    Bundle sfnBundle = getIntent().getExtras();
    String studentFullName = sfnBundle.getString("studentFullName");
    if (studentFullName != null) {
        TextView sfn = (TextView) findViewById(R.id.studentFullNameReviewScreen);
        sfn.setText(studentFullName);
    }
    else {
        TextView sfn = (TextView) findViewById(R.id.studentFullNameReviewScreen);
        sfn.setText("Not showing right!");
    }


    //Student Gender Reciever
    Bundle sgBundle = getIntent().getExtras();
    String studentGender = sgBundle.getString("studentGender");
    TextView sg = (TextView)findViewById(R.id.studentGenderReviewScreen);
    sg.setText(studentGender);
}

}

标签: javaandroidandroid-intent

解决方案


好吧,您有很多错误,例如不正确的类和方法名称。

您可以在一个意图中传递任意数量的额外信息。

public void createCaregiverInfoSplash(View v){
    //Send Student Full Name
    EditText studentFullNameInput = (EditText) findViewById(R.id.studentFullNameInput);
    String studentFullName = studentFullNameInput.getText().toString();

    Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
    String studentGender = genderSpinner.getSelectedItem().toString();

    Intent reviewIntent = new Intent(studentInfoActivity.this, reviewScreenActivity.class);
    reviewIntent.putExtra("studentFullName", studentFullName);
    reviewIntent.putExtra("studentGender", studentGender);
    startActivity(studentIntent);
}

推荐阅读