AndroidStudio之双向传递数据
程序员文章站
2022-07-02 10:32:04
Intent机制 Activity间数据传递双向传递数据A启动B,B使用setResut()方法将Intent结果传到A的onActivityResult中。B返回数据给A的核心代码框架A端Intent intent=new Intent(MainActivity.this,B_Activity.class);startActivityForResult(intent,100);B端Intent intent=getIntent();Bundle bundle=intent.getExtr...
Intent机制 Activity间数据传递
双向传递数据
A启动B,B使用setResut()方法将Intent结果传到A的onActivityResult中。
B返回数据给A的核心代码框架
A端
Intent intent=new Intent(MainActivity.this,B_Activity.class);
startActivityForResult(intent,100);
B端
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
双向传递数据示例
MainActivity输入两个整数并选择需要进行的计算,B_Activity计算并返回值。
MainActivity主要代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1=(Button)findViewById(R.id.rbtn_1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
String a=et1.getText().toString();
String b=et2.getText().toString();
Intent intent=new Intent(MainActivity.this,B_Activity.class);
Bundle bundle=new Bundle();
bundle.putString("a",a);
bundle.putString("b",b);
intent.putExtras(bundle);
startActivityForResult(intent,100);
}
});
Button btn2=(Button)findViewById(R.id.rbtn_2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
String a=et1.getText().toString();
String b=et2.getText().toString();
Intent intent=new Intent(MainActivity.this,B_Activity.class);
Bundle bundle=new Bundle();
bundle.putString("a",a);
bundle.putString("b",b);
intent.putExtras(bundle);
startActivityForResult(intent,200);
}
});
Button btn3=(Button)findViewById(R.id.rbtn_3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
String a=et1.getText().toString();
String b=et2.getText().toString();
Intent intent=new Intent(MainActivity.this,B_Activity.class);
Bundle bundle=new Bundle();
bundle.putString("a",a);
bundle.putString("b",b);
intent.putExtras(bundle);
startActivityForResult(intent,300);
}
});
Button btn4=(Button)findViewById(R.id.rbtn_4);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
String a=et1.getText().toString();
String b=et2.getText().toString();
Intent intent=new Intent(MainActivity.this,B_Activity.class);
Bundle bundle=new Bundle();
bundle.putString("a",a);
bundle.putString("b",b);
intent.putExtras(bundle);
startActivityForResult(intent,400);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100)
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
int s=bundle.getInt("sum");
TextView tv=(TextView)findViewById(R.id.msg);;
tv.setText("结果="+s);
}
if(requestCode==200)
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
int s=bundle.getInt("sum2");
TextView tv=(TextView)findViewById(R.id.msg);;
tv.setText("结果="+s);
}
if(requestCode==300)
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
int s=bundle.getInt("sum3");
TextView tv=(TextView)findViewById(R.id.msg);;
tv.setText("结果="+s);
}
if(requestCode==400)
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
int s=bundle.getInt("sum4");
TextView tv=(TextView)findViewById(R.id.msg);;
tv.setText("结果="+s);
}
}
}
B_Activity主要代码
public class B_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b_);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String a=bundle.getString("a");
String b=bundle.getString("b");
int sum=Integer.parseInt(a)+Integer.parseInt(b);
bundle.putInt("sum",sum);
intent.putExtras(bundle);
int sum2=Integer.parseInt(a)-Integer.parseInt(b);
bundle.putInt("sum2",sum2);
intent.putExtras(bundle);
int sum3=Integer.parseInt(a)*Integer.parseInt(b);
bundle.putInt("sum3",sum3);
intent.putExtras(bundle);
int sum4=Integer.parseInt(a)/Integer.parseInt(b);
bundle.putInt("sum4",sum4);
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
}
}
activity_main.xml布局文件
<LinearLayout
android:id="@+id/LayOut1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="a="
android:textSize="30sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ems="10"
android:inputType="numberSigned" />
</LinearLayout>
<LinearLayout
android:id="@+id/LayOut2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LayOut1">
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="b="
android:textSize="30sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ems="10"
android:inputType="numberSigned" />
</LinearLayout>
<LinearLayout
android:id="@+id/LayOut3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LayOut2">
<RadioGroup
android:id="@+id/RadioButton1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbtn_1"
android:layout_width="74dp"
android:layout_height="50dp"
android:text="+"
android:textSize="30sp" />
<RadioButton
android:id="@+id/rbtn_2"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:text="-"
android:textSize="36sp" />
<RadioButton
android:id="@+id/rbtn_3"
android:layout_width="73dp"
android:layout_height="50dp"
android:layout_weight="0"
android:text="*"
android:textSize="36sp" />
<RadioButton
android:id="@+id/rbtn_4"
android:layout_width="69dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="/"
android:textSize="36sp" />
</RadioGroup>
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="结果="
android:textSize="30sp" />
</LinearLayout>
运行结果
本文地址:https://blog.csdn.net/weixin_44728725/article/details/107199045