Android中的Activity(案例+代码+效果图)

devtools/2024/10/18 12:30:17/

目录

1.Activity的生命周期

核心生命周期回调

1)onCreate()

2)onStart()

3)onResume()

4)onPause()

5)onStop()

6)onRestart()

7)onDestroy()

8)生命周期图示

10)注意事项

2.如何创建一个Activity

xml%EF%BC%89-toc" style="margin-left:0px;">3.清单文件配置Activity  (AndroidManifest.xml

1)第一种配置

2)第二种配置

4.如何启动和关闭

1)启动

2)关闭

3.Intent与IntentFilter

1)Intent

1-主要用途

2-创建 Intent

3-添加额外数据

4-接收额外数据

2)IntentFilter

1-声明方式

2-组成部分

1--Action

2--Category

3--Data

3-匹配规则

1--Action

2--Category

3--Data

4.Activity之间的传递

1)数据传递

1-通过putExtra()方法传递

1--数据的传递

2--数据的接受

2-通过通过Bundle类传递数据

1--数据发送

2--数据接受

2)数据回传

1. startActivityForResult()方法 (activity销毁时,返回数据)

2.setResult()方法

3.onActivityResult()

案例:模拟登录修改

1.代码

xml%E4%BB%A3%E7%A0%81-toc" style="margin-left:80px;">1--xml代码

xml%E4%BB%A3%E7%A0%81-toc" style="margin-left:120px;">1---activity_main.xml代码

xml%E4%BB%A3%E7%A0%81-toc" style="margin-left:120px;">2---activity_login_main.xml代码

xml%E4%BB%A3%E7%A0%81-toc" style="margin-left:120px;">3---activity_alter_info.xml代码

xml-toc" style="margin-left:120px;">4.AndroidManifest.xml

java%E4%BB%A3%E7%A0%81-toc" style="margin-left:80px;">2--java代码

1---MainActivity

2---LoginInfo

3---AlterInfo

2.效果


1.Activity的生命周期

核心生命周期回调

1)onCreate()

  • 当 Activity 被创建时调用
  • 这是初始化组件的最佳时机,比如设置布局、绑定数据等。
  • 示例代码:
    java">@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化其他组件...
    }

2)onStart()

  • 当 Activity 变得可见但尚未获取焦点时调用。
  • 在这个阶段,用户到 Activity,但是还不能与之交互

3)onResume()

  • 当 Activity 开始与用户交互并获得焦点时调用
  • 此时 Activity 处于活动状态,并可以响应用户的输入。

示例代码:

java">@Override
protected void onResume() {super.onResume();// 恢复暂停的操作,如开始传感器监听...
}

4)onPause()

  • 当 Activity 部分被遮挡或完全不可见时调用。
  • 系统可能会在 Activity 不再处于前台时调用此方法,例如当新的 Activity 启动或者对话框出现时。
  • 在这里应该释放一些资源以提高系统性能。
  • 示例代码:
java">@Override
protected void onPause() {super.onPause();// 停止占用CPU的动画或其他操作...
}

5)onStop()

  • 当 Activity 对用户完全不可见时调用。
  • 在这个阶段,Activity 已经不再显示给用户,但仍驻留在内存中
  • 示例代码:
java">@Override
protected void onStop() {super.onStop();// 更多清理工作...
}

6)onRestart()

  • 当 Activity 从停止状态动时调用
  • 它总是在 onStart() 方法之前调用。
  • 示例代码:
java">@Override
protected void onRestart() {super.onRestart();// 重启时的处理逻辑...
}

7)onDestroy()

  • 当 Activity 即将被销毁时调用
  • 在这里进行最后的清理工作,比如取消网络请求、关闭数据库连接等。
  • 示例代码:
java">@Override
protected void onDestroy() {super.onDestroy();// 清理所有剩余资源...
}

8)生命周期图示

+---------------------+
|      onCreate()     |
+---------------------+|v
+---------------------+
|      onStart()      |
+---------------------+|v
+---------------------+
|     onResume()      |
+---------------------+|v
+---------------------+  +---------------------+
|                      |  |    用户交互...     |
|    Activity活跃      |<->|                    |
|                      |  |                    |
+---------------------+  +---------------------+|v
+---------------------+
|     onPause()       |
+---------------------+|v
+---------------------+
|      onStop()       |
+---------------------+|v
+---------------------+
|      onDestroy()    |
+---------------------+

10)注意事项

  • onSaveInstanceState(Bundle outState) 和 onRestoreInstanceState(Bundle savedInstanceState) 是用于保存和恢复 Activity 状态的方法,它们通常在配置更改(如屏幕旋转)或低内存情况下的进程被杀死后重新创建 Activity 时使用。
  • 如果你在 AndroidManifest.xml 中为 Activity 设置了 android:configChanges 属性,那么系统会在配置改变时不会自动销毁并重建 Activity,而是会调用 onConfigurationChanged(Configuration newConfig) 方法。
  • 在处理 Activity 生命周期时,要确保你的应用能够优雅地处理各种可能的情况,包括突然的中断和意外的退出。

2.如何创建一个Activity

在res的目录 === > 鼠标右击  ===》 new   ===> Activity   ===>Empty Views Activity

手动创建一个AlterInfo

xml%EF%BC%89">3.清单文件配置Activity  (AndroidManifest.xml

1)第一种配置

        <!--配置LoginInfo-->
        <activity
            android:name=".LoginInfo"
            android:exported="false" />

2)第二种配置

注:类名大写+

        <!--配置隐式意图-->
        <activity
            android:name=".AlterInfo"
            android:exported="true">
            
            <intent-filter>
                <action android:name="com.xiji.myapplication123.ALTER_INFO" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

清单文件所有内容

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.MyApplication123"tools:targetApi="31"><!--配置LoginInfo--><activityandroid:name=".LoginInfo"android:exported="false" /><!----><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!--配置隐式意图--><activityandroid:name=".AlterInfo"android:exported="true"><intent-filter><action android:name="com.xiji.myapplication123.ALTER_INFO" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity></application></manifest>

4.如何启动和关闭

1)启动

public void startActivity (Intent intent);

//创建意图,并且指定要跳转的页面

Intent intent = new Intent(MainActivity.this,LoginInfo.class);

//启动

startActivity(intent);

2)关闭

public void finsh();

控件.设置监听事件((){

        public void 事件{

                 finsh();//调用finsh()关闭就可以了

        

        }

});

3.Intent与IntentFilter

1)Intent

  Intent 是一种消息对象,它可以在不同组件之间发送,以执行特定的动作。它可以携带数据,并且可以指定要执行的操作的类型、目标组件以及一些额外的数据。

1-主要用途
  • 启动 Activity:使用 startActivity(Intent) 方法。
  • 启动 Service:使用 startService(Intent) 方法。
  • 发送广播:使用 sendBroadcast(Intent)sendOrderedBroadcast(Intent, String), 等方法。
2-创建 Intent
java">// 显式 Intent - 直接指定目标组件
Intent explicitIntent = new Intent(context, TargetActivity.class);// 隐式 Intent - 通过 Action 和 Category 来匹配合适的组件
Intent implicitIntent = new Intent();
implicitIntent.setAction(Intent.ACTION_VIEW);
implicitIntent.setData(Uri.parse("http://www.example.com"));
3-添加额外数据
java">Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
4-接收额外数据
java">Intent intent = getIntent();
String value = intent.getStringExtra("key");

2)IntentFilter

IntentFilter 用来定义一个组件愿意接收哪种类型的 Intent。它通常在 AndroidManifest.xml 文件中与组件一起声明,或者在代码中动态创建。

1-声明方式
  • 在 Manifest 文件中声明

    java"><activity android:name=".TargetActivity"><intent-filter><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:scheme="http" /></intent-filter>
    </activity>
  • 在代码中动态创建

    java">IntentFilter filter = new IntentFilter();
    filter.addAction("com.example.MY_ACTION");
    registerReceiver(myBroadcastReceiver, filter);
2-组成部分
1--Action

        表示 Intent 的动作,例如 ACTION_VIEWACTION_SEND 等。

2--Category

        提供 Intent 的附加信息,比如 CATEGORY_DEFAULTCATEGORY_BROWSABLE 等。

3--Data

        指定 Intent 操作的数据和数据类型,例如 URI 和 MIME 类型。

3-匹配规则

当系统尝试找到能够响应给定 Intent 的组件时,会根据 IntentIntentFilter 的以下属性进行匹配:

1--Action

  Intent 的 action 必须与 IntentFilter 中的一个 action 匹配。

2--Category

  Intent 中的所有 category 都必须在 IntentFilter 中出现。

3--Data

      如果 Intent 包含 data,那么 data 必须与 IntentFilter 中的一个 data 规范匹配。

4.Activity之间的传递

1)数据传递

1-通过putExtra()方法传递

1--数据的传递

Intent intent = new Intent();

//页面跳转

intent.setClass(MainActivity.this,LoginInfo.calss)

//要传递的数据

intent.putExtra("键名","数据");

2--数据的接受

Intent intent = getIntent();

//通过键名获取
intent.getStringExtra("键名")

2-通过通过Bundle类传递数据

1--数据发送

Intent intent = new Intent();

//设置跳转的

intent.setClass(this,LoginInfo.class);

//创建并且封装Bundle类

Bundle bundle = new Bundle();

bundle.putString("键名","键值")

intent.putString(bundle);

startActivity(intent);

2--数据接受

//获取bundle对象

Bundle bundle = getIntent().getExtras();

//通过键名取

String 名字 = bundle.getString("键名")

2)数据回传

1. startActivityForResult()方法 (activity销毁时,返回数据)

startActivityForResult(Intent 意图, int 结果响应码);

2.setResult()方法

setResult(int 结果响应码,Intent 意图)

3.onActivityResult()

接受回传数据

重写回传Activity页面的的onActivityResult()方法;

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);
}

案例:模拟登录修改

流程图:

1.代码

xml%E4%BB%A3%E7%A0%81">1--xml代码

xml%E4%BB%A3%E7%A0%81">1---activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="用户登录"android:gravity="center"android:textSize="20sp"android:textColor="#F6F6F6"tools:ignore="MissingConstraints"android:background="#46E90B"android:padding="20sp"/><!--用户框--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"android:layout_marginTop="60dp"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="77dp"tools:layout_editor_absoluteY="149dp" /><EditTextandroid:id="@+id/editName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="输入用户名"android:inputType="text"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="136dp" /></LinearLayout>
<!--密码框--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="77dp"tools:layout_editor_absoluteY="222dp" /><EditTextandroid:id="@+id/editPassword"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="请输入用户密码"android:inputType="textPassword"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="208dp" /></LinearLayout><!--登录按钮--><Buttonandroid:id="@+id/loginButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#1781EB"android:text="登录"android:theme="@style/Theme.Design.NoActionBar"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="378dp"android:layout_gravity="center"/></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

xml%E4%BB%A3%E7%A0%81">2---activity_login_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".LoginInfo"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="用户信息"android:gravity="center"android:textSize="20sp"android:textColor="#F6F6F6"tools:ignore="MissingConstraints"android:background="#02D8F4"android:padding="20sp"/><!--中间布局--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"android:layout_marginTop="60dp"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="77dp"tools:layout_editor_absoluteY="149dp" /><EditTextandroid:id="@+id/editNameInput"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="用户名"android:inputType="text"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="136dp"android:enabled="false"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="77dp"tools:layout_editor_absoluteY="222dp" /><EditTextandroid:id="@+id/editPasswordInput"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="密码"android:inputType="textPassword"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="208dp"android:enabled="false"/></LinearLayout><Buttonandroid:id="@+id/loginAlterButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#1781EB"android:text="修改"android:theme="@style/Theme.Design.NoActionBar"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="378dp"android:gravity="center"android:layout_gravity="center"android:layout_marginTop="200dp"/></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

xml%E4%BB%A3%E7%A0%81">3---activity_alter_info.xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".AlterInfo"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="用户信息修改"android:gravity="center"android:textSize="20sp"android:textColor="#F6F6F6"tools:ignore="MissingConstraints"android:background="#E9D30B"android:padding="20sp"/><!--控件--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"android:layout_marginTop="60dp"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="77dp"tools:layout_editor_absoluteY="149dp" /><EditTextandroid:id="@+id/editAlterNameInput"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="用户名"android:inputType="text"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="136dp"android:enabled="true"/></LinearLayout><!--控件--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="77dp"tools:layout_editor_absoluteY="222dp" /><EditTextandroid:id="@+id/editAlterPasswordInput"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="密码"android:inputType="text"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="208dp"android:enabled="true"/></LinearLayout><Buttonandroid:id="@+id/loginAlterButtonInfo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#1781EB"android:text="点击修改"android:theme="@style/Theme.Design.NoActionBar"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="145dp"tools:layout_editor_absoluteY="378dp"android:layout_marginTop="50dp"android:layout_gravity="center"/></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

xml">4.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"><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.MyApplication123"tools:targetApi="31"><!--配置LoginInfo--><activityandroid:name=".LoginInfo"android:exported="false" /><!----><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!--配置隐式意图--><activityandroid:name=".AlterInfo"android:exported="true"><!--这里面的活动名字要和action名字一样--><intent-filter><action android:name="com.xiji.myapplication123.ALTER_INFO" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity></application></manifest>

 

java%E4%BB%A3%E7%A0%81">2--java代码

1---MainActivity
java">package com.xiji.myapplication123;import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;public class MainActivity extends AppCompatActivity {//获取控件private EditText username;private EditText password;private Button loginButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});//控件初始化initView();//事件绑定initListener();}//控件初始化private void initView() {username = findViewById(R.id.editName);password = findViewById(R.id.editPassword);loginButton = findViewById(R.id.loginButton);}//事件绑定private void initListener() {loginButton.setOnClickListener(v -> {String user = username.getText().toString();String pwd = password.getText().toString();//显示意图Intent intent = new Intent(MainActivity.this, LoginInfo.class);//传递输入的数据if (user.equals("") || pwd.equals("")) {Toast.makeText(MainActivity.this, "账户和密码不能为空", Toast.LENGTH_SHORT);return;}intent.putExtra("username", user);intent.putExtra("password", pwd);startActivity(intent);Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();});}
}

2---LoginInfo
java">package com.xiji.myapplication123;import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;public class LoginInfo extends AppCompatActivity {//获取展示控件,并且把信息展示private EditText username;private EditText password;//修改按钮private Button alterButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_login_info);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});//控件初始化initView();//监听初始化initListener();//初始化数据initData();}//控件初始化private void initView(){username = findViewById(R.id.editNameInput);password = findViewById(R.id.editPasswordInput);alterButton = findViewById(R.id.loginAlterButton);}private void initListener(){alterButton.setOnClickListener(v -> {//Bundle传递信息Bundle bundle = new Bundle();//创建意图Intent intent = new Intent();//隐式传递intent.setClass(this, AlterInfo.class);intent.setAction("com.xiji.myapplication123.ALTER_INFO");//传递数据bundle.putString("username", username.getText().toString());bundle.putString("password", password.getText().toString());intent.putExtras(bundle);//启动//发送请求  如果要获取返回结果,必须要带请求码startActivityForResult(intent, 100);});}/**** 接受数据,数据传递*/private void initData(){Intent intent = getIntent();if(intent != null){Bundle bundle = intent.getExtras();username.setText(bundle.getString("username"));password.setText(bundle.getString("password"));}Toast.makeText(this, "数据已接收", Toast.LENGTH_SHORT);}/*** 数据回传监听*/@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);System.out.println("接受回传"+data);Toast.makeText(this, "接受回传", Toast.LENGTH_SHORT);//根据返回码不同做出不同的效果//接受请求if (resultCode == 200 && requestCode == 100) {//获取Bundle数据Bundle bundle = data.getExtras();//修改成功username.setText(bundle.getString("username"));password.setText(bundle.getString("password"));Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT);return;}//修改失败Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT);}
}

3---AlterInfo
java">package com.xiji.myapplication123;import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;public class AlterInfo extends AppCompatActivity {//控件初始化private EditText alter_name;private EditText alter_password;//按钮private Button alter_button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_alter_info);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});//控件初始化initView();//事件绑定initListener();//获取数据initData();}//控件初始化private void initView(){alter_name = findViewById(R.id.editAlterNameInput);alter_password = findViewById(R.id.editAlterPasswordInput);alter_button = findViewById(R.id.loginAlterButtonInfo);}//按钮监听并且回传private void initListener(){alter_button.setOnClickListener(v -> {//回传数据Bundle bundle = new Bundle();bundle.putString("username", alter_name.getText().toString());bundle.putString("password", alter_password.getText().toString());//设置回传数据//通过意图返回Intent intent = new Intent(AlterInfo.this, LoginInfo.class);intent.putExtras(bundle);setResult(200, intent);Toast.makeText(this, "userName:: "+alter_name.getText().toString()+"\npassword::"+alter_password.getText().toString(), Toast.LENGTH_SHORT).show();//数据销毁finish();});}//获取数据接受public void initData(){Intent intent = getIntent();if(intent != null){Bundle bundle = intent.getExtras();alter_name.setText(bundle.getString("username"));alter_password.setText(bundle.getString("password"));}}
}

2.效果


http://www.ppmy.cn/devtools/125389.html

相关文章

PyCharm打开及配置现有工程(详细图解)

本文详细介绍了如何利用Pycharm打开一个现有的工程&#xff0c;其中包括编译器的配置。 PyCharm打开及配置现有工程 1、打开工程2、配置编译器 1、打开工程 双击PyCharm软件&#xff0c;点击左上角 文件 >> 打开(O)… 选中想要打开的项目之后点击“确定” 2、配置编译器…

【进阶OpenCV】 (12)--人脸检测识别

文章目录 人脸识别一、获取分类器二、代码实现1. 图片预处理2. 加载人脸检测分类器3. 检测人脸4. 标注人脸 总结 人脸识别 要实现人脸识别首先要判断当前图像中是否出现了人脸&#xff0c;这就是人脸检测。只有检测到图像中出现了人脸&#xff0c;才能据此判断这个人到底是谁。…

实时语音转文字(基于NAudio+Whisper+VOSP+Websocket)

今天花了大半天时间研究一个实时语音转文字的程序&#xff0c;目的还包括能够唤醒服务&#xff0c;并把命令提供给第三方。 由于这方面的材料已经很多&#xff0c;我就只把过程中遇到的和解决方案简单说下。源代码开源在AudioWhisper: 实时语音转文字(基于NAudioWhisperVOSPWe…

解决新版Android studio不能连接手机的问题

我要说的是一个特例&#xff0c;装了22年的版本AS可以正常连接手机&#xff0c;装了23年以后新版本&#xff0c;AS不能正常连接手机了&#xff0c;但是在CMD控制台可以正常的执行adb命令&#xff0c;并且CMD和AS都是指向D:\android_sdk\platform-tools\adb.exe 一、 为什么会出…

应对网站IP劫持的有效策略与技术手段

摘要&#xff1a; IP劫持是一种常见的网络攻击方式&#xff0c;攻击者通过非法手段获取目标网站服务器的控制权&#xff0c;进而改变其网络流量的路由路径&#xff0c;导致用户访问错误的站点。本文将介绍如何识别IP劫持&#xff0c;并提供一系列预防和应对措施&#xff0c;以确…

基于Android11简单分析audio_policy_configuration.xml

开篇先贴上一个高通的例子&#xff0c;后续基于此文件做具体分析。 1 <?xml version"1.0" encoding"UTF-8" standalone"yes"?> 2 <!-- Copyright (c) 2016-2019, The Linux Foundation. All rights reserved 3 Not a Contribut…

C# 字符串(string)三个不同的处理方法:IsNullOrEmpty、IsInterned 、IsNullOrWhiteSpace

在C#中&#xff0c;string.IsNullOrEmpty、string.IsInterned 和 string.IsNullOrWhiteSpace 是三个不同的字符串处理方法&#xff0c;它们各自有不同的用途&#xff1a; 1.string.IsNullOrEmpty&#xff1a; 这个方法用来检查字符串是否为null或者空字符串&#xff08;"…

harbor 如何做到物理删除镜像 harbor镜像清理脚本

一、背景 相比于nexus&#xff0c;harbor的一大优点是方便及时清理无用的docker镜像。本文就harbor怎么设置清理&#xff0c;梳理一下具体的操作办法。 harbor 版本是 v2.9.0 二、目标 随着我们推送至仓库的镜像越来越多&#xff0c;带来的一个最大运维问题就是存储空间的浪…