先看布局:

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" ><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入姓名" /><RadioGroupandroid:id="@+id/radioGroup1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/rb_male"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男" /><RadioButtonandroid:id="@+id/rb_female"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="25dp"android:text="女" /><RadioButtonandroid:id="@+id/rb_other"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="25dp"android:text="人妖" /></RadioGroup><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="click"android:text="计算" /></LinearLayout>

第二个布局:

result_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="张三" /><TextViewandroid:id="@+id/tv_sex"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="男" /><TextViewandroid:id="@+id/tv_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="您的人品非常好" /></LinearLayout>

主活动代码:


import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;public class MainActivity extends Activity {private EditText et_name;private RadioGroup rg_group;@Overrideprotected 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] 判断一下name 是否为空if (TextUtils.isEmpty(name)) {Toast.makeText(getApplicationContext(), "亲 请输入姓名", 1).show();return;}// [3]判断用户选择的性别int radioButtonId = rg_group.getCheckedRadioButtonId();//the unique id of the selected radio button in this groupint sex  = 0;switch (radioButtonId) {case R.id.rb_male: // 代表选择的是男sex = 1;//1表示男性break;case R.id.rb_female: // 代表选择的是女sex = 2;break;case R.id.rb_other: // 代表选择的是人妖sex = 3;break;}if(sex == 0){//哪个RadioButton也没选Toast.makeText(getApplicationContext(), "请选择性别", 1).show();return;}//[4]跳转到ResultActivity页面   用显示意图跳转Intent intent = new Intent(this, ResultActiviyt.class);//传递姓名intent.putExtra("name", name);//传递性别 intent.putExtra("sex", sex);startActivity(intent); }}

第二个活动代码:


import java.io.UnsupportedEncodingException;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;public class ResultActiviyt extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// [1]加载布局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);//第二个参数值:the value to be returned if no value of the desired type is stored with the given name.// [4]根据name 和 sex 显示数据tv_name.setText(name);byte[] bytes = null;// [5]显示性别try {switch (sex) {case 1:tv_sex.setText("男");//Returns a new byte array containing the characters of this string encoded using the named charset. //同时,设置编码是为了得到不同的二进制,目的还是为了得到不同的人品描述bytes = name.getBytes("gbk"); //if the charset is not supported会抛异常break;case 2:tv_sex.setText("女");bytes = name.getBytes("utf-8"); break;case 3:tv_sex.setText("人妖");bytes = name.getBytes("iso-8859-1"); break;default:break;}} catch (UnsupportedEncodingException e) {e.printStackTrace();}//[6]计算人品结果 市面上大多数应用采用的是随机数  。下面是另一种算法int total = 0;for (byte b : bytes) {                  // 0001 1111 int number = b&0xff;               // 1111 1111total+=number;}System.out.println(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("您的人品太次了  您需要努力啊");}      }}

配置文件:

<activityandroid:name="com.it.rpcalc.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!--配置resultActiviyt  --><activity android:name="com.it.rpcalc.ResultActiviyt"></activity>

运行结果:

Android初级教程人品计算器相关推荐

  1. Android初级教程Activity小案例(计算器乘法运算)

    首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算:另一个布局文件进行显示最终结果.Activity1启动Activity2,并传递计算结果值给Activity2. main.xml: &l ...

  2. Android初级教程:RatingBar的使用

    记得淘宝里面买家给卖家评分的时候会有一个星星状的评分条,其实就是基于RatingBar做了自定义使用了.那么本篇文章就对RatingBar的使用做一个基本的认识. 接下来就是正题,那就是对于Ratin ...

  3. Android初级教程获取手机系统联系人信息

    在手机内部,对联系人信息存在对应的数据库.我们创建的而联系人信息都存在这张表中.如下是对数据库的截图,我已经对表和应该注意的地方做了红笔标注: 好了,现在可以根据数据库里面的数据来写代码了. 代码如下 ...

  4. Android初级教程:Android中解析方式之pull解析

    在安卓中有很多种解析方式.按照大方向有xml解析和json解析.而,细致的分,xml和json解析各有自己的很多解析方式.今天这一篇主要介绍xml解析中的pull解析.对于xml的解析方式,我之前在j ...

  5. Android初级教程初谈自定义view自定义属性

    有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...

  6. Android初级教程调用手机拍照与摄像功能

    这个小案例建议在手机上运行. package com.example.camera;import java.io.File;import android.net.Uri; import android ...

  7. Android初级教程短信防火墙

    如果你有女神,而且有情敌的话,你看到这篇文章会有一种窃喜的感觉. 需求:对情敌的号码进行拦截,让女神手机永远收不到它的号码. 首先定义一个广播接收者类: package com.example.sms ...

  8. Android初级教程实现电话录音

    需求:设置来电后自动录音. 首先设置一个按钮,代码很简单这里就不再给出. 建一个类,RecorderServicer extends Service package com.ydl.recorder; ...

  9. Android初级教程IP拨号器初识广播接受者

    需求:输入ip号码并且保存在本地,监听打电话广播,如果电话号码以0开头,则加上ip区号拨打. 首先定义一个页面布局: <LinearLayout xmlns:android="http ...

最新文章

  1. 面试 | HashMap 为什么线程不安全?
  2. Android 指纹调试流程(高通、MTK均适用)
  3. Apprenda发布Kubernetes商业版,PaaS、CaaS任君选择
  4. Matlab之abs、double与char函数
  5. Emacs自带小游戏
  6. jquery 的一款比较好的Menu
  7. f-admin——基于Laravel框架开发的基础权限后台系统
  8. 3811.排列-AcWing题库
  9. jcp 打印机字体变淡变模糊bootstrap
  10. 科学计算机怎么用10次方,一个数怎么用计算器开10次方
  11. golang正则匹配中文字符,查询中文字符会panic退出的问题
  12. ipad MOV转mp4
  13. js与朴php订单评价功能
  14. 杨立昆怒怼机器人索菲娅:AI硬加人类意识是揠苗助长
  15. ANSYS LSDYNA时间步设置
  16. 重大利好,区块链技术能保护森林资源?
  17. 初识设计模式 - 解释器模式
  18. c语言键盘驱动程序,c语言键盘扫描程序
  19. 字符数据在内存中的存储形式及其使用方法
  20. 树莓派4b搭建danted socks5 代理服务器 利用闲置宽带扩展搬砖IP

热门文章

  1. jQuery插件之Cooki(jquery.cookie.js)
  2. linux 录像,Linux的录像与回放
  3. gcc: fatal error: no input files
  4. 免费录音转文字软件有哪些?推荐这几款录音转文字软件
  5. Qt Quick - Popup控件详解
  6. 如何在日常生活修行六度与四摄行?
  7. 雷鸣:把设计当成一门艺术
  8. XServer基本概念 + x11vnc配置远程桌面
  9. Unity中Quaternion * Vector3的理解
  10. webrtc服务器架构Mesh/MCU/SFU