简体   繁体   English

将多个 editText 字段数据传递给另一个活动

[英]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.我希望如果用户在任何一个字段中输入数据,那么它的值会传递给其他活动,而其他 edittext 字段不会传递它们的值。 But when I can add all editText string values l, intent sends all values.但是当我可以添加所有 editText 字符串值时,意图会发送所有值。 I want only those values which have data and other values won't be sent.我只想要那些有数据的值,其他值不会被发送。 I can pass data with Intent.createchooser我可以用Intent.createchooser传递数据

Xml: 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() .解析所有 EditText 并通过调用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.如果 EditText 为空,则此方法返回 EditText 的字符串或null 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.此运算符检查EditText是否为 null 并在文本所在的位置添加一个空格,否则它会添加视图中的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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