java - 如何处理同一 Activity 的多个 Intent ?

标签 java android android-intent

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

这是模拟学校申请表。

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

studentInfoActivity 的 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);
}

最佳答案

嗯,你有很多错误,比如不正确的类名和方法名。

您可以在一个 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);
}

关于java - 如何处理同一 Activity 的多个 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55513968/

相关文章:

android - 如何拦截蓝牙耳机的通话按钮?

android - 重新加载 Activity 后 ProgressBar 看起来很奇怪

c# - 寻找一个项目的想法

android - 用户从状态栏中删除所有通知,如何获取事件

当连接到没有互联网的 SSID 时,android Q/10 无法解决主机 "URL"问题

java - 如何添加 onClick 到此?

Java Android 关闭相机

Java list 属性缺失

作为对象/类的 Java 图形

Android:选择用于打开链接的应用程序