您的位置:首页 > 移动开发 > Android开发

Android初级教程人品计算器

2016-06-13 19:31 375 查看
先看布局:

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>


运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: