码迷,mamicode.com
首页 > 移动开发 > 详细

安卓案例:人品计算器

时间:2018-04-20 10:21:28      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:super   mat   cal   跳转   Nid   简单的   width   exce   edit   

显示意图跳转的运用

 

两个简单的布局:

main

<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="女" />
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="计算" />

</LinearLayout>

 

result:

<?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" />

    <TextView
        android:id="@+id/tv_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

 

MainActivity:

package org.dreamtech.game;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
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);
    }

    public void click(View v) {
        String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(getApplicationContext(), "请输入姓名", Toast.LENGTH_LONG)
                    .show();
            return;
        }
        int radiobuttonID = rg_group.getCheckedRadioButtonId();
        int sex = 0;
        switch (radiobuttonID) {
        case R.id.rb_male:
            sex = 1;
            break;
        case R.id.rb_female:
            sex = 2;
            break;
        }
        if (sex == 0) {
            Toast.makeText(getApplicationContext(), "请选择性别", Toast.LENGTH_LONG)
                    .show();
            return;
        }

        Intent intent = new Intent(this, ResultActivity.class);
        
        intent.putExtra("name",name);
        intent.putExtra("sex", sex);
        
        startActivity(intent);
    }
}

 

 

ResultActivity:

package org.dreamtech.game;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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);

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        byte[] bytes = name.getBytes();
        int sex = intent.getIntExtra("sex", 0);

        tv_name.setText(name);
        switch (sex) {
        case 1:
            tv_sex.setText("男");
            try {
                bytes = name.getBytes("gbk");
            } catch (UnsupportedEncodingException e) {
            }
            break;
        case 2:
            tv_sex.setText("女");
            try {
                bytes = name.getBytes("utf-8");
            } catch (UnsupportedEncodingException e) {
            }
            break;
        }

        int total = 0;
        for (byte b : bytes) {
            int number = b & 0xff;
            total += number;
        }
        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("您的人品很差");
        }
    }
}

 

安卓案例:人品计算器

标签:super   mat   cal   跳转   Nid   简单的   width   exce   edit   

原文地址:https://www.cnblogs.com/xuyiqing/p/8888262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!