Android初级教程人品计算器
2024-10-19 02:25:26
先看布局:
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>
运行结果:
最新文章
- ubuntu 下emacs 配置
- maven pom.xml
- WPF学习之路(三) 属性与依赖
- nodejs——网络编程模块
- NOIP1999邮票面值设计[搜索|DP]
- eclipse的安装环境及eclipse下maven的配置安装
- Hive深入浅出
- nginx/apache/php隐藏http头部版本信息的实现方法
- C语言用static限制函数以及全局变量的作用域
- poj 2246 (zoj 1094)
- java进制转换器 图形用户界面 十进制及其相反数分别转化为二,四,八,十六进制
- Socket层实现系列 — I/O事件及其处理函数
- 如何改变XCode的默认设置
- 菜鸟学习计划浅谈之Linux系统
- FPGA例化ROM存储表格
- nginx proxy_set_header设置、自定义header
- Java和Python分别实现直接选择排序
- JS点击按钮下载文件
- PAT A1133 Splitting A Linked List (25 分)——链表
- MSSQL 2012 修改所有表的架构Schame