public class MainActivity extends AppCompatActivity {
private EditText et_name;
private RadioGroup rg_group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
rg_group = (RadioGroup) findViewById(R.id.radioGroup1);
}
//点击按钮 实现计算人品 跳转到ResultActivity页面
public void click(View v) {
//1 获取用户名
String name = et_name.getText().toString().trim();
//2 判断是否为空
if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "请输入姓名", Toast.LENGTH_SHORT).show();
return;
}
//3 判断用户选择的性别
int radioButtonId = rg_group.getCheckedRadioButtonId();
int sex = 0;
switch (radioButtonId) {
case R.id.rb_male: // 代表选择的是男
sex = 1;
break;
case R.id.rb_female: // 代表选择的是女
sex = 2;
break;
case R.id._rb_other: // 代表选择的是人妖
sex = 3;
break;
}
if (sex == 0) {
Toast.makeText(getApplicationContext(), "请选择性别", Toast.LENGTH_SHORT).show();
return;
}
//4 跳转到ResultActivity页面 用显示意图跳转
Intent intent = new Intent(this, ResultActivity.class);
// 传递姓名
intent.putExtra("name", name);
// 传递性别
intent.putExtra("sex", sex);
startActivity(intent);
}
}
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载布局
setContentView(R.layout.activity_result);
TextView tv_name = (TextView) findViewById(R.id.tv_name);
TextView tv_sex = (TextView) findViewById(R.id.tv_sex);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
//2 获取MainActivity 传递过来的数据
Intent intent = getIntent(); //获取开启此Activity的意图对象
//3 获取name 和 sex 的值 小技巧:传递的是什么数据类型,这边就按照传递的数据类型取
String name = intent.getStringExtra("name");
int sex = intent.getIntExtra("sex", 0);
//4 根据name和sex 显示数据
tv_name.setText(name);
byte[] bytes = null;
//5 显示性别
try {
switch (sex) {
case 1:
tv_sex.setText("男");
bytes = name.getBytes("gbk");
break;
case 2:
tv_sex.setText("女");
bytes = name.getBytes("utf-8");
break;
case 3:
tv_sex.setText("人妖");
bytes = name.getBytes("iso-8859-1");
break;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//6 计算人品结果 市面上大多数应用采用的是随机数
int total = 0;
for (byte b : bytes) {
int number = b&0xff; // 1111 1111 0x是十六进制的标志 ff是 255
total+=number;
Log.d("mylog", "onCreate: total= " + total);
}
//获取得分
int score = Math.abs(total)%100;
if (score > 90) {
tv_result.setText("您的人品非常好");
} else if (score > 80) {
tv_result.setText("您的人品还可以");
} else if (score > 60) {
tv_result.setText("您的人品刚及格");
} else {
tv_result.setText("您的人品太次了");
}
}
}
<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="com.example.a41_character_calculator.MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"/>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="女"/>
<RadioButton
android:id="@+id/_rb_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="人妖"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="计算"/>
</LinearLayout>