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

打印 上一主题 下一主题

主题 1379|帖子 1379|积分 4137

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
目次
  
  
  1.Activity的生命周期
  核心生命周期回调
  1)onCreate()
  2)onStart()
  3)onResume()
  4)onPause()
  5)onStop()
  6)onRestart()
  7)onDestroy()
  8)生命周期图示
  10)留意事项
  2.怎样创建一个Activity
  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.代码
  1--xml代码
  1---activity_main.xml代码
  2---activity_login_main.xml代码
  3---activity_alter_info.xml代码
  4.AndroidManifest.xml
  2--java代码
  1---MainActivity
  2---LoginInfo
  3---AlterInfo
  2.效果
  
  
  

1.Activity的生命周期

核心生命周期回调

1)onCreate()



  • 当 Activity 被创建时调用
  • 这是初始化组件的最佳机遇,比如设置布局、绑定命据等。
  • 示例代码:
    1. @Override
    2. protected void onCreate(Bundle savedInstanceState) {
    3.     super.onCreate(savedInstanceState);
    4.     setContentView(R.layout.activity_main);
    5.     // 初始化其他组件...
    6. }
    复制代码
2)onStart()



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



  • 当 Activity 开始与用户交互并获得核心时调用
  • 此时 Activity 处于活动状态,并可以响应用户的输入。
示例代码:
  1. @Override
  2. protected void onResume() {
  3.     super.onResume();
  4.     // 恢复暂停的操作,如开始传感器监听...
  5. }
复制代码
4)onPause()



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



  • 当 Activity 对用户完全不可见时调用。
  • 在这个阶段,Activity 已经不再表现给用户,但仍驻留在内存中
  • 示例代码:
  1. @Override
  2. protected void onStop() {
  3.     super.onStop();
  4.     // 更多清理工作...
  5. }
复制代码
6)onRestart()



  • 当 Activity 从停止状态重新启动时调用
  • 它总是在 onStart() 方法之前调用。
  • 示例代码:
  1. @Override
  2. protected void onRestart() {
  3.     super.onRestart();
  4.     // 重启时的处理逻辑...
  5. }
复制代码
7)onDestroy()



  • 当 Activity 即将被烧毁时调用
  • 在这里进行最后的清算工作,比如取消网络请求、关闭数据库毗连等。
  • 示例代码:
  1. @Override
  2. protected void onDestroy() {
  3.     super.onDestroy();
  4.     // 清理所有剩余资源...
  5. }
复制代码
8)生命周期图示

  1. +---------------------+
  2. |      onCreate()     |
  3. +---------------------+
  4.           |
  5.           v
  6. +---------------------+
  7. |      onStart()      |
  8. +---------------------+
  9.           |
  10.           v
  11. +---------------------+
  12. |     onResume()      |
  13. +---------------------+
  14.           |
  15.           v
  16. +---------------------+  +---------------------+
  17. |                      |  |    用户交互...     |
  18. |    Activity活跃      |<->|                    |
  19. |                      |  |                    |
  20. +---------------------+  +---------------------+
  21.           |
  22.           v
  23. +---------------------+
  24. |     onPause()       |
  25. +---------------------+
  26.           |
  27.           v
  28. +---------------------+
  29. |      onStop()       |
  30. +---------------------+
  31.           |
  32.           v
  33. +---------------------+
  34. |      onDestroy()    |
  35. +---------------------+
复制代码
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


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>
  清单文件全部内容
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools">
  4.     <application
  5.         android:allowBackup="true"
  6.         android:dataExtractionRules="@xml/data_extraction_rules"
  7.         android:fullBackupContent="@xml/backup_rules"
  8.         android:icon="@mipmap/ic_launcher"
  9.         android:label="@string/app_name"
  10.         android:roundIcon="@mipmap/ic_launcher_round"
  11.         android:supportsRtl="true"
  12.         android:theme="@style/Theme.MyApplication123"
  13.         tools:targetApi="31">
  14.         <!--配置LoginInfo-->
  15.         <activity
  16.             android:name=".LoginInfo"
  17.             android:exported="false" />
  18.         <!---->
  19.         <activity
  20.             android:name=".MainActivity"
  21.             android:exported="true">
  22.             <intent-filter>
  23.                 <action android:name="android.intent.action.MAIN" />
  24.                 <category android:name="android.intent.category.LAUNCHER" />
  25.             </intent-filter>
  26.         </activity>
  27.         <!--配置隐式意图-->
  28.         <activity
  29.             android:name=".AlterInfo"
  30.             android:exported="true">
  31.             
  32.             <intent-filter>
  33.                 <action android:name="com.xiji.myapplication123.ALTER_INFO" />
  34.                 <category android:name="android.intent.category.DEFAULT" />
  35.             </intent-filter>
  36.         </activity>
  37.     </application>
  38. </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

  1. // 显式 Intent - 直接指定目标组件
  2. Intent explicitIntent = new Intent(context, TargetActivity.class);
  3. // 隐式 Intent - 通过 Action 和 Category 来匹配合适的组件
  4. Intent implicitIntent = new Intent();
  5. implicitIntent.setAction(Intent.ACTION_VIEW);
  6. implicitIntent.setData(Uri.parse("http://www.example.com"));
复制代码
3-添加额外数据

  1. Intent intent = new Intent(this, TargetActivity.class);
  2. intent.putExtra("key", "value");
  3. startActivity(intent);
复制代码
4-接收额外数据

  1. Intent intent = getIntent();
  2. String value = intent.getStringExtra("key");
复制代码
2)IntentFilter

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



  • 在 Manifest 文件中声明
    1. <activity android:name=".TargetActivity">
    2.     <intent-filter>
    3.         <action android:name="android.intent.action.VIEW" />
    4.         <category android:name="android.intent.category.DEFAULT" />
    5.         <data android:scheme="http" />
    6.     </intent-filter>
    7. </activity>
    复制代码
  • 在代码中动态创建
    1. IntentFilter filter = new IntentFilter();
    2. filter.addAction("com.example.MY_ACTION");
    3. registerReceiver(myBroadcastReceiver, filter);
    复制代码




2-组成部门

1--Action

        表示 Intent 的动作,比方 ACTION_VIEW, ACTION_SEND 等。
2--Category

        提供 Intent 的附加信息,比如 CATEGORY_DEFAULT, CATEGORY_BROWSABLE 等。
3--Data

        指定 Intent 操作的数据和数据类型,比方 URI 和 MIME 类型。


3-匹配规则

当系统实验找到能够响应给定 Intent 的组件时,会根据 Intent 和 IntentFilter 的以下属性进行匹配:
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()方法;
  
  1. @Override
  2. protected void onActivityResult(int <strong>requestCode</strong>, int <strong>resultCode</strong>, @Nullable Intent <strong>data</strong>) {
  3.     super.onActivityResult(requestCode, resultCode, data);
  4. }
复制代码

案例:模仿登录修改

流程图:

1.代码

1--xml代码

1---activity_main.xml代码

  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/main"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context=".MainActivity">
  9.     <LinearLayout
  10.         android:layout_width="match_parent"
  11.         android:layout_height="match_parent"
  12.         android:orientation="vertical"
  13.         >
  14.         <TextView
  15.             android:layout_width="match_parent"
  16.             android:layout_height="wrap_content"
  17.             android:text="用户登录"
  18.             android:gravity="center"
  19.             android:textSize="20sp"
  20.             android:textColor="#F6F6F6"
  21.             tools:ignore="MissingConstraints"
  22.             android:background="#46E90B"
  23.             android:padding="20sp"
  24.             />
  25.         <!--用户框-->
  26.     <LinearLayout
  27.         android:layout_width="match_parent"
  28.         android:layout_height="wrap_content"
  29.         android:orientation="horizontal"
  30.         android:gravity="center"
  31.         android:layout_marginTop="60dp"
  32.         >
  33.         <TextView
  34.             android:id="@+id/textView"
  35.             android:layout_width="wrap_content"
  36.             android:layout_height="wrap_content"
  37.             android:text="用户名"
  38.             tools:ignore="MissingConstraints"
  39.             tools:layout_editor_absoluteX="77dp"
  40.             tools:layout_editor_absoluteY="149dp" />
  41.         <EditText
  42.             android:id="@+id/editName"
  43.             android:layout_width="wrap_content"
  44.             android:layout_height="wrap_content"
  45.             android:ems="10"
  46.             android:hint="输入用户名"
  47.             android:inputType="text"
  48.             tools:ignore="MissingConstraints"
  49.             tools:layout_editor_absoluteX="145dp"
  50.             tools:layout_editor_absoluteY="136dp" />
  51.     </LinearLayout>
  52. <!--密码框-->
  53.     <LinearLayout
  54.         android:layout_width="match_parent"
  55.         android:layout_height="wrap_content"
  56.         android:orientation="horizontal"
  57.         android:gravity="center"
  58.         >
  59.         <TextView
  60.             android:id="@+id/textView2"
  61.             android:layout_width="wrap_content"
  62.             android:layout_height="wrap_content"
  63.             android:text="密 码"
  64.             tools:ignore="MissingConstraints"
  65.             tools:layout_editor_absoluteX="77dp"
  66.             tools:layout_editor_absoluteY="222dp" />
  67.         <EditText
  68.             android:id="@+id/editPassword"
  69.             android:layout_width="wrap_content"
  70.             android:layout_height="wrap_content"
  71.             android:ems="10"
  72.             android:hint="请输入用户密码"
  73.             android:inputType="textPassword"
  74.             tools:ignore="MissingConstraints"
  75.             tools:layout_editor_absoluteX="145dp"
  76.             tools:layout_editor_absoluteY="208dp" />
  77.     </LinearLayout>
  78.         <!--登录按钮-->
  79.         <Button
  80.             android:id="@+id/loginButton"
  81.             android:layout_width="wrap_content"
  82.             android:layout_height="wrap_content"
  83.             android:background="#1781EB"
  84.             android:text="登录"
  85.             android:theme="@style/Theme.Design.NoActionBar"
  86.             tools:ignore="MissingConstraints"
  87.             tools:layout_editor_absoluteX="145dp"
  88.             tools:layout_editor_absoluteY="378dp"
  89.             android:layout_gravity="center"
  90.             />
  91.     </LinearLayout>
  92. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码

  

2---activity_login_main.xml代码

  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/main"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context=".LoginInfo">
  9.     <LinearLayout
  10.         android:layout_width="match_parent"
  11.         android:layout_height="match_parent"
  12.         android:orientation="vertical"
  13.         >
  14.         <TextView
  15.             android:layout_width="match_parent"
  16.             android:layout_height="wrap_content"
  17.             android:text="用户信息"
  18.             android:gravity="center"
  19.             android:textSize="20sp"
  20.             android:textColor="#F6F6F6"
  21.             tools:ignore="MissingConstraints"
  22.             android:background="#02D8F4"
  23.             android:padding="20sp"
  24.             />
  25.         <!--中间布局-->
  26.     <LinearLayout
  27.         android:layout_width="match_parent"
  28.         android:layout_height="wrap_content"
  29.         android:orientation="horizontal"
  30.         android:gravity="center"
  31.         android:layout_marginTop="60dp"
  32.         >
  33.         <TextView
  34.             android:id="@+id/textView"
  35.             android:layout_width="wrap_content"
  36.             android:layout_height="wrap_content"
  37.             android:text="用户名"
  38.             tools:ignore="MissingConstraints"
  39.             tools:layout_editor_absoluteX="77dp"
  40.             tools:layout_editor_absoluteY="149dp" />
  41.         <EditText
  42.             android:id="@+id/editNameInput"
  43.             android:layout_width="wrap_content"
  44.             android:layout_height="wrap_content"
  45.             android:ems="10"
  46.             android:hint="用户名"
  47.             android:inputType="text"
  48.             tools:ignore="MissingConstraints"
  49.             tools:layout_editor_absoluteX="145dp"
  50.             tools:layout_editor_absoluteY="136dp"
  51.             android:enabled="false"
  52.             />
  53.     </LinearLayout>
  54.         <LinearLayout
  55.             android:layout_width="match_parent"
  56.             android:layout_height="wrap_content"
  57.             android:orientation="horizontal"
  58.             android:gravity="center"
  59.             >
  60.             <TextView
  61.                 android:id="@+id/textView2"
  62.                 android:layout_width="wrap_content"
  63.                 android:layout_height="wrap_content"
  64.                 android:text="密 码"
  65.                 tools:ignore="MissingConstraints"
  66.                 tools:layout_editor_absoluteX="77dp"
  67.                 tools:layout_editor_absoluteY="222dp" />
  68.             <EditText
  69.                 android:id="@+id/editPasswordInput"
  70.                 android:layout_width="wrap_content"
  71.                 android:layout_height="wrap_content"
  72.                 android:ems="10"
  73.                 android:hint="密码"
  74.                 android:inputType="textPassword"
  75.                 tools:ignore="MissingConstraints"
  76.                 tools:layout_editor_absoluteX="145dp"
  77.                 tools:layout_editor_absoluteY="208dp"
  78.                 android:enabled="false"
  79.                 />
  80.         </LinearLayout>
  81.         <Button
  82.             android:id="@+id/loginAlterButton"
  83.             android:layout_width="wrap_content"
  84.             android:layout_height="wrap_content"
  85.             android:background="#1781EB"
  86.             android:text="修改"
  87.             android:theme="@style/Theme.Design.NoActionBar"
  88.             tools:ignore="MissingConstraints"
  89.             tools:layout_editor_absoluteX="145dp"
  90.             tools:layout_editor_absoluteY="378dp"
  91.             android:gravity="center"
  92.             android:layout_gravity="center"
  93.             android:layout_marginTop="200dp"
  94.             />
  95.     </LinearLayout>
  96. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码

  

3---activity_alter_info.xml代码

  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/main"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context=".AlterInfo">
  9.     <LinearLayout
  10.         android:layout_width="match_parent"
  11.         android:layout_height="match_parent"
  12.         android:orientation="vertical"
  13.         >
  14.         <TextView
  15.             android:layout_width="match_parent"
  16.             android:layout_height="wrap_content"
  17.             android:text="用户信息修改"
  18.             android:gravity="center"
  19.             android:textSize="20sp"
  20.             android:textColor="#F6F6F6"
  21.             tools:ignore="MissingConstraints"
  22.             android:background="#E9D30B"
  23.             android:padding="20sp"
  24.             />
  25.         <!--控件-->
  26.         <LinearLayout
  27.             android:layout_width="match_parent"
  28.             android:layout_height="wrap_content"
  29.             android:orientation="horizontal"
  30.             android:gravity="center"
  31.             android:layout_marginTop="60dp"
  32.             >
  33.             <TextView
  34.                 android:id="@+id/textView"
  35.                 android:layout_width="wrap_content"
  36.                 android:layout_height="wrap_content"
  37.                 android:text="用户名"
  38.                 tools:ignore="MissingConstraints"
  39.                 tools:layout_editor_absoluteX="77dp"
  40.                 tools:layout_editor_absoluteY="149dp" />
  41.             <EditText
  42.                 android:id="@+id/editAlterNameInput"
  43.                 android:layout_width="wrap_content"
  44.                 android:layout_height="wrap_content"
  45.                 android:ems="10"
  46.                 android:hint="用户名"
  47.                 android:inputType="text"
  48.                 tools:ignore="MissingConstraints"
  49.                 tools:layout_editor_absoluteX="145dp"
  50.                 tools:layout_editor_absoluteY="136dp"
  51.                 android:enabled="true"
  52.                 />
  53.         </LinearLayout>
  54.         <!--控件-->
  55.         <LinearLayout
  56.             android:layout_width="match_parent"
  57.             android:layout_height="wrap_content"
  58.             android:orientation="horizontal"
  59.             android:gravity="center"
  60.             >
  61.             <TextView
  62.                 android:id="@+id/textView2"
  63.                 android:layout_width="wrap_content"
  64.                 android:layout_height="wrap_content"
  65.                 android:text="密 码"
  66.                 tools:ignore="MissingConstraints"
  67.                 tools:layout_editor_absoluteX="77dp"
  68.                 tools:layout_editor_absoluteY="222dp" />
  69.             <EditText
  70.                 android:id="@+id/editAlterPasswordInput"
  71.                 android:layout_width="wrap_content"
  72.                 android:layout_height="wrap_content"
  73.                 android:ems="10"
  74.                 android:hint="密码"
  75.                 android:inputType="text"
  76.                 tools:ignore="MissingConstraints"
  77.                 tools:layout_editor_absoluteX="145dp"
  78.                 tools:layout_editor_absoluteY="208dp"
  79.                 android:enabled="true"
  80.                 />
  81.         </LinearLayout>
  82.         <Button
  83.             android:id="@+id/loginAlterButtonInfo"
  84.             android:layout_width="wrap_content"
  85.             android:layout_height="wrap_content"
  86.             android:background="#1781EB"
  87.             android:text="点击修改"
  88.             android:theme="@style/Theme.Design.NoActionBar"
  89.             tools:ignore="MissingConstraints"
  90.             tools:layout_editor_absoluteX="145dp"
  91.             tools:layout_editor_absoluteY="378dp"
  92.             android:layout_marginTop="50dp"
  93.             android:layout_gravity="center"
  94.             />
  95.     </LinearLayout>
  96. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码

  

4.AndroidManifest.xml

  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools">
  4.     <application
  5.         android:allowBackup="true"
  6.         android:dataExtractionRules="@xml/data_extraction_rules"
  7.         android:fullBackupContent="@xml/backup_rules"
  8.         android:icon="@mipmap/ic_launcher"
  9.         android:label="@string/app_name"
  10.         android:roundIcon="@mipmap/ic_launcher_round"
  11.         android:supportsRtl="true"
  12.         android:theme="@style/Theme.MyApplication123"
  13.         tools:targetApi="31">
  14.         <!--配置LoginInfo-->
  15.         <activity
  16.             android:name=".LoginInfo"
  17.             android:exported="false" />
  18.         <!---->
  19.         <activity
  20.             android:name=".MainActivity"
  21.             android:exported="true">
  22.             <intent-filter>
  23.                 <action android:name="android.intent.action.MAIN" />
  24.                 <category android:name="android.intent.category.LAUNCHER" />
  25.             </intent-filter>
  26.         </activity>
  27.         <!--配置隐式意图-->
  28.         <activity
  29.             android:name=".AlterInfo"
  30.             android:exported="true">
  31.             <!--这里面的活动名字要和action名字一样-->
  32.             <intent-filter>
  33.                 <action android:name="com.xiji.myapplication123.ALTER_INFO" />
  34.                 <category android:name="android.intent.category.DEFAULT" />
  35.             </intent-filter>
  36.         </activity>
  37.     </application>
  38. </manifest>
复制代码

   

2--java代码

1---MainActivity

  
  1. package com.xiji.myapplication123;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.EditText;
  6. import android.widget.Toast;
  7. import androidx.activity.EdgeToEdge;
  8. import androidx.annotation.Nullable;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.core.graphics.Insets;
  11. import androidx.core.view.ViewCompat;
  12. import androidx.core.view.WindowInsetsCompat;
  13. public class MainActivity extends AppCompatActivity {
  14.     //获取控件
  15.     private EditText username;
  16.     private EditText password;
  17.     private Button loginButton;
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         EdgeToEdge.enable(this);
  22.         setContentView(R.layout.activity_main);
  23.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  24.             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  25.             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  26.             return insets;
  27.         });
  28.         //控件初始化
  29.         initView();
  30.         //事件绑定
  31.         initListener();
  32.     }
  33.     //控件初始化
  34.     private void initView() {
  35.         username = findViewById(R.id.editName);
  36.         password = findViewById(R.id.editPassword);
  37.         loginButton = findViewById(R.id.loginButton);
  38.     }
  39.     //事件绑定
  40.     private void initListener() {
  41.         loginButton.setOnClickListener(v -> {
  42.             String user = username.getText().toString();
  43.             String pwd = password.getText().toString();
  44.             //显示意图
  45.             Intent intent = new Intent(MainActivity.this, LoginInfo.class);
  46.             //传递输入的数据
  47.             if (user.equals("") || pwd.equals("")) {
  48.                 Toast.makeText(MainActivity.this, "账户和密码不能为空", Toast.LENGTH_SHORT);
  49.                 return;
  50.             }
  51.             intent.putExtra("username", user);
  52.             intent.putExtra("password", pwd);
  53.             startActivity(intent);
  54.             Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  55.         });
  56.     }
  57. }
复制代码

  


2---LoginInfo

  
  1. package com.xiji.myapplication123;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.EditText;
  6. import android.widget.Toast;
  7. import androidx.activity.EdgeToEdge;
  8. import androidx.annotation.Nullable;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.core.graphics.Insets;
  11. import androidx.core.view.ViewCompat;
  12. import androidx.core.view.WindowInsetsCompat;
  13. public class LoginInfo extends AppCompatActivity {
  14.     //获取展示控件,并且把信息展示
  15.     private EditText username;
  16.     private EditText password;
  17.     //修改按钮
  18.     private Button alterButton;
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         EdgeToEdge.enable(this);
  23.         setContentView(R.layout.activity_login_info);
  24.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  25.             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  26.             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  27.             return insets;
  28.         });
  29.         //控件初始化
  30.         initView();
  31.         //监听初始化
  32.         initListener();
  33.         //初始化数据
  34.         initData();
  35.     }
  36.     //控件初始化
  37.     private void initView(){
  38.         username = findViewById(R.id.editNameInput);
  39.         password = findViewById(R.id.editPasswordInput);
  40.         alterButton = findViewById(R.id.loginAlterButton);
  41.     }
  42.     private void initListener(){
  43.         alterButton.setOnClickListener(v -> {
  44.             //Bundle传递信息
  45.             Bundle bundle = new Bundle();
  46.             //创建意图
  47.             Intent intent = new Intent();
  48.             //隐式传递
  49.             intent.setClass(this, AlterInfo.class);
  50.             intent.setAction("com.xiji.myapplication123.ALTER_INFO");
  51.             //传递数据
  52.             bundle.putString("username", username.getText().toString());
  53.             bundle.putString("password", password.getText().toString());
  54.             intent.putExtras(bundle);
  55.             //启动
  56.             //发送请求  如果要获取返回结果,必须要带请求码
  57.             startActivityForResult(intent, 100);
  58.         });
  59.     }
  60.     /***
  61.      * 接受数据,数据传递
  62.      */
  63.     private void initData(){
  64.         Intent intent = getIntent();
  65.         if(intent != null){
  66.             Bundle bundle = intent.getExtras();
  67.             username.setText(bundle.getString("username"));
  68.             password.setText(bundle.getString("password"));
  69.         }
  70.         Toast.makeText(this, "数据已接收", Toast.LENGTH_SHORT);
  71.     }
  72.     /**
  73.      * 数据回传监听
  74.      */
  75.     @Override
  76.     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  77.         super.onActivityResult(requestCode, resultCode, data);
  78.         System.out.println("接受回传"+data);
  79.         Toast.makeText(this, "接受回传", Toast.LENGTH_SHORT);
  80.         //根据返回码不同做出不同的效果
  81.         //接受请求
  82.         if (resultCode == 200 && requestCode == 100) {
  83.             //获取Bundle数据
  84.             Bundle bundle = data.getExtras();
  85.             //修改成功
  86.             username.setText(bundle.getString("username"));
  87.             password.setText(bundle.getString("password"));
  88.             Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT);
  89.             return;
  90.         }
  91.         //修改失败
  92.         Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT);
  93.     }
  94. }
复制代码

  

3---AlterInfo

  
  1. package com.xiji.myapplication123;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.EditText;
  6. import android.widget.Toast;
  7. import androidx.activity.EdgeToEdge;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. import androidx.core.graphics.Insets;
  10. import androidx.core.view.ViewCompat;
  11. import androidx.core.view.WindowInsetsCompat;
  12. public class AlterInfo extends AppCompatActivity {
  13.     //控件初始化
  14.     private EditText alter_name;
  15.     private EditText alter_password;
  16.     //按钮
  17.     private Button alter_button;
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         EdgeToEdge.enable(this);
  22.         setContentView(R.layout.activity_alter_info);
  23.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  24.             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  25.             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  26.             return insets;
  27.         });
  28.         //控件初始化
  29.         initView();
  30.         //事件绑定
  31.         initListener();
  32.         //获取数据
  33.         initData();
  34.     }
  35.     //控件初始化
  36.     private void initView(){
  37.         alter_name = findViewById(R.id.editAlterNameInput);
  38.         alter_password = findViewById(R.id.editAlterPasswordInput);
  39.         alter_button = findViewById(R.id.loginAlterButtonInfo);
  40.     }
  41.     //按钮监听并且回传
  42.     private void initListener(){
  43.         alter_button.setOnClickListener(v -> {
  44.             //回传数据
  45.             Bundle bundle = new Bundle();
  46.             bundle.putString("username", alter_name.getText().toString());
  47.             bundle.putString("password", alter_password.getText().toString());
  48.             //设置回传数据
  49.             //通过意图返回
  50.             Intent intent = new Intent(AlterInfo.this, LoginInfo.class);
  51.             intent.putExtras(bundle);
  52.             setResult(200, intent);
  53.             Toast.makeText(this, "userName:: "+alter_name.getText().toString()+"\npassword::"+alter_password.getText().toString(), Toast.LENGTH_SHORT).show();
  54.             //数据销毁
  55.             finish();
  56.         });
  57.     }
  58.     //获取数据接受
  59.     public void initData(){
  60.         Intent intent = getIntent();
  61.         if(intent != null){
  62.             Bundle bundle = intent.getExtras();
  63.             alter_name.setText(bundle.getString("username"));
  64.             alter_password.setText(bundle.getString("password"));
  65.         }
  66.     }
  67. }
复制代码

  

2.效果




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

光之使者

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表