0
点赞
收藏
分享

微信扫一扫

Android--Activity的跳转

王传学 2022-04-21 阅读 67

跳转所需的组件

Intent(意图)

简介

语法

1)创建Intent对象 

2)启动

示例

要求

创建Activity文件和布局文件

右击Activity文件所要创建的包-->New-->Activity-->Empty Activity

设置活动类文件名(Activity)以及布局文件名(Layout)

项目结构

布局文件

activity_main.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"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户姓名:"/>
        <EditText
            android:id="@+id/et_name"
            android:layout_width="200dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户密码:"/>
        <EditText
            android:id="@+id/et_pwd"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:id="@+id/bt_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户详细信息"/>
    </LinearLayout>


</LinearLayout>

activity_inforset.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"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="昵称:"/>
        <EditText
            android:id="@+id/et_nickname"
            android:layout_width="200dp"
            android:layout_height="wrap_content" />
    </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="年龄:"/>
        <EditText
            android:id="@+id/et_age"
            android:layout_width="200dp"
            android:layout_height="wrap_content" />
    </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="性别选择:"/>
        <RadioButton
            android:id="@+id/rb_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
        <RadioButton
            android:id="@+id/rb_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </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="爱好选择:"/>
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="3"
            android:id="@+id/g1_hobby"
            >
            <CheckBox
                android:id="@+id/cb1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="阅读"
                />
            <CheckBox
                android:id="@+id/cb2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="旅游"
                />
            <CheckBox
                android:id="@+id/cb3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="发呆"
                />
            <CheckBox
                android:id="@+id/cb4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="唱歌"
                />
            <CheckBox
                android:id="@+id/cb5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="编程"
                />
            <CheckBox
                android:id="@+id/cb6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="运动"
                />
        </GridLayout>
    </LinearLayout>

    <Spinner
        android:id="@+id/sp_career"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/zhiye">
    </Spinner>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:id="@+id/bt_save"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:onClick="clickSave"
            android:text="保存"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:id="@+id/bt_reset"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text="重置"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:id="@+id/bt_return"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text="返回"/>
    </LinearLayout>


</LinearLayout>

activity_show.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"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_nickname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="昵称:"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄:"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱好:"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_career"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="职业:"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/bt_sr"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="返回"
            />
    </LinearLayout>



</LinearLayout>

活动类

MainActivity

package com.star.shiyan0602;

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

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //声明
    EditText etName,etPwd;
    Button btLogin;
    TextView tvShow;
    String sName,sPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        etName =(EditText) findViewById(R.id.et_name);
        etPwd = findViewById(R.id.et_pwd);
        btLogin = findViewById(R.id.bt_login);
        tvShow = findViewById(R.id.tv_show);


        //登录
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sName =  etName.getText().toString();
                sPwd = etPwd.getText().toString();

                if (sName.equals("star") && sPwd.equals("197324")){
                    Toast.makeText(MainActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
                    //跳转
                    //1.创建Intent对象
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this,InforSetActivity.class);

                    //
                    startActivityForResult(intent,1);
                    //3.启动
                    startActivity(intent);

                }else{
                    Toast.makeText(MainActivity.this,"用户姓名或密码错误,请重新输入", Toast.LENGTH_SHORT).show();
                    etName.setText("");
                    etPwd.setText("");
                }
            }
        });
    }

    //数据回传  InforSetActivity-->MainActivity
    //3.调取方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断数据的来源
        if (requestCode==1){
            //判断结果
            if (resultCode==RESULT_OK){
                //读取数据
                String nickname = data.getStringExtra("nickname");
                String age = data.getStringExtra("age");
                String sex = data.getStringExtra("sex");
                String hobby= data.getStringExtra("hobby");
                String career = data.getStringExtra("career");


                tvShow.setText("昵称:"+nickname+"\n年龄:"+age
                        +"\n性别:"+sex+"\n爱好:"
                        +hobby+"\n职业:"+career);

            }
        }


    }
}

InforSetActivity

package com.star.shiyan0602;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

public class InforSetActivity extends AppCompatActivity {

    //声明
    EditText etNickname,etAge;

    RadioButton rMan,rWomen;

    CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
    CheckBox cb[]=new CheckBox[6];
    GridLayout g1;
    Spinner spCareer;
    Button btSave,btReset,btReturn;

    String sNickname,sAge,sSex,sHobby,sCareer;

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

        etNickname = findViewById(R.id.et_nickname);
        etAge = findViewById(R.id.et_age);

        rMan = findViewById(R.id.rb_man);
        rWomen = findViewById(R.id.rb_woman);

        g1 = findViewById(R.id.g1_hobby);

        spCareer = findViewById(R.id.sp_career);

        btSave = findViewById(R.id.bt_save);
        btReset = findViewById(R.id.bt_reset);
        btReturn = findViewById(R.id.bt_return);

        //保存
        btSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建意图
                Intent intent = new Intent();
                intent.setClass(InforSetActivity.this,ShowActivity.class);
                //获取数据
                get_data();
                //写入数据
                intent.putExtra("nickname",sNickname);
                intent.putExtra("age",sAge);
                intent.putExtra("hobby",sHobby);
                intent.putExtra("sex",sSex);
                intent.putExtra("career",sCareer);

                //启动
                startActivity(intent);

            }
        });

        //重置
        btReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNickname.setText("");
                etAge.setText("");

                rMan.setChecked(false);
                rWomen.setChecked(false);


                for(int i=0;i<g1.getChildCount() ;i++) {
                    CheckBox cbs = (CheckBox) g1.getChildAt(i);
                    cbs.setChecked(false);
                }
                spCareer.setSelection(0);
            }
        });


        //返回上一个页面,并回传数据到MainActivity
        //为返回按钮设置监听器对象
        btReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //2.回传数据
                //创建Intent对象
                Intent intent = new Intent();
                //2)写入数据
                get_data();

                intent.putExtra("nickname",sNickname);
                intent.putExtra("age",sAge);
                intent.putExtra("hobby",sHobby);
                intent.putExtra("sex",sSex);
                intent.putExtra("career",sCareer);


                //3)回传
                setResult(RESULT_OK,intent);
                //4)销毁
                finish();

            }
        });


    }

    public void get_data(){
        //传递的数据
        sNickname = etNickname.getText().toString();
        sAge = etAge.getText().toString();

        if(rMan.isChecked()){
            sSex = "男";
        }else {
            sSex = "女";
        }
        sHobby="";
        for(int i=0;i<g1.getChildCount() ;i++) {
            CheckBox cbs = (CheckBox) g1.getChildAt(i);
            //判断状态
            if(cbs.isChecked()) {
                sHobby+=cbs.getText().toString()+"  ";
            }
        }
        sCareer = spCareer.getSelectedItem().toString();

    }



}

ShowActivity

package com.star.shiyan0602;

import androidx.appcompat.app.AppCompatActivity;

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

public class ShowActivity extends AppCompatActivity {

    //声明

    Button btR;
    TextView tvNick,tvAge,tvSex,tvHobby,tvCareer;


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


        tvNick = findViewById(R.id.tv_nickname);
        tvAge = findViewById(R.id.tv_age);
        tvSex = findViewById(R.id.tv_sex);
        tvHobby = findViewById(R.id.tv_hobby);
        tvCareer = findViewById(R.id.tv_career);

        //获取intent
        Intent intent = getIntent();
        //获取数据
        String nickname = intent.getStringExtra("nickname");
        String age = intent.getStringExtra("age");
        String sex = intent.getStringExtra("sex");
        String hobby= intent.getStringExtra("hobby");
        String career = intent.getStringExtra("career");

        tvNick.append(nickname);
        tvAge.append(age);
        tvSex.append(sex);
        tvHobby.append(hobby);
        tvCareer.append(career);


        btR = findViewById(R.id.bt_sr);

        btR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }
}

运行效果

登录界面

填写信息界面

若点击返回,信息回传到登录界面

 若点击保存,则跳转到show界面

 

举报

相关推荐

0 条评论