Android初级教程人品计算器
先看布局:
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" > <EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" /> <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" /> <RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="女" /> <RadioButton
android:id="@+id/rb_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="人妖" />
</RadioGroup> <Button
android: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" > <TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="张三" /> <TextView
android:id="@+id/tv_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="男" /> <TextView
android: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; @Override
protected 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 group
int 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 { @Override
protected 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 1111
total+=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("您的人品太次了 您需要努力啊"); } } }
配置文件:
<activity
android: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初级教程人品计算器的更多相关文章
- Android初级教程理论知识(第三章测试&数据存储&界面展现)
首先介绍单元测试,我在javaweb部分有详细介绍单元测试框架的一篇文章. 可以先看在javaweb中的单元测试详解篇http://blog.csdn.net/qq_32059827/article/ ...
- Android初级教程Activity小案例(计算器乘法运算)
首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算:另一个布局文件进行显示最终结果.Activity1启动Activity2,并传递计算结果值给Activity2. main.xml: &l ...
- Android初级教程:RatingBar的使用
记得淘宝里面买家给卖家评分的时候会有一个星星状的评分条,其实就是基于RatingBar做了自定义使用了.那么本篇文章就对RatingBar的使用做一个基本的认识. 接下来就是正题,那就是对于Ratin ...
- Android初级教程_获取Android控件的宽和高
转载:http://blog.csdn.net/johnny901114/article/details/7839512 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看 ...
- Android初级教程:ViewPage使用详解
转载本博客,请注明出处:http://blog.csdn.net/qq_32059827点击打开链接 ViewPage使用之一就是轮播广告,就以此为出发点,来详细解析一下ViewPage的使用和加载机 ...
- Android初级教程:如何自定义一个状态选择器
有这样一种场景:点击一下某个按钮或者图片(view),改变了样式(一般改变背景颜色).这个时候一种解决方案,可能就是状态选择器.接下来就介绍如何实现状态选择器: 步骤: 一.新建这样的文件夹:res/ ...
- Android初级教程:shape的基本用法
转载本文请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203347 点击打开链接 在自定义进度条之前,先来学习一下shape的用 ...
- Android初级教程:Android中解析方式之pull解析
在安卓中有很多种解析方式.按照大方向有xml解析和json解析.而,细致的分,xml和json解析各有自己的很多解析方式.今天这一篇主要介绍xml解析中的pull解析.对于xml的解析方式,我之前在j ...
- Android初级教程:使用xml序列器
之前备份短信的时候生成xml都是手动拼写的,有一个问题:当短信里面存在</body>这样的标签的时候,最后结果就不是完整的xml文件,显然出错.但是,今天使用序列化器的方式,就能有效的解决 ...
随机推荐
- windows平台下cocos2d-x-3.0beta2创建新项目
我目前使用的版本是cocos2d-x-3.0beta2 前提: 已安装了python2.7.3 已安装了VS2012 打开我的cocos2d-x-3.0beta2所在文件夹F:\Framework\c ...
- sublime text3中设置Emmet输入标签自动闭合
项目后端前一段时间从C#转成了JAVA,在开发的过程中,由于HTML对标签的语法很宽松,比如这样:<img src="" alt="">在标签的结尾 ...
- js变量的一点认识
js中变量包含两种不同数据类型的值,基本类型值(简单的数据段)和引用类型值(可能由多个值组成的对象).那么他们在保存方式和复制变量值是上有什么不同呢? 一.保存 只能给引用类型的值动态添加属性,不能给 ...
- UDP网络编程
概念: UDP协议(用户数据报协议)是无连接,不可靠的,无序的.速度比较快, UDP协议以数据报作为数据传输的载体 进行数据传输时,首先将传输的数据定义成数据报(Datagram),在数据报中指明数据 ...
- c++ Lambda表达式待修改
C++11引入了lambda表达式,使得程序员可以定义匿名函数,该函数是一次性执行的,既方便了编程,又能防止别人的访问. Lambda表达式的语法通过下图来介绍: 这里假设我们定义了一个如上图的lam ...
- ABP文档笔记系列
ABP文档笔记 - 模块系统 及 配置中心 ABP文档笔记 - 事件BUS ABP文档笔记 - 数据过滤 ABP文档笔记 - 规约 ABP文档笔记 - 配置.设置.版本.功能.权限 ABP文档笔记 - ...
- Node.js 教程
简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务端Ja ...
- DB2 911错误的解释
SQL0911N 因为死锁或超时,所以当前事务已回滚.原因码为 "<原因码>". 说明: 当前工作单元参与了未解决的对象争用,因此必须回滚. 原因码如下所示: 2 ...
- Dynamics CRM build numbers
Dynamics CRM build numbers CRM各大版本及补丁列表,整理的很全
- [OpenCV] How to install opencv by compiling source code
Introduction Install OpenCV and its dependence ! STEPs 1, compiler sudo apt-get install build-essent ...