单条数据传值(MainActivity页面的代码):
Intent intent = new Intent(MainActivity.this,GetDataActivity.class); intent.putExtra("name","张三"); startActivity(intent);
GetDataActivity页面的代码:
Intent intent = getIntent(); String name = intent.getStringExtra("name"); //name就是上一页传过来的值
多条数据传值(MainActivity页面的代码):
ArrayList<String> data = new ArrayList<>(); data.add("张三"); data.add("李四"); data.add("王五"); intent.putStringArrayListExtra("nameList",data); startActivity(intent);
GetDataActivity页面的代码:
Intent intent = getIntent(); ArrayList nameList = intent.getCharSequenceArrayListExtra("nameList"); String name1 = nameList.get(0); String name2 = nameList.get(1); String name3 = nameList.get(2);
单条数据取值(MainActivity代码):
Intent intent = new Intent(this,GetDataActivity.class); startActivityForResult(intent,0x11); @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 0x11){ if(resultCode == 1){ String lessData = data.getStringExtra("lessData"); } } }
GetDataActivity页面的代码:
Intent intent = getIntent(); String title = "头部“; intent.putExtra("lessData",title); setResult(1,intent); finish();
多条数据传值(MainActivity页面的代码):
Intent intent = new Intent(this,GetDataActivity.class); startActivityForResult(intent,0x12); @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 0x12){ if(resultCode == 2){ ArrayList mData = data.getCharSequenceArrayListExtra("manyData"); } } }
GetDataActivity页面的代码:
ArrayList<String> manyData = new ArrayList<>(); manyData.add("张一"); manyData.add("张二"); manyData.add("张三"); manyData.add("张四"); intent.putStringArrayListExtra("manyData",manyData); setResult(2,intent); finish();
package com.example.testproject; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_less,btn_many; private TextView tv_title,tv_name,tv_sex,tv_age; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ btn_less = (Button)findViewById(R.id.btn_Less); btn_many = (Button)findViewById(R.id.btn_Many); tv_title = (TextView)findViewById(R.id.text_title); tv_name = (TextView)findViewById(R.id.text_name); tv_sex = (TextView)findViewById(R.id.text_sex); tv_age = (TextView)findViewById(R.id.text_age); btn_less.setOnClickListener(this); btn_many.setOnClickListener(this); } @Override public void onClick(View view) { ArrayList<String> data = new ArrayList<>(); data.add("张三"); data.add("李四"); data.add("王五"); Intent intent = new Intent(this,GetDataActivity.class); switch (view.getId()){ case R.id.btn_Less: intent.putExtra("data","Less"); // intent.putStringArrayListExtra("nameList",data); startActivityForResult(intent,0x11); break; case R.id.btn_Many: intent.putExtra("data","Mang"); startActivityForResult(intent,0x12); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); cleanText(); switch (requestCode){ case 0x11: String lessData = data.getStringExtra("lessData"); switch (resultCode){ case 1: tv_title.setText(lessData); Toast.makeText(this, "1", Toast.LENGTH_SHORT).show(); break; case 2: tv_name.setText(lessData); Toast.makeText(this, "2", Toast.LENGTH_SHORT).show(); break; case 3: tv_age.setText(lessData); break; case 4: tv_sex.setText(lessData); break; default:break; } break; case 0x12: if(resultCode == 123){ ArrayList mData = data.getCharSequenceArrayListExtra("manyData"); tv_title.setText(mData.get(0).toString()); tv_name.setText(mData.get(1).toString()); tv_age.setText(mData.get(2).toString()); tv_sex.setText(mData.get(3).toString()); } break; } } private void cleanText(){ tv_title.setText(""); tv_name.setText(""); tv_age.setText(""); tv_sex.setText(""); } }
MainActivity的布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:paddingTop="10dp" android:paddingBottom="10dp" android:paddingRight="10dp" android:paddingLeft="10dp" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:gravity="center_vertical" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题:"/> <TextView android:id="@+id/text_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="标题"/> </LinearLayout> <LinearLayout android:gravity="center_vertical" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"> <TextView android:text="姓名:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="姓名"/> </LinearLayout> <LinearLayout android:gravity="center_vertical" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:"/> <TextView android:id="@+id/text_sex" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="性别"/> </LinearLayout> <LinearLayout android:gravity="center_vertical" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年龄:"/> <TextView android:id="@+id/text_age" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="年龄"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"> <Button android:id="@+id/btn_Many" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="多条数据"/> <Button android:id="@+id/btn_Less" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="单条数据"/> </LinearLayout> </LinearLayout>
GetDataActivity页面的代码:
package com.example.testproject; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; public class GetDataActivity extends AppCompatActivity implements View.OnClickListener{ private EditText edt_title,edt_name,edt_sex,edt_age; private Button btn_M,btn_L; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_data); initView(); } private void initView(){ edt_title = (EditText)findViewById(R.id.edt_title); edt_name = (EditText)findViewById(R.id.edt_name); edt_sex = (EditText)findViewById(R.id.edt_sex); edt_age = (EditText)findViewById(R.id.edt_age); btn_L = (Button)findViewById(R.id.btn_Less_data); btn_M = (Button)findViewById(R.id.btn_Many_data); btn_L.setOnClickListener(this); btn_M.setOnClickListener(this); Intent intent = getIntent(); String data = intent.getStringExtra("data"); switch (data){ case "Less": btn_M.setEnabled(false); break; case "Mang": btn_L.setEnabled(false); break; } // ArrayList nameList = intent.getCharSequenceArrayListExtra("nameList"); // Toast.makeText(this, "" + nameList, Toast.LENGTH_SHORT).show(); } @Override public void onClick(View view) { Intent intent = new Intent(); switch (view.getId()){ case R.id.btn_Less_data: inputCheck(1); break; case R.id.btn_Many_data: inputCheck(2); break; } } //输入框校验 private void inputCheck(int position){ String title = edt_title.getText().toString(); String name = edt_name.getText().toString(); String age = edt_age.getText().toString(); String sex = edt_sex.getText().toString(); if(title.isEmpty() && name.isEmpty() && age.isEmpty() && sex.isEmpty() ){ Toast.makeText(this, "传值不能为空", Toast.LENGTH_SHORT).show(); return; } Intent intent = getIntent(); switch (position){ case 1: if(!title.isEmpty()){ intent.putExtra("lessData",title); setResult(1,intent); finish(); } if(!name.isEmpty()){ intent.putExtra("lessData",name); setResult(2,intent); finish(); } if(!age.isEmpty()){ intent.putExtra("lessData",age); setResult(3,intent); finish(); } if(!sex.isEmpty()){ intent.putExtra("lessData",sex); setResult(4,intent); finish(); } break; case 2: if(title.isEmpty() || name.isEmpty() || age.isEmpty() || sex.isEmpty() ){ Toast.makeText(this, "传值不能为空", Toast.LENGTH_SHORT).show(); }else{ ArrayList<String> manyData = new ArrayList<>(); manyData.add(title); manyData.add(name); manyData.add(age); manyData.add(sex); intent.putStringArrayListExtra("manyData",manyData); setResult(123,intent); finish(); } break; } } }
GetDataActivity的布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:paddingTop="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" tools:context=".GetDataActivity"> <EditText android:id="@+id/edt_title" android:lines="1" android:maxLength="5" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="标题"/> <EditText android:id="@+id/edt_name" android:lines="1" android:maxLength="9" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="姓名"/> <EditText android:id="@+id/edt_age" android:lines="1" android:maxLength="2" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="年龄"/> <EditText android:id="@+id/edt_sex" android:lines="1" android:maxLength="2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="性别"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp"> <Button android:id="@+id/btn_Less_data" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="单条数据"/> <Button android:id="@+id/btn_Many_data" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="多条数据"/> </LinearLayout> </LinearLayout>
测试项目(点击获取,提取码:g555)