首页 技术 正文
技术 2022年11月20日
0 收藏 748 点赞 4,548 浏览 3425 个字

本文实现一个简易的人品计算器来实践在不同Actitity之间数据传递

intent的数据传递

从A界面打开B界面 把A界面的数据传递给B界面

1. intent.setData(uri) — intent.getData();

可以传递简单的文本数据。

2. intent.putExtra(name, value)

8大基本类型的数据,数组都可以传递
String对象 可以传递 charSequence
可以序列化的对象(序列化到文件) Serializable 也可以传递
可以序列化的对象(序列化到内存) Parcelable 也可以传递 bitmap
可以传递 一个map集合 Bundle extras = new Bundle();

本文地址:http://www.cnblogs.com/wuyudong/p/5679191.html,转载请注明源地址。

新建项目,MainActivity中的代码如下:

package com.wuyudong.testrp;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.Toast;public class MainActivity extends Activity { private EditText et_name; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText)findViewById(R.id.et_name);
} public void click(View view) {
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "名字不能为空", 1).show();
return;
}
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("name", name);
startActivity(intent);
}}

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=".MainActivity" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="人品计算器"
android:textColor="#ff0000"
android:textSize="26sp" /> <EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" >
</EditText> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:layout_centerInParent="true"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
>
</Button>
</RelativeLayout></LinearLayout>

界面如下:

Android 在不同Actitity之间数据传递

输入姓名后,点击按钮跳转到另一个结果界面: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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试结果:"
android:textColor="#ff0000"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_result"
android:text="xxx:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#99000000"
android:textSize="17sp" /> <ProgressBar
android:id="@+id/progressBar1"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /></LinearLayout>

界面如下:

Android 在不同Actitity之间数据传递

ResultActivity.java代码如下:

package com.wuyudong.testrp;import java.util.Random;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
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_result = (TextView) findViewById(R.id.tv_result); Intent intent = getIntent(); String name = intent.getStringExtra("name");
Random rm = new Random();
int rp = rm.nextInt(101);
tv_result.setText(name + ":您的人品值为:" + rp); ProgressBar pb = (ProgressBar)findViewById(R.id.progressBar1);
pb.setProgress(rp); }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901