简体   繁体   中英

Passing multiple editText field data to another activity

I want if the user enters data in any one field then it's value passes to other activity and other edittext fields don't pass their values. But when I can add all editText string values l, intent sends all values. I want only those values which have data and other values won't be sent. I can pass data with Intent.createchooser

Xml:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:id="@+id/student_name_sixClass_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:hint="Student name"
                android:paddingLeft="10dp"
                android:textSize="20sp" />


            <EditText
                android:id="@+id/english_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="English"
                android:paddingLeft="10dp"
                android:textSize="20sp" />


            <EditText
                android:id="@+id/math_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="Math"
                android:paddingLeft="10dp"
                android:textSize="20sp" />


            <EditText
                android:id="@+id/sciece_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="Science"
                android:paddingLeft="10dp"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/urdu_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="Urdu"
                android:paddingLeft="10dp"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/islamiyat_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="Islamiyat"
                android:paddingLeft="10dp"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/ps_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="P.S"
                android:paddingLeft="10dp"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/history_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="History"
                android:paddingLeft="10dp"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/computer_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="Computer"
                android:paddingLeft="10dp"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/geography_six_sub_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="@android:color/white"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="Geography"
                android:paddingStart="10dp"
                android:textSize="20sp" />

            <RadioGroup
                android:id="@+id/sixClass_radio_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <RadioButton
                    android:id="@+id/monthly_test_report_radioBtn_sixClass"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:text="Monthly Test Report"
                    android:textSize="20sp" />

                <RadioButton
                    android:id="@+id/weekly_test_report_radioBtn_sixClass"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:text="Weekly Test Report"
                    android:textSize="20sp" />

            </RadioGroup>

            <Button
                android:id="@+id/sendBtn_six"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="30dp"
                android:layout_marginBottom="10dp"
                android:background="@color/btn_color"
                android:text="Send"
                android:textColor="@android:color/white"
                android:textSize="20sp" />


        </LinearLayout>
    </ScrollView>

Java code:

 sendBtn_six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isValid()){
            

       Intent sharingIntent = new Intent();
                sharingIntent.setAction(Intent.ACTION_SEND);
                sharingIntent.putExtra(Intent.EXTRA_TEXT, "ANMOL ACEDMY OF SCIENCE\n" + "Asslam_O_Alaikum!" + "\n" + "Repert: " + slct_monthly_reprottext + "\n" + "Field: " + slct_biology_field_nineScience + "\n" + "On Dated: " + current_date+"\n" + "Student Name: " + student_name_nineScience
                        + "\n" + "English: " + engilish_nine_science_sub + "\n" + "Math: " + math_nine_science_sub + "\n" + "Urdu: " + urdu_nine_science_sub
                        + "\n" + "Islamiyat: " + islamiyat_nine_science_sub + "\n" + "P.S: " + ps_nine_science_sub + "\n" + "Chemistry: " + chemistry_nine_science_sub
                        + "\n" + "computer: " + computer_nine_science_sub + "\n" + "Physics: " + physics_nine_science_sub
                        + "\n" + "Regards: Anmol Acedmy of Science");
//
                sharingIntent.setType("text/plain");
                Intent sendData = Intent.createChooser(sharingIntent, null);
                startActivity(sendData);
                }
            }
        });

I want if user wants to add data in some fields then clicks the button, the data can pass to other application

Parse all your EditTexts and get their values by calling yourEditTextView.getText() .

I assume you bind your views first:

EditText mathSix = findViewById(R.id.math_six_sub_editText);

String mathSixSub = mathSix.getText();

This method returns the string of the EditText or null if the EditText is empty. Then add it to the intent you've created accordingly, or if you want to send the data to an activity within your app use a simple Intent:

Intent intent = new Intent(yourCurrentActivity.this, theActivityThatReceivesTheData.class);
intent.putExtra(KEY, "Math Six: " + mathSixSub);
startActivity(intent);

If you want to. add an empty space where the values are null, you can add the following nullity check instead:

String mathSixSub = mathSix.getText() == null ? " " : mathSix.getText();

this operator checks if the EditText is null and adds a space to where the text would have been, otherwise it adds the text that's in the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM