又是天空晴朗的一天,今天心情还不错啊。今天的我依然想玩亚索。哈哈啊哈,话不多说,进入正题,今天聊一聊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的传值过程。以后认真复习学习该内容,挺重要的。