安卓案例:人品计算器

Posted xuyiqing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓案例:人品计算器相关的知识,希望对你有一定的参考价值。

显示意图跳转的运用

 

两个简单的布局:

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("您的人品很差");
        }
    }
}

 

以上是关于安卓案例:人品计算器的主要内容,如果未能解决你的问题,请参考以下文章

java 代码片段【安卓】

人品问题

计算机从业者人品计算器

JZOJ 3914. 人品问题

[树形dp] Jzoj P3914 人品问题

片段从一开始就没有显示 |安卓工作室