您的位置:首页 > 移动开发 > Android开发

activity间数据传递实例_人品计算器

2016-12-07 10:12 323 查看
一、项目目录结构



二、activity_main.xml界面



三、activity_main.xml代码

<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="com.zgs.ActivityPassData.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: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="20dp"
android:text="女" />

<RadioButton
android:id="@+id/rb_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="人妖" />
</RadioGroup>

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

</LinearLayout>
四、MainActivity.java代码
package com.zgs.ActivityPassData;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;

//(1)找到我们关心的控件
et_name = (EditText) findViewById(R.id.et_name);
rg_group = (RadioGroup) findViewById(R.id.rg_group);
}

//点击按钮 获取数据 跳转到ResultActivity页面
public void click(View v) {
//[1]获取用户名
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_LONG).show();
return;
}

//[2]判断选中性别
int checkedRadioButtonId = rg_group.getCheckedRadioButtonId();
//[3]判断一下具体选中的性别
int sex = 0; //默认值为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;
}
//[4]判断性别
if (sex == 0) {
Toast.makeText(getApplicationContext(), "亲 请选择性别 ", 0).show();
return;
}

//[5] 跳转到resutActivity页面  显示意图
Intent intent = new Intent(mContext,ResultActivity.class);

//[5.1]要把name 和 sex 传递到结果页面  底层map
intent.putExtra("name", name);
intent.putExtra("sex", sex);

//[6]开启Activity
startActivity(intent);
}

}
五、activity_result.xml界面



六、activity_result.xml代码

<?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"
android:text="张三" />

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

<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您的人品非常好 " />

</LinearLayout>
七、ResultActivity.java代码
package com.zgs.ActivityPassData;

import android.app.Activity;
import android.content.Intent;
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);
// [1]找到我们关心的控件
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);

// [2]获取开启此Activity的意图对象
Intent intent = getIntent();
// [3]获取我们携带过来的数据 取出性别和name 传递的是什么样的数据类型 你在取的时候
String name = intent.getStringExtra("name");// 获取name
int sex = intent.getIntExtra("sex", 0);

// [4]把数据显示到控件上
tv_name.setText(name); // 显示姓名

// [5]显示性别
byte[] bytes = null;

try {
switch (sex) {
case 1: // 代表男
tv_sex.setText("男");
bytes = name.getBytes("gbk");
break;

case 2:
tv_sex.setText("女");
bytes = name.getBytes("utf-8");
break;

case 3:
tv_sex.setText("人妖");
bytes= name.getBytes("iso-8859-1");
break;
}
} catch (Exception e) {
e.printStackTrace();
}

//[6]根据我们输入的姓名和性别 来计算人品得分  根据得分显示结果
int  total = 0;
for (byte b : bytes) {     //0001 1000
int number =b&0xff;    //1111 1111
total+=number;
}

//算出得分
int score = Math.abs(total)%100;
if (score >90) {
tv_result.setText("您的人品非常好");
}else if (score >70) {
tv_result.setText("有你这样的人品算是不错了..");
}else if (score >60) {
tv_result.setText("您的人品刚刚及格");
}else{
tv_result.setText("您的人品不及格....");
}
}

}
八、AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zgs.ActivityPassData"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />

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

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--配置resultActivity页面   -->
<activity android:name="com.zgs.ActivityPassData.ResultActivity"></activity>
</application>

</manifest>
九、操作演示



十、源码下载

activity数据传递_人品计算器 提取码:59hj
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐