Android算命小程序的实现

[0]RadioGroup的使用
[1]Intent页面跳转,manifest 设置。
[2]Intent传递参数,数据。
[3]随机数获取算法。

//manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest package="jacky.rpresult"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".ResultActivity"></activity>
    </application>

</manifest>

//MainActivity

package jacky.rpresult;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

          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.rg_group);
          }

          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 checkedRadioButtonId = rg_group.getCheckedRadioButtonId();
                    int sex=0;
                    switch (checkedRadioButtonId) {
                              //选择的是男
                              case R.id.rb_male:sex=1;
                                        break;
                              //选择的是女
                              case R.id.rb_female:sex=2;
                                        break;
                              //选择的是其他
                              case R.id.rb_other:sex=3;
                                        break;
                    }
                    if (sex == 0) {
                              Toast.makeText(getApplicationContext(), "请输入性别",Toast.LENGTH_LONG).show();
                              return;
                    } else {
                              //跳转逻辑
                              Intent intent=new Intent(this,ResultActivity.class);
                              intent.putExtra("name",name);
                              intent.putExtra("sex",sex);
                              startActivity(intent);
                    }


          }
}

//ResultActivity

package jacky.rpresult;

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

import java.io.UnsupportedEncodingException;

/**
 * 作者:Jacky
 * 邮箱:550997728@qq.com
 * 时间:2016/2/8 14:42
 */
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");
                    int sexx = intent.getIntExtra("sex", 0);
                    String sex=null;
                    //实现算命数据的生成
                    byte[] bytes=null;
                    try {
                              switch (sexx) {
                                        case 1:sex="男";bytes=name.getBytes("gbk");
                                                  break;
                                        case 2:sex="女";bytes=name.getBytes("utf-8");
                                                  break;
                                        case 3:sex="其他";bytes=name.getBytes("iso-8859-1");
                                                  break;
                              }
                    } catch (UnsupportedEncodingException e) {
                              e.printStackTrace();
                    }
                    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("您的RP非常好,出门要捡红包了!");
                    }else if (score>80) {
                              tv_result.setText("您的RP十分优秀,有钱途!");
                    }else if (score>70) {
                              tv_result.setText("您的RP好!");
                    }else if (score > 60) {
                              tv_result.setText("您的RP一般!");
                    } else {
                              tv_result.setText("您的RP太差了,多烧点香火!");
                    }
                    tv_name.setText(name);
                    tv_sex.setText(sex);
          }
}

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="jacky.rpresult.MainActivity">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"
        />

    <RadioGroup
        android:id="@+id/rg_group"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="男"/>

        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="女"/>

        <RadioButton
            android:id="@+id/rb_other"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:text="其他"/>
    </RadioGroup>

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


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="张三"/>
    <TextView
        android:id="@+id/tv_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="男"/>
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="您的RP非常好!"/>
</LinearLayout>

这里写图片描述

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗杰海贼团

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值