效果图如下:
第二个页面:
显示结果和姓名、性别有关,代码如下:
activity_main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context=".MainActivity"> 8 9 <EditText 10 android:id="@+id/name" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:hint="姓名" /> 14 15 <RadioGroup 16 android:id="@+id/rg" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:orientation="horizontal"> 20 21 <RadioButton 22 android:id="@+id/rb1" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="男" /> 26 27 <RadioButton 28 android:id="@+id/rb2" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="女" /> 32 33 <RadioButton 34 android:id="@+id/rb3" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="其他" /> 38 </RadioGroup> 39 40 <Button 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:onClick="btnClick" 44 android:text="计算" /> 45 46 </LinearLayout>
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 @Override 21 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 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".ResultActivity" 7 android:orientation="vertical"> 8 <TextView 9 android:id="@+id/rName" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="张三"/> 13 <TextView 14 android:id="@+id/rSex" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="男"/> 18 <TextView 19 android:id="@+id/result" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="您的人品很好"/> 23 24 </LinearLayout>
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 @Override 18 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 1111 51 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 }
这就完成了,只适合小白练习而已,做个小笔记。