照相

package com.joinhead.activity;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.bawei.area.City_cnActivity;
import com.bawei.datetimepicker.DateTimePickDialogUtil;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;

@SuppressLint("ShowToast")
public class PersonalInformationActivity extends Activity {
 
    /* 头像文件 */
    private static final String IMAGE_FILE_NAME = "temp_head_image.jpg";

    /* 请求识别码 */
    private static final int CODE_GALLERY_REQUEST = 0xa0;
    private static final int CODE_CAMERA_REQUEST = 0xa1;
    private static final int CODE_RESULT_REQUEST = 0xa2;

    // 裁剪后图片的宽(X)和高(Y),480 X 480的正方形。
    private static int output_X = 480;
    private static int output_Y = 480;

    @SuppressLint("ShowToast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_information_scrollview);
        //找控件方法
        initViewId();
        //需要初始化的数据方法
        initData();
        OnClickListener l = new OnClickListener() {

            @SuppressLint("ShowToast")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //给返回图标加监听
                if (v.getId() == R.id.back) {
                    new AlertDialog.Builder

(PersonalInformationActivity.this)
                            .setTitle("系统提示")
                            // 设置对话框标题
                            .setMessage("您所有数据都没

保存确定要退出!")
                            // 设置显示的内容
                            .setPositiveButton("确定",
                                    new

DialogInterface.OnClickListener() {// 添加确定按钮

                                        

@Override
                                        

public void onClick(
                                            

    DialogInterface dialog,
                                            

    int which) {
                                            

// 确定按钮的响应事件

                                            

// TODO Auto-generated method stub

                                            

Intent intent = new Intent();
                                            

intent.putExtra("back", "返回");
                                            

setResult(0, intent);
                                            

finish();

                                        }

                                    })
                            .setNegativeButton("返回",
                                    new

DialogInterface.OnClickListener() {// 添加返回按钮

                                        

@Override
                                        

public void onClick(
                                            

    DialogInterface dialog,
                                            

    int which) {// 响应事件

                                            

// TODO Auto-generated method stub
                                        }

                                    }).show

();// 在按键响应事件中显示此对话框

                }
                //保存或者保存并填写下一项
                else if (v.getId() == R.id.save || v.getId() ==

R.id.submit) {
                    int id = v.getId();
                    String name = uesr_name.getText().toString

();
                    String phone = myPhone.getText().toString

();
                    String mailbox = mymailbox.getText

().toString();
                    String strbirthdata = birthdata.getText

().toString();
                    String strmyaccount = myaccount.getText

().toString();
                    String strmycity = mycity.getText

().toString();
                    String strjobs_data = jobs_data.getText

().toString();
                    if (name != null && !name.equals("") &&

phone != null
                            && !phone.equals("") &&

mailbox != null
                            && !mailbox.equals("") &&

strbirthdata != null
                            && !strbirthdata.equals("")

&& strmyaccount != null
                            && !strmyaccount.equals("")

&& strmycity != null
                            && !strmycity.equals("") &&

strjobs_data != null
                            && !strjobs_data.equals

("")) {
                        if (isMobileNO(phone)) {
                            if (isEmail(mailbox)) {
                                if (id ==

R.id.save) {
                                    Intent

intent = new Intent();
                                    

intent.putExtra("back", "完整");
                                    setResult

(0, intent);
                                    finish();
                                    

Toast.makeText(
                                            

PersonalInformationActivity.this,
                                            

"-" + name + "-" + phone + "-"
                                            

        + mailbox + "-" + sax, 000)
                                            

.show();
                                } else {
                                    Intent

intent = new Intent();
                                    

intent.putExtra("b", true);
                                    

intent.putExtra("back", "完整");
                                    setResult

(0, intent);
                                    finish();
                                    
                                }
                            } else {
                                Toast.makeText(
                                        

PersonalInformationActivity.this,
                                        "邮

箱格式错误", 9999).show();
                            }
                        } else {
                            Toast.makeText

(PersonalInformationActivity.this,
                                    "电话格式错

误", 9999).show();
                        }

                    } else {
                        Toast.makeText

(PersonalInformationActivity.this,
                                "姓名,电话,邮箱不

能为空", 9999).show();
                    }
                } else if (v.getId() == R.id.rl_birthdata) {
                    DateTimePickDialogUtil dateTimePicKDialog =

new DateTimePickDialogUtil(
                            

PersonalInformationActivity.this);
                    dateTimePicKDialog.dateTimePicKDialog

(birthdata,"出生日期");
                } else if (v.getId() == R.id.rl_jobs_data) {
                    DateTimePickDialogUtil dateTimePicKDialog =

new DateTimePickDialogUtil(
                            

PersonalInformationActivity.this);
                    dateTimePicKDialog.dateTimePicKDialog

(jobs_data,"参加工作时间:");
                } else if (v.getId() == R.id.rl_boy) {
                    sax = tv_boy.getText().toString();
                    rl_boy.setBackgroundColor(Color.BLUE);
                    rl_girl.setBackgroundColor(Color.WHITE);
                } else if (v.getId() == R.id.rl_girl) {
                    sax = tv_girlf.getText().toString();
                    rl_boy.setBackgroundColor(Color.WHITE);
                    rl_girl.setBackgroundColor(Color.BLUE);
                }
                // 现居住城市:
                else if (v.getId() == R.id.rl_mycity) {
                    Intent it = new Intent

(PersonalInformationActivity.this,
                            City_cnActivity.class);
                    it.putExtra("requestCode", 420);
                    startActivityForResult(it, 420);
                }
                // 户口所在地:
                else if (v.getId() == R.id.rl_myaccount) {
                    Intent it = new Intent

(PersonalInformationActivity.this,
                            City_cnActivity.class);
                    it.putExtra("requestCode", 807);
                    startActivityForResult(it, 807);
                }
                //图片监听
                else if (v.getId() == R.id.iv_HeadPortrait) {
                    initDialog();

                }
            }
        };
        back.setOnClickListener(l);
        save.setOnClickListener(l);
        submit.setOnClickListener(l);
        rl_birthdata.setOnClickListener(l);
        rl_jobs_data.setOnClickListener(l);
        rl_boy.setOnClickListener(l);
        rl_girl.setOnClickListener(l);
        rl_mycity.setOnClickListener(l);
        rl_myaccount.setOnClickListener(l);
        iv_HeadPortrait.setOnClickListener(l);
    }

    private void initData() {
        // TODO Auto-generated method stub
        rl_boy.setBackgroundColor(Color.BLUE);
        sax = tv_boy.getText().toString();
    }

    // 验证手机号码
    public static boolean isMobileNO(String mobiles) {
        /*
         * 移动:134、135、136、137、138、139、150、151、157(TD)、158、159

、187、188
         * 联通:130、131、132、152、155、156、185、186 电信:133、153、180

、189、(1349卫通)
         * 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为

0-9
         */
        String telRegex = "[1][3578]\\d{9}";// "[1]"代表第1位为数字

1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9

位。
        if (TextUtils.isEmpty(mobiles))
            return false;
        else
            return mobiles.matches(telRegex);
    }

    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?

[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    private void initViewId() {
        back = (ImageView) super.findViewById(R.id.back);
        save = (TextView) super.findViewById(R.id.save);
        iv_HeadPortrait = (ImageView) super.findViewById

(R.id.iv_HeadPortrait);
        uesr_name = (EditText) super.findViewById(R.id.uesr_name);
        rl_boy = (RelativeLayout) super.findViewById(R.id.rl_boy);
        rl_girl = (RelativeLayout) super.findViewById(R.id.rl_girl);
        tv_boy = (TextView) super.findViewById(R.id.tv_boy);
        tv_girlf = (TextView) super.findViewById(R.id.tv_girlf);
        myPhone = (EditText) super.findViewById(R.id.myPhon);
        mymailbox = (EditText) super.findViewById(R.id.mymailbox);
        submit = (Button) super.findViewById(R.id.submit);
        rl_birthdata = (RelativeLayout) super.findViewById

(R.id.rl_birthdata);
        rl_myaccount = (RelativeLayout) super.findViewById

(R.id.rl_myaccount);
        rl_mycity = (RelativeLayout) super.findViewById(R.id.rl_mycity);
        rl_jobs_data = (RelativeLayout) super.findViewById

(R.id.rl_jobs_data);
        birthdata = (TextView) super.findViewById(R.id.birthdata);
        myaccount = (TextView) super.findViewById(R.id.myaccount);
        mycity = (TextView) super.findViewById(R.id.mycity);
        jobs_data = (TextView) super.findViewById(R.id.jobs_data);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent

data) {
        // TODO Auto-generated method stub
        // 用户没有进行有效的设置操作,返回
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplication(), "取消",

Toast.LENGTH_LONG).show();
            return;
        }
     

     

if (requestCode == CODE_GALLERY_REQUEST) {

            cropRawPhoto(data.getData());

        } else if (requestCode == CODE_CAMERA_REQUEST) {
            if (hasSdcard()) {
                File tempFile = new File(
                        

Environment.getExternalStorageDirectory(),
                        IMAGE_FILE_NAME);
                cropRawPhoto(Uri.fromFile(tempFile));
            } else {
                Toast.makeText(getApplication(), "没有SDCard!",

Toast.LENGTH_LONG)
                        .show();
            }
        } else if (requestCode == CODE_RESULT_REQUEST) {
            if (data != null) {
                setImageToHeadView(data);
            }
        }
    }

    /**
     * 裁剪原始的图片
     */
    public void cropRawPhoto(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");

        // 设置裁剪
        intent.putExtra("crop", "true");

        // aspectX , aspectY :宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX , outputY : 裁剪图片宽高
        intent.putExtra("outputX", output_X);
        intent.putExtra("outputY", output_Y);
        intent.putExtra("return-data", true);

        startActivityForResult(intent, CODE_RESULT_REQUEST);
    }

    /**
     * 提取保存裁剪之后的图片数据,并设置头像部分的View
     */
    private void setImageToHeadView(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            iv_HeadPortrait.setImageBitmap(photo);

        }
    }

    /**
     * 检查设备是否存在SDCard的工具方法
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 有存储的SDCard
            return true;
        } else {
            return false;
        }
    }

    public void initDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                PersonalInformationActivity.this);
        builder.setTitle("提示");

        builder.setIcon(R.drawable.ic_launcher);
        builder.setPositiveButton("相册", new

DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                // 从本地相册选取图片作为头像
                Intent intentFromGallery = new Intent();
                // 设置文件类型
                intentFromGallery.setType("image/*");
                intentFromGallery.setAction

(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intentFromGallery,

CODE_GALLERY_REQUEST);
            }
        });
        builder.setNegativeButton("拍照", new

DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                Intent intentFromCapture = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);

                // 判断存储卡是否可用,存储照片文件
                if (hasSdcard()) {
                    intentFromCapture.putExtra

(MediaStore.EXTRA_OUTPUT, Uri
                            .fromFile(new File

(Environment
                                    

.getExternalStorageDirectory(),
                                    

IMAGE_FILE_NAME)));
                }

                startActivityForResult(intentFromCapture,

CODE_CAMERA_REQUEST);
                // dialog.dismiss();
            }
        });
        builder.setNeutralButton("返回", new

DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        builder.show();
    }

}



<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值