android多活动练习--人品计算器
效果图如下:
第二个页面:
显示结果和姓名、性别有关,代码如下:
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 }
这就完成了,只适合小白练习而已,做个小笔记。
android多活动练习--人品计算器的更多相关文章
- Android(java)学习笔记218:开发一个多界面的应用程序之人品计算器的简单实现
1.开启新的Activity的方法: (1)Intent 意图 (2)intent.setAction("自定义") 记得在清单文件中声明 (3)intent.setData(前 ...
- Android初级教程人品计算器
先看布局: main_activity.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr ...
- Android(java)学习笔记161:开发一个多界面的应用程序之人品计算器的简单实现
1.开启新的Activity的方法: (1)Intent 意图 (2)intent.setAction("自定义") 记得在清单文件中声明 (3)intent.setData(前 ...
- Android实训案例(一)——计算器的运算逻辑
Android实训案例(一)--计算器的运算逻辑 应一个朋友的邀请,叫我写一个计算器,开始觉得,就一个计算器嘛,很简单的,但是写着写着发现自己写出来的逻辑真不严谨,于是搜索了一下,看到mk(没有打广告 ...
- 人品计算器 JFrame 窗体软件版 JPanel JTextField JTextArea JButtton JLabel setContentPane Swing包(用户界面工具包)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.eve ...
- Android笔记——活动的生命周期
一.活动的重要性 掌握活动的生命周期对任何 Android 开发者来说都非常重要,当你深入理解活动的生命周期之后,就可以写出更加连贯流畅的程序,并在如何合理管理应用资源方面,你会发挥的游刃有余.你的应 ...
- Android——在活动中使用Menu
ADT在HelloWorldActivity中自动创建了一个onCreateOptionsMenu()方法.这个方法是用于在活动中创建菜单的,现在我们来仔细分析一下了.手机毕竟和电脑不同,它的屏幕空间 ...
- Android Studio 活动启动模式
启动模式一共分4种 可以再配置文件中设置 <activity android:name=".MainActivity" android:launchMode="si ...
- Android Studio 活动的生命周期
Activity 类中定义了7个回调方法,覆盖了活动的活动周期的每一环节 onCreate() 活动第一次创建的时候调用 onStart() 这个活动由不可见变为可见的时候调用 onResume() ...
- Android:活动的启动模式
启动模式一共有四种,分别是 standard .singleTop . singleTask 和 singleInstance , 可 以 在 AndroidManifest.xml 中 通 过 给 ...
随机推荐
- 测试用例Excel转XML格式教程
运行环境: Python版本:Python2.7.15 第三方库:pywin32 Excel版本:Excel2016 1.安装Python2.7.15 1)下载Python安装包 进入Python官网 ...
- idea的配置优化
一.显示工具条 二.设置鼠标悬浮提示 三.显示方法分隔符 四.忽略大小写提示 五.主题设置 如果需要很好看的编码风格,这里有很多主题 http://color-themes.com/?view=ind ...
- 为什么 Java 8 移除了永久代(PermGen)并引入了元空间(Metaspace)?
为什么 Java 8 移除了永久代(PermGen)并引入了元空间(Metaspace)? 在 Java 8 中,JVM 移除了 永久代(PermGen)并引入了 元空间(Metaspace),这一改 ...
- 关于μkeil v5.40(keil5) 如何使用STM32(ARM)虚拟下载器进行Proteus联调
最近我心血来潮,想用Proteus+keil5进行联调,但仔细在网上一找,全是某SDN扒下来的陈年老黑X,都快转出数字包浆了还在用,完完全全跟不上时代,也全是51单片机的版本,STM32(ARM)根本 ...
- 【记录】Word 2021|编号缩进调整
版本: Word 2021 专业版 Word编号缩进调整 有时候会觉得word列表的悬挂缩进太大或太小了. 第一步:右键编号的数字-调整列表缩进. 第二步:编辑格式-编号. 第三步:点击更多,选择编号 ...
- ISCTF的MISC复现
1. 小蓝鲨的签到02 随波逐流 识别问题加上IS即可 2. 数字迷雾:在像素中寻找线索 还是随波逐流 加个} 3. 小蓝鲨的签到01 关注公众号发送ISCTF2024即可 4. 小蓝鲨的问卷 答完得 ...
- B1041 考试座位号
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
- C#LINQ去掉数组字符串中的指定元素
例字符串: string s1 = "111,111,111222111,111333111,111"; string del = "111"; 要删除指定元素 ...
- [转]编译报错:virtual memory exhausted: Cannot allocate memory
一.问题 当安装虚拟机时系统时没有设置swap大小或设置内存太小,编译程序会出现virtual memory exhausted: Cannot allocate memory的问题,可以用swap扩 ...
- git clone速度慢?教你用最简单的方法解决最恶心的问题!
以前解决Github文件下载速度慢的方法只有挂代理或者导入Gitee,最近又发现一种新的解决方案. 只要把github.com换成hub.fastgit.org就可以解决问题了!具体的在下面的图片 介 ...