码迷,mamicode.com
首页 > 其他好文 > 详细

Intent的使用传递数据

时间:2016-11-12 23:27:12      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:source   return   ica   public   aced   ini   rem   人脸识别   分享   

直接看效果图:

  技术分享技术分享技术分享技术分享技术分享

  部分代码:  

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:paddingBottom="25dp"
            android:paddingLeft="15dp"
            android:paddingTop="25dp"
            android:text="头像" />

        <ImageView
            android:id="@+id/photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:scaleType="fitCenter"
            android:onClick="onClick"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="25dp"
            android:paddingTop="25dp"
            android:src="@mipmap/jiantou" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:paddingBottom="25dp"
            android:paddingLeft="15dp"
            android:paddingTop="25dp"
            android:text="昵称" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="right"
            android:paddingBottom="25dp"
            android:paddingTop="25dp"
            android:onClick="onClick"
            android:text="张三" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="25dp"
            android:paddingTop="25dp"
            android:src="@mipmap/jiantou" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:paddingBottom="25dp"
            android:paddingLeft="15dp"
            android:paddingTop="25dp"
            android:text="手机号" />

        <TextView
            android:id="@+id/tv_telphone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="right"
            android:paddingBottom="25dp"
            android:paddingTop="25dp"
            android:onClick="onClick"
            android:text="未填写" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="25dp"
            android:paddingTop="25dp"
            android:src="@mipmap/jiantou" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:paddingBottom="25dp"
            android:paddingLeft="15dp"
            android:paddingTop="25dp"
            android:text="邮箱" />

        <TextView
            android:id="@+id/tv_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="right"
            android:paddingBottom="25dp"
            android:paddingTop="25dp"
            android:onClick="onClick"
            android:text="110@163.com" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="25dp"
            android:paddingTop="25dp"
            android:src="@mipmap/jiantou" />
    </LinearLayout>

</LinearLayout>

MainActivity.class文件

package com.dc.lesson07_works;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
    private static final int PHOTO_REQUEST_CUT = 3;// 结果
    private ImageView photo;

    /* 头像名称 */
    private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
    private File tempFile;


    private static final int TO_ABCTIVITY = 30;
    private TextView tv_name;
    private TextView tv_telphone;
    private TextView tv_email;
    Intent intent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_telphone = (TextView) findViewById(R.id.tv_telphone);
        tv_email = (TextView) findViewById(R.id.tv_email);
        photo = (ImageView) findViewById(R.id.photo);
        photo.setOnClickListener(this);
        tv_name.setOnClickListener(this);
        tv_telphone.setOnClickListener(this);
        tv_email.setOnClickListener(this);
    }
    /*
            * 剪切图片
            */
    private void crop(Uri uri) {
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例,1:1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);

        intent.putExtra("outputFormat", "JPEG");// 图片格式
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra("return-data", true);
        // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    /*
    * 判断sdcard是否被挂载
    */
    private boolean hasSdcard() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.photo:
                // 激活系统图库,选择一张图片
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
                startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
                break;

            case R.id.tv_name:
                intent = new Intent(this, SeconedActivity.class);
                intent.putExtra("tv_name", tv_name.getText().toString());
                Toast.makeText(this, "" + tv_name.getText().toString(), Toast.LENGTH_SHORT).show();
                intent.putExtra("source", "name");
                startActivityForResult(intent, 1);
                break;
            case R.id.tv_telphone:
                intent = new Intent(this, SeconedActivity.class);
                intent.putExtra("tv_phone", tv_telphone.getText().toString());
                Toast.makeText(this, "" + tv_telphone.getText().toString(), Toast.LENGTH_SHORT).show();
                intent.putExtra("source", "phone");
                startActivityForResult(intent, 1);
                break;
            case R.id.tv_email:
                intent = new Intent(this, SeconedActivity.class);
                intent.putExtra("tv_email", tv_email.getText().toString());
                Toast.makeText(this, "" + tv_email.getText().toString(), Toast.LENGTH_SHORT).show();
                intent.putExtra("source", "email");
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            tv_name.setText(data.getStringExtra("content"));
        }
        if (requestCode == 1 && resultCode == 2) {
            tv_telphone.setText(data.getStringExtra("content"));
        }
        if (requestCode == 1 && resultCode == 3) {
            tv_email.setText(data.getStringExtra("content"));

        }
        if (requestCode == PHOTO_REQUEST_GALLERY) {
            // 从相册返回的数据
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                crop(uri);
            }
        }  else if (requestCode == PHOTO_REQUEST_CUT) {
                // 从剪切图片返回的数据
                if (data != null) {
                    Bitmap bitmap = data.getParcelableExtra("data");
                    photo.setImageBitmap(bitmap);
                }
                try {
                    // 将临时文件删除
                    tempFile.delete();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

secondActivity:

package com.dc.lesson07_works;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 *
 *
 *         2016年11月10日 下午5:10:09
 */
public class SeconedActivity extends Activity {
    EditText edit;
    String source;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seconed);
        initView();

    }

    private void initView() {
        source = getIntent().getStringExtra("source");
        edit = (EditText) findViewById(R.id.edit);
        btn = (Button) findViewById(R.id.btn);
        //获取启动此界面的意图
        Intent intent = getIntent();
        //获取启动此界面时存放的数据
        switch (source) {
            case "name":
                String naems=intent.getStringExtra("tv_name");
                edit.setText(naems);
                Toast.makeText(this, "==========="+naems, Toast.LENGTH_SHORT).show();
                edit.setHint("请输入用户名");
                break;
            case "phone":
                String phones=intent.getStringExtra("tv_phone");
                edit.setText(phones);
                Toast.makeText(this, "==========="+phones, Toast.LENGTH_SHORT).show();
                edit.setHint("请输入电话");
                break;
            case "email":
                String emails=intent.getStringExtra("tv_email");
                edit.setText(emails);
                Toast.makeText(this, "==========="+emails, Toast.LENGTH_SHORT).show();
                edit.setHint("请输入邮箱");
                break;
        }

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("content", edit.getText().toString());
                switch (source) {
                    case "name":
                        setResult(1, intent);
                        break;
                    case "phone":
                        setResult(2, intent);
                        break;
                    case "email":
                        setResult(3, intent);
                        break;
                }

                finish();
            }
        });
    }
}

代码不是最简便的,功能可以实现,

Intent的使用传递数据

标签:source   return   ica   public   aced   ini   rem   人脸识别   分享   

原文地址:http://www.cnblogs.com/dingxiansen/p/6057665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!