feat: 上传第四次安卓课程代码(2026年10月16日)

This commit is contained in:
Andy0613
2026-10-16 11:24:05 +08:00
parent d228e617d6
commit b043f324b7
8 changed files with 139 additions and 17 deletions
-11
View File
@@ -13,17 +13,6 @@
</DropdownSelection> </DropdownSelection>
<DialogSelection /> <DialogSelection />
</SelectionState> </SelectionState>
<SelectionState runConfigName="FirstActivity">
<option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2026-09-18T02:12:50.815210100Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\stu\.android\avd\MyPhone35.avd" />
</handle>
</Target>
</DropdownSelection>
<DialogSelection />
</SelectionState>
</selectionStates> </selectionStates>
</component> </component>
</project> </project>
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
+1 -1
View File
@@ -4,7 +4,7 @@
<option name="optimizeImportsOnTheFly" value="true" /> <option name="optimizeImportsOnTheFly" value="true" />
</component> </component>
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="jbr-21" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
@@ -11,6 +11,10 @@ import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@@ -20,6 +24,9 @@ import androidx.appcompat.widget.PopupMenu;
public class FirstActivity extends AppCompatActivity { public class FirstActivity extends AppCompatActivity {
private static final String TAG = "FirstActivity"; private static final String TAG = "FirstActivity";
// step1: 定义contract启动器类对象,初始化放在Activity生命周期的初始阶段
ActivityResultLauncher mActivityResultLauncher;
// 先声明(变量命名:小驼峰)再使用 // 先声明(变量命名:小驼峰)再使用
// 全局(成员)变量:m开头 // 全局(成员)变量:m开头
Button mFirstBtn1; Button mFirstBtn1;
@@ -41,11 +48,27 @@ public class FirstActivity extends AppCompatActivity {
setContentView(R.layout.first_layout); setContentView(R.layout.first_layout);
Log.d(TAG, "onCreate: 活动创建成功"); // 组合键2:alter + enter Log.d(TAG, "onCreate: 活动创建成功"); // 组合键2:alter + enter
// step2: 初始化启动器类对象
mActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() { // 触发要求:SecondActivity销毁
@Override
public void onActivityResult(ActivityResult o) {
if (o.getResultCode() == RESULT_OK && o.getData() != null) {
Intent intent = o.getData();
String msgBackFromSecond = intent.getStringExtra("second_backto_first");
Log.d(TAG, "收到Second反馈的数据:" + msgBackFromSecond);
}
}
});
// step3: 适当的时候启动启动器类
mFirstBtn4 = findViewById(R.id.first_btn4); mFirstBtn4 = findViewById(R.id.first_btn4);
mFirstBtn4.setOnClickListener(v -> { mFirstBtn4.setOnClickListener(v -> {
// Activity之间的跳转——显式Intent // Activity之间的跳转——显式Intent
Intent intent = new Intent(FirstActivity.this, SecondActivity.class); Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent); intent.putExtra("first_to_second", "Hello, second! I'm first! Are you OK? 6117");
mActivityResultLauncher.launch(intent);
// startActivity(intent);
}); });
mFirstBtn3 = findViewById(R.id.first_btn3); mFirstBtn3 = findViewById(R.id.first_btn3);
@@ -2,23 +2,81 @@ package com.yanzitong.activitytest6117;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity { public class SecondActivity extends AppCompatActivity {
public static final String TAG = "SecondActivity";
TextView mSecondShowMsgTV;
ActivityResultLauncher mLauncher;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout); setContentView(R.layout.second_layout);
mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
o -> {
if (o.getResultCode() == RESULT_OK && o.getData() != null) {
Intent intent = o.getData();
String msgBackFromThird = intent.getStringExtra("third_backto_second");
Log.d(TAG, "Second收到Third反馈的数据:" + msgBackFromThird);
}
});
// 判断数据是否接受成功
Intent intent2 = getIntent();
String msgFromFirst = intent2.getStringExtra("first_to_second");
mSecondShowMsgTV = findViewById(R.id.second_show_msg_tv);
Button mSecondBtn2 = findViewById(R.id.second_btn2);
mSecondBtn2.setOnClickListener(v -> {
Intent intent3 = new Intent();
if (!TextUtils.isEmpty(msgFromFirst)) {
intent3.putExtra("second_backto_first", "Hi, first! 我收到你的消息了,谢谢,加油!");
} else {
intent3.putExtra("second_backto_first", "Hi, first! 我没有收到你的数据,有什么事啊?");
}
setResult(RESULT_OK, intent3);
finish();
});
if (TextUtils.isEmpty(msgFromFirst) == false) {
Log.d(TAG, "收到来自于First的数据" + msgFromFirst);
Toast.makeText(this, "收到来自于First的数据" + msgFromFirst, Toast.LENGTH_SHORT).show();
mSecondShowMsgTV.setText(msgFromFirst);
} else {
}
Button mSecondBtn1 = findViewById(R.id.second_btn1); Button mSecondBtn1 = findViewById(R.id.second_btn1);
mSecondBtn1.setOnClickListener(v -> { mSecondBtn1.setOnClickListener(v -> {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class); Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent); intent.putExtra("second_to_third", "Nice to meet you, Third! 我是Second! 6117");
mLauncher.launch(intent);
// startActivity(intent);
}); });
} }
} }
/**
* your turn:
* SecondActivity传递数据给ThirdActivity, "Nice to meet you, Third! 我是Second! 6117"
* 日志、Toast、界面显示数据
*/
/** /**
* your turn: * your turn:
* 新建UI: third_layout和Java class: thirdActivity * 新建UI: third_layout和Java class: thirdActivity
@@ -1,8 +1,13 @@
package com.yanzitong.activitytest6117; package com.yanzitong.activitytest6117;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@@ -11,12 +16,26 @@ public class ThirdActivity extends AppCompatActivity {
private Button mThirdBtn1; private Button mThirdBtn1;
private Button mThirdBtn2; private Button mThirdBtn2;
private Button mThirdBtn3; private Button mThirdBtn3;
TextView mThirdShowMsgTV;
public static final String TAG = "ThirdActivity";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout); setContentView(R.layout.third_layout);
mThirdShowMsgTV = findViewById(R.id.third_show_msg_tv);
Intent intent3 = getIntent();
String msgFromSecond = intent3.getStringExtra("second_to_third");
if (!TextUtils.isEmpty(msgFromSecond)) {
Log.d(TAG, "收到来自于Second的数据" + msgFromSecond);
Toast.makeText(this, "收到来自于Second的数据" + msgFromSecond, Toast.LENGTH_SHORT).show();
mThirdShowMsgTV.setText(msgFromSecond);
} else {
}
mThirdBtn1 = findViewById(R.id.third_btn1); mThirdBtn1 = findViewById(R.id.third_btn1);
mThirdBtn1.setOnClickListener(v -> { // 项目内部隐式Intent:匹配action和category mThirdBtn1.setOnClickListener(v -> { // 项目内部隐式Intent:匹配action和category
Intent intent = new Intent("com.yanzitong.activitytest6117.action.YZT_FIRST"); // 为FirstActivity定义对应的action Intent intent = new Intent("com.yanzitong.activitytest6117.action.YZT_FIRST"); // 为FirstActivity定义对应的action
@@ -27,12 +46,28 @@ public class ThirdActivity extends AppCompatActivity {
mThirdBtn2 = findViewById(R.id.third_btn2); mThirdBtn2 = findViewById(R.id.third_btn2);
mThirdBtn3 = findViewById(R.id.third_btn3); mThirdBtn3 = findViewById(R.id.third_btn3);
mThirdBtn2.setOnClickListener(v -> { mThirdBtn2.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW); // 等价于"Intent intent = new Intent(android.intent.action.VIEW)" // 方法二
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent); startActivity(intent);
// 方法一
/* Uri uri = Uri.parse("https://www.baidu.com");
* Intent intent = new Intent(Intent.ACTION_VIEW, uri); // 等价于"Intent intent = new Intent(android.intent.action.VIEW)"
* startActivity(intent);
*/
}); });
mThirdBtn3.setOnClickListener(v -> { mThirdBtn3.setOnClickListener(v -> {
Intent intent = new Intent(); Intent intent4 = new Intent();
startActivity(intent); if (TextUtils.isEmpty(msgFromSecond) == false) {
intent4.putExtra("third_backto_second", "Hi, second! 我收到你的消息了,谢谢,加油!");
} else {
intent4.putExtra("third_backto_second", "Hi, second! 我没有收到消息!你想跟我说什么?");
}
setResult(RESULT_OK, intent4);
finish();
// Intent intent = new Intent(Intent.ACTION_DIAL);
// intent.setData(Uri.parse("tel:10000"));
// startActivity(intent);
}); });
} }
@@ -19,4 +19,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="跳转到ThirdActivity" android:text="跳转到ThirdActivity"
android:textAllCaps="false" /> android:textAllCaps="false" />
<Button
android:id="@+id/second_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="反馈数据给First"
android:textAllCaps="false" />
</LinearLayout> </LinearLayout>
+9
View File
@@ -27,4 +27,13 @@
android:layout_marginTop="50dp" android:layout_marginTop="50dp"
android:text="打开拨号界面" android:text="打开拨号界面"
android:textAllCaps="false" /> android:textAllCaps="false" />
<TextView
android:id="@+id/third_show_msg_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="显示数据"
android:textColor="#F00"
android:textSize="18sp" />
</LinearLayout> </LinearLayout>