MainActivity
package com.example.intentdemo;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivityextends AppCompatActivityimplements Button.OnClickListener{
Buttonsubmit;
TextViewshow;
int Sub_Activity =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(this);
show = (TextView) findViewById(R.id.show);
}
@Override
public void onClick(View v) {
Intent intent =new Intent(MainActivity.this, Main2Activity.class);
startActivityForResult(intent, Sub_Activity);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri uriData = data.getData();
show.setText("用户名:" + uriData.toString());
}
}
}
main.layout
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=".MainActivity"
android:rowCount="2"
android:layout_column="1">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:id="@+id/submit"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center"
android:layout_marginTop="180dp"/>
android:id="@+id/show"
android:text="用户名:"
android:layout_row="1"
android:layout_column="0"
android:layout_marginTop="-190dp"
android:layout_gravity="center"/>
SubActivity
package com.example.intentdemo;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main2Activityextends AppCompatActivityimplements Button.OnClickListener{
EditTextinput;
Buttongoback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
input = (EditText) findViewById(R.id.input);
goback = (Button) findViewById(R.id.goback);
goback.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Uri data = Uri.parse(input.getText().toString());
Intent result =new Intent(null, data);
setResult(RESULT_OK, result);
finish();
}
}
sub.layout
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=".Main2Activity"
android:rowCount="2"
android:columnCount="2">
android:text="用户名:"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="0"
android:layout_marginTop="190dp"/>
android:id="@+id/input"
android:textSize="20dp"
android:layout_width="280dp"
android:layout_row="0"
android:layout_column="1"
android:layout_marginTop="190dp"/>
android:id="@+id/goback"
android:text="返回"
android:layout_row="1"
android:layout_columnSpan="2"
android:layout_marginTop="40dp"
android:layout_gravity="top|center" />