• Android 在不同Actitity之间数据传递


    本文实现一个简易的人品计算器来实践在不同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>

    界面如下:

    输入姓名后,点击按钮跳转到另一个结果界面: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>

    界面如下:

    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);
    
        }
    }
  • 相关阅读:
    041_form表单重置数据reset()
    040_下拉列表的显示与提交数值时,需要用到转义字符
    039_如何选取checkbox的id值?
    011_表单数据非空验证
    010_@ResposBody错误
    010_页面单击按钮失灵
    使用Maven创建 web项目
    java设计模式(八) 适配器模式
    设计模式 6大设计原则
    Java设计模式(七) 模板模式-使用钩子
  • 原文地址:https://www.cnblogs.com/wuyudong/p/5679191.html
Copyright © 2020-2023  润新知