package com.lidaochen.test; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; 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); // 获取EditText控件 et_name = (EditText)findViewById(R.id.et_name); // 获取RadioGroup控件 rg_group = (RadioGroup)findViewById(R.id.rg_group); } // 点击按钮 实现计算人品 跳转到ResultActivity页面 public void click(View v) { // 1、获取用户 String name = et_name.getText().toString().trim(); // 2、判断一下name是否为空 if(TextUtils.isEmpty(name)) { Toast.makeText(getApplicationContext(), "亲,请输入姓名...", Toast.LENGTH_SHORT).show(); return; } // 3、判断用户选择的性别 int radioBtnID = rg_group.getCheckedRadioButtonId(); int sex = 0; switch (radioBtnID) { // 代表选择的是男 case R.id.rb_man: sex = 1; break; // 代表选择的是女 case R.id.rb_woman: sex = 2; break; // 代表选择的是人妖 case R.id.rb_renyao: sex = 3; break; } // 4、判断一下是否选择了性别 if (sex == 0) { Toast.makeText(getApplicationContext(), "亲,请选择性别...", Toast.LENGTH_SHORT).show(); return; } // 用显示意图跳转到ResultActivity页面 Intent intent = new Intent(this, ResultActivity.class); // 传递姓名 intent.putExtra("name", name); // 传递性别 intent.putExtra("sex", sex); // 开启Activity startActivity(intent); } }
package com.lidaochen.test; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.io.UnsupportedEncodingException; public class ResultActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); // 获取TextView控件 TextView tv_name = (TextView)findViewById(R.id.tv_name); TextView tv_sex = (TextView)findViewById(R.id.tv_sex); TextView tv_rp = (TextView)findViewById(R.id.tv_rp); // 获取MainActivity传递过来的数据 Intent intent = getIntent(); // 获取name和sex的值 String name = intent.getStringExtra("name"); int sex = intent.getIntExtra("sex", 0); // 显示数据 tv_name.setText(name); byte[] bytes = null; switch (sex) { case 1: tv_sex.setText("男"); try { bytes = name.getBytes("gbk"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } break; case 2: tv_sex.setText("女"); try { bytes = name.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } break; case 3: tv_sex.setText("人妖"); try { bytes = name.getBytes("iso-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } break; default: break; } // 计算人品结果 int total = 0; for (byte b :bytes) { int number = b & 0xFF; total += number; } // 获取得分 int score = Math.abs(total) % 100; if (score > 90) { tv_rp.setText("您的人品非常好,您家的祖坟都冒青烟啦!"); } else if(score > 80) { tv_rp.setText("您的人品还可以!"); } else if(score > 60) { tv_rp.setText("您的人品刚及格!"); } else { tv_rp.setText("您的人品太次了,您需要努力啊!"); } } }