Android算命小程式的實現
阿新 • • 發佈:2018-12-23
[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
* 郵箱:[email protected]
* 時間: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>