1.意图方法:startActivityForResult---请求码的startActivity
2.设置返回码和返回值/结束Activity:setResult和finish
3.接收返回值:重写onActivityResult,判断请求码和返回码
Manifest.xml:清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.manusas.activitycalbak"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NextActivity"> <intent-filter> <action android:name="nextaction" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
MainActivity.java
package com.manusas.activitycalbak; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private EditText plus1; private EditText plus2; private EditText showsumnum; private Button btngoto; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); plus1 = (EditText) this.findViewById(R.id.plus1); plus2 = (EditText) this.findViewById(R.id.plus2); showsumnum = (EditText) this.findViewById(R.id.showsumnum); btngoto = (Button) this.findViewById(R.id.btngoto); btngoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, NextActivity.class); String p1 = plus1.getText().toString().trim(); String p2 = plus2.getText().toString().trim(); i.putExtra("p1", p1); i.putExtra("p2", p2); //startActivityForResult 100是请求码 相当于请求pk 不是startActivity了 startActivityForResult(i, 100); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //重写接收返回值方法 判断请求和返回码 if (requestCode == 100 && resultCode == 200) { String sum = data.getStringExtra("sum"); showsumnum.setText(sum); } } }
NextActivity.java
package com.manusas.activitycalbak; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class NextActivity extends Activity { private TextView showplus; private EditText sumnum; private Button btncalbak; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); Intent i = getIntent(); String s = i.getStringExtra("p1") + "+" + i.getStringExtra("p2") + ":"; showplus = (TextView) this.findViewById(R.id.showplus); showplus.setText(s); btncalbak = (Button) findViewById(R.id.btncalbak); btncalbak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sumnum = (EditText) findViewById(R.id.sumnum); String sumn = sumnum.getText().toString().trim(); Intent calbak = new Intent(); calbak.putExtra("sum", sumn); //设置返回值方法 200返回码 相当于返回值pk setResult(200, calbak); finish(); } }); } }
activity_main.xml:主页
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.manusas.activitycalbak.MainActivity"> <EditText android:id="@+id/plus1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toEndOf="@+id/textView" android:ems="10" android:inputType="number" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="加数1:" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/plus1" android:text="加数2:" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/plus2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/plus1" android:layout_below="@+id/plus1" android:ems="10" android:inputType="number" /> <Button android:id="@+id/btngoto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/plus2" android:text="跳转" /> <EditText android:id="@+id/showsumnum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/btngoto" android:layout_centerHorizontal="true" /> </RelativeLayout>
activity_next.xml:跳转页
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.manusas.activitycalbak.MainActivity"> <TextView android:id="@+id/showplus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="和:" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/sumnum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toEndOf="@+id/showplus" android:ems="10" android:inputType="number" /> <Button android:id="@+id/btncalbak" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/sumnum" android:layout_alignParentEnd="true" android:text="值回传" /> </RelativeLayout>