首页 > 系统 > Android > 正文

Android中Activity之间的跳转与传值

2019-11-09 14:37:25
字体:
来源:转载
供稿:网友

—— + —— = ——

在第一个Activity中获得输入的两个运算的数传递给第二个窗体来计算,然后第二个窗体将计算结果回传给第一个窗体来显示。

第一个Activity代码:

public class MainActivity extends AppCompatActivity {    PRivate EditText etNumOne;    private EditText etNumTwo;    private TextView tvResult;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
        etNumOne = (EditText) findViewById(R.id.etNumOne);        etNumTwo = (EditText) findViewById(R.id.etNumTwo);        tvResult = (TextView) findViewById(R.id.tvResult );    }
    public void calculate(View view){
        int numOne = Integer.parseInt(etNumOne.getText().toString());        int numTwo = Integer.parseInt(etNumTwo.getText().toString());
	//使用intent进行跳转与传值        Intent intent = new Intent(this,SecondActivity.class);
	//bundle用来封装数据。
	//使用bundle的好处在于当需要将多个数据从A->B->C多个类进行传递时,不需要在B中把这些数        据都获得再进行传递,只需要获得和传递一个bundle对象即可。        Bundle bundle = new Bundle();        bundle.putInt("numOne",numOne);        bundle.putInt("numTwo",numTwo);        intent.putExtras(bundle);
	//有值需要回传时调用该方法,传入数据并设置requestCode        startActivityForResult(intent,10);    }
    //重写该方法,根据requestCode与resultCode来区分哪次跳转和哪次回传,从而获得data中返回的       数据    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {       if(requestCode == 10 && resultCode == 20){           tvResult.setText(data.getIntExtra("result",0)+"");       }    }}
第一个窗体的布局文件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.shen.fourth.MainActivity">    <LinearLayout        android:layout_marginLeft="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <EditText            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/etNumOne"/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="+"/>        <EditText            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/etNumTwo"/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="="/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/tvResult"/>    </LinearLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/calculate"        android:onClick="calculate"/></LinearLayout>
线性布局嵌套线性布局,点击计算按钮进行计算。
第二个Activity的代码:
public class SecondActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        Intent intent = getIntent();
	//在第二个Activity中获得第一个窗体传入的数据进行计算        Bundle bundle = intent.getExtras();        int numOne = bundle.getInt("numOne",-1);        int numTwo = bundle.getInt("numTwo",-1);        int result = numOne + numTwo;
	//将计算的结果回传给第一个Activity        Intent reReturnIntent = new Intent(this,MainActivity.class);
        reReturnIntent.putExtra("result",result);        setResult(20,reReturnIntent);
	//退出第二个Activity
        this.finish();    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表