//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ml.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 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=".SUBACTIVITY1"> </activity> </application> </manifest>
//MainActivity.java
package com.example.ml.myapplication; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private static final int SUBACTIVITY1 = 1; //private static final int SUBACTIVITY2 = 2; TextView ed1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1 = (TextView) findViewById(R.id.et1); final Button btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,SUBACTIVITY1.class); startActivityForResult(intent,SUBACTIVITY1); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case SUBACTIVITY1: if (resultCode == RESULT_OK){ Uri uriData = data.getData(); ed1.setText(uriData.toString()); } break; } } }
//SUBACTIVITY1.java
package com.example.ml.myapplication; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; /** * Created by Administrator on 2018/6/19. */ @SuppressLint("Registered") public class SUBACTIVITY1 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.subactivity1); final EditText editText = (EditText)findViewById(R.id.et2); Button btnOK = (Button)findViewById(R.id.btn2); btnOK.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ String uriString = editText.getText().toString(); Uri data = Uri.parse(uriString); Intent result = new Intent(null, data); setResult(RESULT_OK, result); finish(); } }); } }
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context="com.example.ml.myapplication.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical|center"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="login"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="user:"/> <TextView android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </LinearLayout> </LinearLayout> </android.support.constraint.ConstraintLayout>
//subactivity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Username:"/> <EditText android:id="@+id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="backward"/> </LinearLayout>