效果图
Main2Activity.java`
package xdglyt.software.jhwz.myapplication9bataback;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
private TextView tv1;
private TextView tv2;
private Button btn;
private MyInfo myInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv1 = (TextView) findViewById(R.id.tv_show_name);
tv2 = (TextView) findViewById(R.id.tv_show_psd);
btn = (Button) findViewById(R.id.edit);
}
public void onclick(View v) {
Intent intent = new Intent(this,MainActivity.class);
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(data != null){
if(requestCode == 1){
if(resultCode == 1){
myInfo = (MyInfo) data.getSerializableExtra("myInfo");
updateMyInfo(myInfo);
}
}
}
}
private void updateMyInfo(MyInfo myInfo){
tv1.setText(myInfo.getName()+"");
tv2.setText(myInfo.getPsd()+"");
}
}
xml
<?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:orientation="vertical"
tools:context=".Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<TextView
style="@style/text"
android:text="姓名:"/>
<TextView
android:id="@+id/tv_show_name"
android:background="#fff"
style="@style/input"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<TextView
style="@style/text"
android:text="密码:"/>
<TextView
android:id="@+id/tv_show_psd"
android:background="#fff"
style="@style/input"
/>
</LinearLayout>
<Button
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="edit"
android:onClick="onclick"
android:textSize="25sp"/>
</LinearLayout>
MainActivity.java
package xdglyt.software.jhwz.myapplication9bataback;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private Button btn;
private EditText et1;
private EditText et2;
private MyInfo myInfo;
private String name;
private String psd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.submit);
et1 = (EditText) findViewById(R.id.et_name);
et2 = (EditText) findViewById(R.id.et_psd);
}
public void onclick(View v) {
Intent intent = new Intent();
name = et1.getText().toString();
psd = et2.getText().toString();
myInfo = new MyInfo(name,psd);
intent.putExtra("myInfo",myInfo);
setResult(1,intent);
finish();
}
}
xml
<?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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="@style/text"
android:text="用户名:"/>
<EditText
android:id="@+id/et_name"
style="@style/input"
android:hint="please input the name"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="@style/text"
android:text="密 码:"/>
<EditText
android:id="@+id/et_psd"
style="@style/input"
android:hint="please input the name"/>
</LinearLayout>
<Button
android:id="@+id/submit"
android:onClick="onclick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="submit"
android:textSize="25sp"/>
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="text">
<item name="android:textSize">25sp</item>
<item name="android:layout_weight">2</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">right</item>
<item name="android:paddingRight">5dp</item>
</style>
<style name="input">
<item name="android:textSize">25sp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">5dp</item>
<item name="android:textColor">#000</item>
<item name="android:paddingLeft">20dp</item>
<item name="android:background">#fff</item>
<item name="android:layout_marginRight">15dp</item>
<item name="android:layout_weight">5</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_marginLeft">8dp</item>
</style>
</resources>
MyInfo.java
package xdglyt.software.jhwz.myapplication9bataback;
import java.io.Serializable;
public class MyInfo implements Serializable {
private String name;
private String psd;
public MyInfo(String name,String psd){
this.name = name;
this.psd = psd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsd() {
return psd;
}
public void setPsd(String psd) {
this.psd = psd;
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xdglyt.software.jhwz.myapplication9bataback">
<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=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>