第12天-03-activity之间通过intent传值

又是天空晴朗的一天,今天心情还不错啊。今天的我依然想玩亚索。哈哈啊哈,话不多说,进入正题,今天聊一聊activity之间的传值问题。

我们新建一个项目,命名为人品计算器(RpCalc)。首先看一下成品效果吧。在这里插入图片描述
哈哈哈,哈行吧。话不多说,直接上代码
先来看一下项目结构在这里插入图片描述
MainActivity代码:

package com.glsite.rpcalc;

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

import java.net.IDN;

public class MainActivity extends AppCompatActivity {

    private EditText mEtName;
    private RadioGroup mRgSex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通过输入姓名和性别,然后点击计算跳转到第二个页面显示人品值
        //开发步骤
        //1.找到空间,并设置事件
        //2.在点击事件当中,获取输入信息
        //3.将输入的信息设置到intent对象
        //4.利用startactivity,将intent对象的内容传递给activity2
        //5.activity2获得intent对象传递的内容并显示计算器
        mEtName = findViewById(R.id.et_name);
        mRgSex = findViewById(R.id.rg_sex);

    }

    /**
     * 点击按钮进入第二个界面
     * @param view
     */

    public void click(View view) {
        String name =  mEtName.getText().toString().trim();
        int Sex = mRgSex.getCheckedRadioButtonId();
        if (Sex == R.id.rb_male) {
            Sex = sex.MALE;
        } else if (Sex == R.id.rb_female){
            Sex = sex.FEMALE;
        } else {
            Sex = sex.UNKNOW;
        }
        //利用startactivity和intent传值到第二个activity
        Intent intent = new Intent(this,CalcActivity.class);
        intent.putExtra("name",name);
        intent.putExtra("Sex",Sex);
        startActivity(intent);
    }
}

CalcActivity代码:

package com.glsite.rpcalc;

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

public class CalcActivity extends AppCompatActivity {

    private TextView mTvName;
    private TextView mTvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calc);
        mTvName = findViewById(R.id.tv_name);
        mTvResult = findViewById(R.id.tv_result);

        Intent intent = this.getIntent();
        int Sex = intent.getIntExtra("Sex", 0);
        String name = intent.getStringExtra("name");
        System.out.println("name" + name + ",Sex"+Sex);
        byte[] result = null;
        switch (Sex) {
            case sex.MALE:
                result = name.getBytes();
                showArray(result,sex.MALE);
                break;
            case sex.FEMALE:
                result = name.getBytes();
                showArray(result,sex.FEMALE);
                break;
            case sex.UNKNOW:
                result = name.getBytes();
                showArray(result,sex.UNKNOW);
                break;

        }
        int score = 0;
        for(byte b : result) {
            score += b & 0xff;
        }
        score = Math.abs(score) % 100;
        mTvName.setText(name);
        mTvResult.setText("人品值为:" + score);

    }

    private void showArray(byte[] arr,int sex){
        for (int i = 0; i < arr.length; i++) {
            arr[i] += sex;
            System.out.println(arr[i]);
        }
        System.out.println();
    }
}

sex代码:

package com.glsite.rpcalc;

/**
 * @author glsite.com
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class sex {
    public static final int MALE = 1;
    public static final int FEMALE = 2;
    public static final int UNKNOW = 3;
}

activity_main代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="欢迎使用人品计算器"
        android:textColor="#ff0000"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="请输入姓名"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <RadioGroup
        android:id="@+id/rg_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="52dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_name"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="男" />

        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="女" />

        <RadioButton
            android:id="@+id/rb_unknow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="未知" />
    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="click"
        android:text="进入计算截面"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

activity_calc代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".CalcActivity">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:text="姓名:"
        android:textColor="#ff0000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="32dp"
        android:text="计算结果:"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_name" />
</android.support.constraint.ConstraintLayout>

好了,以上就是人品计算器的全部代码了,里面穿插着intent的传值过程。以后认真复习学习该内容,挺重要的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值