实验四 组件通信

实验四 组件通信

实验目的

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

实验要求

1.运行课本的示例程序,理解组件通信的方式和过程
2.设计一个主Activity和一个子Activity(Sub-Activity),使用主Activity上的按钮启动子Activity,并将子Activity的一些信息返回给主Activity,并显示在主Activity上。

可以自己设计界面和场景,也可以使用下面提供的内容:

主Activity界面上有一个“登录”按钮和一个用了显示信息的TextView,点击“登录”按钮后打开一个新的Activity,新Activity上面有输入用户名的控件,在用户关闭这个Activity后,将用户输入的用户名到主Activity,并显示在主Activity的TextView中。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

工程代码

项目工程结构图
在这里插入图片描述

Activity:

package com.example.wyx.exp_4;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import static android.R.attr.data;

/**
 * Created by wyx on 2018/9/25.
 */

public class Activity extends AppCompatActivity {
    private static final int resultcode=1;
    TextView tv;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
           Uri uriData = data.getData();

            tv.setText("用户名:"+uriData.toString());
            		}

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);

        final Button bt=(Button)findViewById(R.id.bt);
        tv=(TextView)findViewById(R.id.textView);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Activity.this,SubActivity.class);
                startActivityForResult(intent,resultcode);

            }
        });

    }
}

SubActivity:

package com.example.wyx.exp_4;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by wyx on 2018/9/25.
 */

public class SubActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);
        final Button return2=(Button)findViewById(R.id.return2);
        final EditText name=(EditText)findViewById(R.id.name);

        return2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=name.getText().toString();
                Uri data = Uri.parse(str);
                Intent result=new Intent(null,data);
                setResult(RESULT_OK,result);
                finish();

            }
        });
    }
}

layout1.xml:

<?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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="118dp"
            android:text="登录" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/bt"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="92dp"
            android:text="用户名:" />
    </RelativeLayout>
</LinearLayout>


layout2.xml:

<?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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="83dp"
            android:layout_marginTop="127dp"
            android:text="用户名:" />

        <EditText
            android:id="@+id/name"
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_marginStart="11dp"
            android:layout_toEndOf="@+id/textView2" />

        <Button
            android:id="@+id/return2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="99dp"
            android:text="登录" />

    </RelativeLayout>
</LinearLayout>

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.wyx.exp_4">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Activity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SubActivity"
            android:label="@string/app_name">
        </activity>

    </application>

</manifest>

实现效果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39699765/article/details/84311761