将多个编辑框字段数据传递到另一个活动

huangapple go评论67阅读模式
英文:

Passing multiple editText field data to another activity

问题

我想要的是,如果用户在任何一个字段中输入数据,那么它的值将传递到其他活动,而其他编辑文本字段不会传递它们的值。但是,当我可以添加所有EditText字符串值时,意图发送所有值。我只想传递那些有数据的值,其他值不会被发送。我可以使用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="学生姓名"
            android:paddingLeft="10dp"
            android:textSize="20sp" />

        <!-- 其他 EditText 字段 -->

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

            <!-- 无线电按钮 -->

        </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="发送"
            android:textColor="@android:color/white"
            android:textSize="20sp" />

    </LinearLayout>
</ScrollView>

Java 代码:

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 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:

&lt;ScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;EditText
android:id=&quot;@+id/student_name_sixClass_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:hint=&quot;Student name&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/english_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;English&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/math_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;Math&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/sciece_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;Science&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/urdu_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;Urdu&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/islamiyat_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;Islamiyat&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/ps_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;P.S&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/history_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;History&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/computer_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;Computer&quot;
android:paddingLeft=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/geography_six_sub_editText&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:backgroundTint=&quot;@android:color/white&quot;
android:drawableRight=&quot;@drawable/ic_arrow_down&quot;
android:hint=&quot;Geography&quot;
android:paddingStart=&quot;10dp&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;RadioGroup
android:id=&quot;@+id/sixClass_radio_group&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;RadioButton
android:id=&quot;@+id/monthly_test_report_radioBtn_sixClass&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;10dp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:text=&quot;Monthly Test Report&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;RadioButton
android:id=&quot;@+id/weekly_test_report_radioBtn_sixClass&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;10dp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:text=&quot;Weekly Test Report&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;/RadioGroup&gt;
&lt;Button
android:id=&quot;@+id/sendBtn_six&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:layout_marginRight=&quot;30dp&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:background=&quot;@color/btn_color&quot;
android:text=&quot;Send&quot;
android:textColor=&quot;@android:color/white&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;

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, &quot;ANMOL ACEDMY OF SCIENCE\n&quot; + &quot;Asslam_O_Alaikum!&quot; + &quot;\n&quot; + &quot;Repert: &quot; + slct_monthly_reprottext + &quot;\n&quot; + &quot;Field: &quot; + slct_biology_field_nineScience + &quot;\n&quot; + &quot;On Dated: &quot; + current_date+&quot;\n&quot; + &quot;Student Name: &quot; + student_name_nineScience
+ &quot;\n&quot; + &quot;English: &quot; + engilish_nine_science_sub + &quot;\n&quot; + &quot;Math: &quot; + math_nine_science_sub + &quot;\n&quot; + &quot;Urdu: &quot; + urdu_nine_science_sub
+ &quot;\n&quot; + &quot;Islamiyat: &quot; + islamiyat_nine_science_sub + &quot;\n&quot; + &quot;P.S: &quot; + ps_nine_science_sub + &quot;\n&quot; + &quot;Chemistry: &quot; + chemistry_nine_science_sub
+ &quot;\n&quot; + &quot;computer: &quot; + computer_nine_science_sub + &quot;\n&quot; + &quot;Physics: &quot; + physics_nine_science_sub
+ &quot;\n&quot; + &quot;Regards: Anmol Acedmy of Science&quot;);
//
sharingIntent.setType(&quot;text/plain&quot;);
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

答案1

得分: 1

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.

英文:

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, &quot;Math Six: &quot; + 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 ? &quot; &quot; : 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.

huangapple
  • 本文由 发表于 2020年9月11日 14:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定