startActivityForResult的用法(Android)
Posted 太快的幸福,措手不及
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了startActivityForResult的用法(Android)相关的知识,希望对你有一定的参考价值。
1、第一个页面的activity.java
package cn.buaa.z1; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void open(View view) { Intent intent = new Intent("sec"); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode ==1 && resultCode == 1){ String msg = data.getStringExtra("msg"); EditText et = (EditText)findViewById(R.id.et1); et.setText(msg); } } }
2、第二个页面的activity.java
package cn.buaa.z1; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class Main2Activity extends AppCompatActivity { private EditText et ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } public void retu(View view) { et=(EditText)findViewById(R.id.et2); Intent intent = this.getIntent(); intent.putExtra("msg",et.getText().toString()); setResult(1,intent); finish(); } }
3、第一个页面layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="cn.buaa.z1.MainActivity" android:orientation="vertical"> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="open" android:onClick="open"/> </LinearLayout>
4、第二个页面layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="cn.buaa.z1.Main2Activity" android:orientation="vertical"> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="return" android:onClick="retu"/> </LinearLayout>
5、xml配置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.buaa.z1"> <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=".Main2Activity"> <intent-filter> <action android:name="sec"></action> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> </manifest>
以上是关于startActivityForResult的用法(Android)的主要内容,如果未能解决你的问题,请参考以下文章
startActivityForResult的用法(Android)
Android startActivityForResult和onActivityResult的基本用法
Activity系列:startActivityForResult的用法