效果图如下:
第二个页面:
显示结果和姓名、性别有关,代码如下:
activity_main.xml:
1 28 9 14 15 20 21 26 27 32 33 38 39 40 45 46
MainActivity.java:
1 import android.content.Intent; 2 import android.support.v7.app.AppCompatActivity; 3 import android.os.Bundle; 4 import android.text.TextUtils; 5 import android.view.View; 6 import android.widget.EditText; 7 import android.widget.RadioButton; 8 import android.widget.RadioGroup; 9 import android.widget.TextView;10 import android.widget.Toast;11 12 public class MainActivity extends AppCompatActivity {13 14 private EditText et_name;15 private RadioGroup rg;16 private RadioButton rb1;17 private RadioButton rb2;18 private RadioButton rb3;19 20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 super.onCreate(savedInstanceState);23 setContentView(R.layout.activity_main);24 25 initView();26 27 }28 29 public void initView() {30 et_name = findViewById(R.id.name);31 rg = findViewById(R.id.rg);32 rb1 = findViewById(R.id.rb1);33 rb1 = findViewById(R.id.rb3);34 rb1 = findViewById(R.id.rb1);35 }36 37 public void btnClick(View view) {38 //获取用户名39 String name = et_name.getText().toString().trim();40 //判断是否为空41 if (TextUtils.isEmpty(name)) {42 Toast.makeText(getApplicationContext(), "请输入姓名", Toast.LENGTH_SHORT).show();43 } else {44 //判断性别45 int radioButtonId = rg.getCheckedRadioButtonId();46 int sex = 0;//sex代表性别47 switch (radioButtonId) {48 case R.id.rb1://代表男49 sex = 1;50 break;51 case R.id.rb2://代表女52 sex = 2;53 break;54 case R.id.rb3://代表其他55 sex = 3;56 break;57 }58 if (sex == 0) {59 Toast.makeText(getApplicationContext(), "请输入性别", Toast.LENGTH_SHORT).show();60 61 } else {62 //跳转页面63 Intent intent = new Intent(MainActivity.this, ResultActivity.class);64 intent.putExtra("name", name);65 intent.putExtra("sex", sex);66 startActivity(intent);67 }68 }69 }70 }
activity_result.xml:
1 28 13 18 23 24
ResulrActivity.java:
1 import android.content.Intent; 2 import android.net.Uri; 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.TextView; 7 8 import java.util.Random; 9 10 public class ResultActivity extends AppCompatActivity {11 12 private TextView rName;13 private TextView rSex;14 private TextView result;15 private byte[] bytes;16 17 @Override18 protected void onCreate(Bundle savedInstanceState) {19 super.onCreate(savedInstanceState);20 setContentView(R.layout.activity_result);21 22 rName = findViewById(R.id.rName);23 rSex = findViewById(R.id.rSex);24 result = findViewById(R.id.result);25 26 //获取传递来的数据27 Intent intent = getIntent();28 String name = intent.getStringExtra("name");29 int sex = intent.getIntExtra("sex", 0);30 rName.setText(name);31 String str = name;32 switch (sex) {33 case 1:34 rSex.setText("男");35 str = name + "男";36 break;37 case 2:38 rSex.setText("女");39 str = name + "女";40 break;41 case 3:42 rSex.setText("其他");43 str = name + "其他";44 break;45 }46 //计算结果使用随机数47 bytes = str.getBytes();48 int totle = 0;49 for (byte b : bytes) {50 int number = b & 0xff;//b&1111 111151 totle += number;52 53 }54 int score = Math.abs(totle) % 100;55 if (score > 90) {56 result.setText("你的人品不错");57 } else if (score > 90) {58 result.setText("你的人品还行");59 } else if (score > 70) {60 result.setText("你的人品刚及格");61 } else if (score > 50) {62 result.setText("你快被世界抛弃了");63 } else if (score > 40) {64 result.setText("虽然被抛弃了,你要活着,");65 }//重点不是数据,然是页面间的传值66 67 68 }69 }
这就完成了,只适合小白练习而已,做个小笔记。