Android初级教程人品计算器

来源:互联网 发布:二次元动态桌面软件 编辑:程序博客网 时间:2024/05/23 14:47

先看布局:

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;@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("您的人品太次了  您需要努力啊");}}}
配置文件:

<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>

运行结果:



1 0