英文:
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:
<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
答案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, "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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论