zoukankan      html  css  js  c++  java
  • 【Android实验】组件通信Intent

    实验目的

    1. 了解使用Intent进行组件通信原理

    2. 掌握使用Intent启动Activity的方法

    3. 熟悉和掌握Android组件间通信的方式和技巧

    实验要求

    设计一个主Activity和一个子Activity(Sub-Activity),使用主Activity上的按钮启动子Activity,并将子Activity的一些信息返回给主Activity,并显示在主Activity上。

    graph LR Activity --1.start--> Sub-Activity Sub-Activity --2.return&show--> Activity

    场景解释:

    输入当前用户名->确认->输入新的用户名->确认->返回原始界面

    graph LR 请输入当前的用户名 --1.确认--> 请输入新的用户名 请输入新的用户名 --2.输入并确认--> 请输入当前的用户名 请输入新的用户名 --3.返回--> 请输入当前的用户名

    实验结果

    进入用户主界面
    点击登录以后输入新的名字
    点击确认以后

    实验代码

    father.xml:主界面文件
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Space
            android:layout_width="match_parent"
            android:layout_height="150dp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="欢迎你,"
                android:textSize="20dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:text="请输入用户名!"
                android:id="@+id/tv_resetName"/>
        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名:"
                android:textSize="20dp"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/et_name"
                />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Space
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="登录"
                android:textSize="15dp"
                android:layout_weight="1"
                android:id="@+id/btn_login"
                />
        </LinearLayout>
    </LinearLayout>
    
    content_child.xml:子界面
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Space
            android:layout_width="match_parent"
            android:layout_height="150dp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="欢迎你,"
                android:textSize="20dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:text="请输入用户名!"
                android:id="@+id/tv_resetName"/>
        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名:"
                android:textSize="20dp"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/et_name"
                />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Space
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="登录"
                android:textSize="15dp"
                android:layout_weight="1"
                android:id="@+id/btn_login"
                />
        </LinearLayout>
    </LinearLayout>
    
    Fourth.java: 处理文件
    package com.example.administrator.fourth;
    
    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class Fourth extends AppCompatActivity {
    
        private static final int SUBACTIVITY1 = 1;
        TextView tv_resetName;
        Button btn_login;
        EditText et_name;
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == SUBACTIVITY1)
            {
                if(resultCode == RESULT_OK)
                {
                    Uri uriData = data.getData();
                    tv_resetName.setText(uriData.toString());
                }
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.father);
    
            tv_resetName = (TextView)findViewById(R.id.tv_resetName);
            btn_login = (Button)findViewById(R.id.btn_login);
            et_name = (EditText)findViewById(R.id.et_name);
    
            btn_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Fourth.this,ChildActivity.class);
                    intent.putExtra("Name",et_name.getText().toString());
                    startActivityForResult(intent,SUBACTIVITY1);
                }
            });
        }
    }
    
    ChildActivity.java: 子界面处理文件
    package com.example.administrator.fourth;
    
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class ChildActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_child);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            final EditText et_resetName = (EditText)findViewById(R.id.et_resetName);
            Button btn_sure = (Button)findViewById(R.id.btn_sure);
            TextView tv_newUser = (TextView)findViewById(R.id.tv_newUser);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
    
            Intent data = getIntent();
            tv_newUser.setText(data.getStringExtra("Name"));
    
    
            btn_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String uriString = et_resetName.getText().toString();
                    Uri data = Uri.parse(uriString);
                    Intent result = new Intent(null,data);
                    setResult(RESULT_OK,result);
                    finish();
                }
            });
        }
    }
    

    实验总结

    通过本次实验,主要是熟悉了Intent的用法,Intent可以看做一封信,用来解决Android应用的各组件之间的通信,他负责对应用中动作,动作涉及的数据,附加数据进行描述,Android则负责根据描述,找到对应的组件,将Intent传递到被调用的组件,并完成组件的调用。

    • 父Activity获得子activity分为三步:
    1. 在父activity中的按钮监听器中显示启动子activity

      Intent intent = new Intent(MainActivity.this,SubActivity.class);
      startActivityForResult(intent,SUBACTIVITY1); //SUBACTIVITY1是该activity的静态标志
      
    2. 在子activity调用finish关闭之前调用setResult返回

      其中需要返回内容对其进行包装

      String uriString = et_resetName.getText().toString();
      Uri data = Uri.parse(uriString);
      Intent result = new Intent(null,data);
      setResult(RESULT_OK,result);
      finish();
      

      通用资源标志符(Universal Resource Identifier, 简称"URI")。
      Uri代表要操作的数据,Android上可用的每种资源 - 图像、视频片段等都可以用Uri来表示,我们很经常需要解析Uri,并从Uri中获取数据。setResult函数可以设置结果码和返回值,传递给父activity

    3. 在父activity中写onActivityResult函数获取返回值

      if(requestCode == SUBACTIVITY1)
      {
           if(resultCode == RESULT_OK)
           {
                Uri uriData = data.getData();
                tv_resetName.setText(uriData.toString());
           }
      }
      
    • 子activity收到父activity

      1. 父activity中的处理
      Intent intent = new Intent(Fourth.this,ChildActivity.class);
      intent.putExtra("Name",et_name.getText().toString());
      startActivityForResult(intent,SUBACTIVITY1);
      
      1. 子activity中的处理
      Intent data = getIntent();
      tv_newUser.setText(data.getStringExtra("Name"));
      
  • 相关阅读:
    896. Monotonic Array单调数组
    865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树
    489. Robot Room Cleaner扫地机器人
    JavaFX
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
  • 原文地址:https://www.cnblogs.com/pprp/p/9874623.html
Copyright © 2011-2022 走看看