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

This commit is contained in:
Andy0613
2026-10-16 01:15:39 +08:00
parent 4a3821c791
commit f9f06d514d
8 changed files with 234 additions and 5 deletions
+6 -1
View File
@@ -10,15 +10,20 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ActivityTest6117_1">
<activity
android:name=".FirstActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="com.yanzitong.activitytest6117.action.YZT_FIRST" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.yanzitong.activitytest6117.category.YZT_FIRST" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity" />
</application>
</manifest>
@@ -1,16 +1,20 @@
package com.yanzitong.activitytest6117;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
// 组合键:shift + enter
public class FirstActivity extends AppCompatActivity {
@@ -20,6 +24,9 @@ public class FirstActivity extends AppCompatActivity {
// 全局(成员)变量:m开头
Button mFirstBtn1;
Button mFirstBtn2;
LinearLayout mFirstLinearLayout;
Button mFirstBtn3;
Button mFirstBtn4;
/**
* 事件驱动机制
@@ -33,13 +40,40 @@ public class FirstActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
Log.d(TAG, "onCreate: 活动创建成功"); // 组合键2:alter + enter
mFirstBtn4 = findViewById(R.id.first_btn4);
mFirstBtn4.setOnClickListener(v -> {
// Activity之间的跳转——显式Intent
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
});
mFirstBtn3 = findViewById(R.id.first_btn3);
mFirstBtn3.setOnClickListener(v -> {
finish();
});
mFirstBtn1 = findViewById(R.id.first_btn1);
mFirstBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FirstActivity.this, "6117严梓桐单击了按钮", Toast.LENGTH_SHORT).show();
PopupMenu popupMenu = new PopupMenu(FirstActivity.this, v); // this, super
popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.add_item) {
Toast.makeText(FirstActivity.this, "单击了add按钮", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == R.id.modify_item) {
Toast.makeText(FirstActivity.this, "单击了modify按钮", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == R.id.delete_item) {
Toast.makeText(FirstActivity.this, "单击了delete按钮", Toast.LENGTH_SHORT).show();
}
return true;
}); // 箭头函数(lambda表达式)——接口[只能有一个抽象函数]
popupMenu.show();
}
});
mFirstBtn2 = findViewById(R.id.first_btn2);
mFirstBtn2.setOnClickListener(new View.OnClickListener() {
@Override
@@ -48,8 +82,43 @@ public class FirstActivity extends AppCompatActivity {
Toast.makeText(FirstActivity.this, "我是严梓桐", Toast.LENGTH_SHORT).show();
}
});
mFirstLinearLayout = findViewById(R.id.first_linear_layout);
registerForContextMenu(mFirstLinearLayout);
}
// 弹出式菜单——触发事件
// 上下文菜单
// ctrl + shift + "+ / -": 展开、折叠代码块
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("上下文菜单");
// 参数1:分组号,参数2:item编号,参数3:顺序编号
menu.add(1, 1, 1, "保存");
menu.add(1, 2, 2, "更改");
menu.add(1, 3, 3, "删除");
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == 1) {
Toast.makeText(this, "人物简介——特长", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onContextItemSelected: 人物简介——特长");
} else if (item.getItemId() == 2) {
Toast.makeText(this, "人物简介——战斗力", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onContextItemSelected: 人物简介——战斗力");
} else if (item.getItemId() == 3) {
Toast.makeText(this, "人物简介——经典语录", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onContextItemSelected: 人物简介——经典语录");
}
return super.onContextItemSelected(item);
}
// 选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
@@ -74,6 +143,7 @@ public class FirstActivity extends AppCompatActivity {
/**
* 添加一个按钮,id = first_btn2,完成单击事件:弹出Toast消息日志,内容自定义,带个人标识
* <p>
* mFirstBtn2 = findViewById(R.id.first_btn2);
* mFirstBtn2.setOnClickListener(new View.OnClickListener() {
*
@@ -0,0 +1,27 @@
package com.yanzitong.activitytest6117;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Button mSecondBtn1 = findViewById(R.id.second_btn1);
mSecondBtn1.setOnClickListener(v -> {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
});
}
}
/**
* your turn:
* 新建UI: third_layout和Java class: thirdActivity
* 按钮text = "跳转到ThirdActivity"
* 完成单击事件:实现从 SecondActivity 显式跳转到 ThirdActivity
*/
@@ -0,0 +1,46 @@
package com.yanzitong.activitytest6117;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class ThirdActivity extends AppCompatActivity {
private Button mThirdBtn1;
private Button mThirdBtn2;
private Button mThirdBtn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
mThirdBtn1 = findViewById(R.id.third_btn1);
mThirdBtn1.setOnClickListener(v -> { // 项目内部隐式Intent:匹配action和category
Intent intent = new Intent("com.yanzitong.activitytest6117.action.YZT_FIRST"); // 为FirstActivity定义对应的action
intent.addCategory("com.yanzitong.activitytest6117.category.YZT_FIRST");
startActivity(intent);
});
mThirdBtn2 = findViewById(R.id.third_btn2);
mThirdBtn3 = findViewById(R.id.third_btn3);
mThirdBtn2.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW); // 等价于"Intent intent = new Intent(android.intent.action.VIEW)"
startActivity(intent);
});
mThirdBtn3.setOnClickListener(v -> {
Intent intent = new Intent();
startActivity(intent);
});
}
// 隐式Intent——项目内部或项目之间的调用
// 1. 实现ThirdActivity返回FirstActivity
/**
* 为FirstActivity定义对应的action和category的值
* category可以是默认值,不可缺
* category自定义
*/
}
+19 -4
View File
@@ -1,22 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/first_btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到SecondActivity"
android:textAllCaps="false" />
<Button
android:id="@+id/first_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:layout_marginTop="50dp"
android:text="显示Toast消息" />
android:text="显示Toast消息"
android:textAllCaps="false" />
<Button
android:id="@+id/first_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:layout_marginTop="50dp"
android:text="我是严梓桐" />
android:text="我是严梓桐"
android:textAllCaps="false" />
<Button
android:id="@+id/first_btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="退出当前Activity"
android:textAllCaps="false" />
</LinearLayout>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical">
<TextView
android:id="@+id/second_show_msg_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="显示数据"
android:textColor="#F00"
android:textSize="18sp" />
<Button
android:id="@+id/second_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到ThirdActivity"
android:textAllCaps="false" />
</LinearLayout>
+30
View File
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/third_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="隐式跳转到FirstActivity"
android:textAllCaps="false" />
<Button
android:id="@+id/third_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="打开浏览器"
android:textAllCaps="false" />
<Button
android:id="@+id/third_btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="打开拨号界面"
android:textAllCaps="false" />
</LinearLayout>
+14
View File
@@ -13,4 +13,18 @@
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.ActivityTest6117_1" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>