Android算命小程序的实现

来源:互联网 发布:厦门二手房成交数据 编辑:程序博客网 时间:2024/05/04 21:22

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

这里写图片描述

这里写图片描述

这里写图片描述

0 0