<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="300dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/bt_log"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登录"
android:textSize="25dp"
android:layout_marginBottom="60dp"/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textAlignment="center"
android:textSize="25dp"
android:text="用户名:" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:translationY="250dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25dp"
android:textAlignment="center"
android:text="用户名:" />
<EditText
android:id="@+id/et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_weight="2"/>
</LinearLayout>
<Button
android:id="@+id/bt_back"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="返回"
android:textSize="25dp"
android:layout_marginBottom="60dp"/>
</LinearLayout>
package com.example.op422;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import androidx.annotation.*;
import android.content.*;
public class MainActivity extends AppCompatActivity {
private Button bt_log;
private TextView tv;
int inta;
ActivityResultLauncher<Intent> mStartForResult=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new
ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode()== Activity.RESULT_OK){
Intent intent=result.getData();
String rs=intent.getStringExtra("result");
tv.append(rs);
}
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_log=findViewById(R.id.bt_log);
tv=findViewById(R.id.tv);
bt_log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// startActivityForResult(new Intent(MainActivity.this,SubActivity.class),1);
Intent intent=new Intent(MainActivity.this,SubActivity.class);
mStartForResult.launch(intent);
}
});
}
//这是如果用startActivityForResult返回结果处理的函数
// @Override
// protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
//
// if(requestCode==1 && resultCode==RESULT_OK){
// Intent intent=getIntent();
// String rs=intent.getStringExtra("result");
// tv.append(rs);
// }
// }
}
package com.example.op422;
import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.*;
import android.net.Uri;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.content.*;
import android.content.*;
import java.net.URI;
public class SubActivity extends AppCompatActivity {
private Button bt_back;
private EditText et;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
bt_back=findViewById(R.id.bt_back);
et=findViewById(R.id.et);
//返回按钮的单击响应事件
bt_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String result=et.getText().toString();
Uri uridata= Uri.parse(result);
Intent intent = new Intent();
intent.putExtra("result",result);
setResult(RESULT_OK,intent);
finish();
}
});
}
}
注意不要忘记在AndroidManifest.xml中添加定义
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Op422"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SubActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
注意Intent发送消息的流程:
因为我的StartActivityForResult被禁用了,于是用了这个launch的方式
1.先定义registerForActivityResult,在onActivityForResult中写的是返回结果的处理
2.在button的onClick中定义intent,将两个Activity联系到一起
3.button的onClick直接.launch跳转到另一个Activity
4.在另一个Activity中,发送数据用intent.putExtra();
//当然还有隐式启动,和其他发送数据的方法,老师ppt上都有,但懒惰的薯条此时并不想学会,期末考前会学会的。。。。