Android开发作业【页面之间互相传参】

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

package com.example.study01;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivityImplicit extends AppCompatActivity {
    
    
    EditText et_msg;
    TextView tv_bottom;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_implicit);
        et_msg = findViewById(R.id.et_msg);
        tv_bottom = findViewById(R.id.tv_bottom);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    
    
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
    
    
            String username = data.getStringExtra("username");
            String password = data.getStringExtra("password");
            String sessionId = data.getStringExtra("sessionId");
            tv_bottom.setText("username: " + username +" password: " + password +" sessionId: " + sessionId );
        }
    }

    public void Search(View view) {
    
    
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(),MainActivityLogin.class);
        intent.putExtra("msg",et_msg.getText().toString());
        startActivityForResult(intent,1);
        //startActivity(intent);
    }
}
package com.example.study01;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivityLogin extends AppCompatActivity {
    
    
    TextView tv_msg;
    EditText username;
    EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_login);
//        et_msg = findViewById(R.id.et_msg);
        tv_msg = findViewById(R.id.tv_Login1);
        Intent intent = getIntent();
        tv_msg.setText("搜索内容:" + intent.getStringExtra("msg"));
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
    }

    public void Login(View view) {
    
    
        Intent intent = new Intent();
        intent.putExtra("username",username.getText().toString());
        intent.putExtra("password",password.getText().toString());
        intent.putExtra("sessionId","sad1sd4245dsf42as");
        setResult(1,intent);
        finish();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivityImplicit">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_msg"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="搜索"
        android:textSize="30sp"
        android:onClick="Search"/>
    <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="等待参数回传"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivityLogin"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_Login1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textSize="17sp"/>
    <TextView
        android:id="@+id/tv_Login2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户未登录...."/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"/>
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:"/>
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:onClick="Login">
    </Button>
</LinearLayout>

作业总结:
startActivity( ) 与startActivityForResult( )的区别
一、startActivity( ) 与startActivityForResult( )的区别
1、startActivity( )
startActivity( ) 仅仅是启动另一个Activity,他不会自定回转到原来的Activity,若是想跳回原来的页面,则必须再使用一次startActivity( )来启动原来的Activity。
2、startActivityForResult( )
可以一次性完成这项任务,当程序执行到这段代码的时候,假若从FirstActivity跳转到SecondActivity,当这个SecondActivity执行完finish()方法后,程序会自动回调FirstActivity的onActivityResult(int requestCode, int resultCode, Intent intent)方法。
所以重写该方法即可调用intent,显示参数

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    
    
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
    
    
            String username = data.getStringExtra("username");
            String password = data.getStringExtra("password");
            String sessionId = data.getStringExtra("sessionId");
            tv_bottom.setText("username: " + username +" password: " + password +" sessionId: " + sessionId );
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_51461002/article/details/127529741
今日推荐