/** * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.fanxin.huangfangyi.main.activity; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.text.Editable; import android.text.Html; import android.text.Selection; import android.text.Spannable; import android.text.TextWatcher; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.alibaba.fastjson.JSONObject; import com.fanxin.huangfangyi.R; import com.fanxin.huangfangyi.main.FXConstant; import com.fanxin.huangfangyi.main.utils.Param; import com.fanxin.huangfangyi.main.utils.OkHttpManager; import com.fanxin.huangfangyi.main.widget.FXAlertDialog; import com.fanxin.huangfangyi.ui.BaseActivity; /** * 注册页 */ public class RegisterActivity extends BaseActivity { private EditText et_usernick; private EditText et_usertel; private EditText et_password; private Button btn_register; private TextView tv_xieyi; private ImageView iv_hide; private ImageView iv_show; private ImageView iv_photo; private String imageName = "false"; private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fx_activity_register); et_usernick = (EditText) findViewById(R.id.et_usernick); et_usertel = (EditText) findViewById(R.id.et_usertel); et_password = (EditText) findViewById(R.id.et_password); // 监听多个输入框 et_usernick.addTextChangedListener(new TextChange()); et_usertel.addTextChangedListener(new TextChange()); et_password.addTextChangedListener(new TextChange()); btn_register = (Button) findViewById(R.id.btn_register); tv_xieyi = (TextView) findViewById(R.id.tv_xieyi); iv_hide = (ImageView) findViewById(R.id.iv_hide); iv_show = (ImageView) findViewById(R.id.iv_show); iv_photo = (ImageView) findViewById(R.id.iv_photo); String xieyi = "<font color=" + "\"" + "#AAAAAA" + "\">" + "点击上面的" + "\"" + "注册" + "\"" + "按钮,即表示你同意" + "</font>" + "<u>" + "<font color=" + "\"" + "#576B95" + "\">" + "《腾讯微信软件许可及服务协议》" + "</font>" + "</u>"; tv_xieyi.setText(Html.fromHtml(xieyi)); iv_hide.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { iv_hide.setVisibility(View.GONE); iv_show.setVisibility(View.VISIBLE); et_password .setTransformationMethod(HideReturnsTransformationMethod .getInstance()); // 切换后将EditText光标置于末尾 CharSequence charSequence = et_password.getText(); if (charSequence instanceof Spannable) { Spannable spanText = (Spannable) charSequence; Selection.setSelection(spanText, charSequence.length()); } } }); iv_show.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { iv_show.setVisibility(View.GONE); iv_hide.setVisibility(View.VISIBLE); et_password .setTransformationMethod(PasswordTransformationMethod .getInstance()); // 切换后将EditText光标置于末尾 CharSequence charSequence = et_password.getText(); if (charSequence instanceof Spannable) { Spannable spanText = (Spannable) charSequence; Selection.setSelection(spanText, charSequence.length()); } } }); iv_photo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showCamera(); } }); btn_register.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String usernick = et_usernick.getText().toString().trim(); String password = et_password.getText().toString().trim(); String usertel = et_usertel.getText().toString().trim(); register(usernick, password, usertel); } }); } private void register(String usernick, String password, String usertel) { final ProgressDialog pd = new ProgressDialog(RegisterActivity.this); pd.setCanceledOnTouchOutside(false); pd.setMessage("正在注册..."); pd.show(); File file = new File(FXConstant.DIR_AVATAR + imageName); List<Param> params = new ArrayList<Param>(); params.add(new Param("usertel", usertel)); params.add(new Param("password", password)); params.add(new Param("usernick", usernick)); params.add(new Param("image", imageName)); List<File> files = new ArrayList<File>(); files.add(file); OkHttpManager.getInstance().post(params, files, FXConstant.URL_REGISTER, new OkHttpManager.HttpCallBack() { @Override public void onResponse(JSONObject jsonObject) { pd.dismiss(); int code = jsonObject.getInteger("code"); if (code == 1000) { Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT) .show(); finish(); } else if (code == 2000) { pd.dismiss(); Toast.makeText(RegisterActivity.this, "该手机号码已被注册...", Toast.LENGTH_SHORT) .show(); } else { pd.dismiss(); Toast.makeText(RegisterActivity.this, "服务器繁忙请重试...", Toast.LENGTH_SHORT) .show(); } } @Override public void onFailure(String errorMsg) { pd.dismiss(); } }); } // 拍照部分 private void showCamera() { List<String> items = new ArrayList<String>(); items.add("拍照"); items.add("相册"); FXAlertDialog fxAlertDialog = new FXAlertDialog(RegisterActivity.this, null, items); fxAlertDialog.init(new FXAlertDialog.OnItemClickListner() { @Override public void onClick(int position) { switch (position) { case 0: imageName = getNowTime() + ".png"; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 指定调用相机拍照后照片的储存路径 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(FXConstant.DIR_AVATAR, imageName))); startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO); break; case 1: imageName = getNowTime() + ".png"; Intent intent2 = new Intent(Intent.ACTION_PICK, null); intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent2, PHOTO_REQUEST_GALLERY); break; } } }); } @SuppressLint("SdCardPath") @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case PHOTO_REQUEST_TAKEPHOTO: startPhotoZoom( Uri.fromFile(new File(FXConstant.DIR_AVATAR, imageName)), 480); break; case PHOTO_REQUEST_GALLERY: if (data != null) startPhotoZoom(data.getData(), 480); break; case PHOTO_REQUEST_CUT: // BitmapFactory.Options options = new BitmapFactory.Options(); // // /** // * 最关键在此,把options.inJustDecodeBounds = true; // * 这里再decodeFile(),返回的bitmap为空 // * ,但此时调用options.outHeight时,已经包含了图片的高了 // */ // options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(FXConstant.DIR_AVATAR + imageName); iv_photo.setImageBitmap(bitmap); break; } super.onActivityResult(requestCode, resultCode, data); } } private void startPhotoZoom(Uri uri1, int size) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri1, "image/*"); // crop为true是设置在开启的intent中设置显示的view可以剪裁 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁图片的宽高 intent.putExtra("outputX", size); intent.putExtra("outputY", size); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(FXConstant.DIR_AVATAR, imageName))); intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, PHOTO_REQUEST_CUT); } private String getNowTime() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat("MMddHHmmssSS"); return dateFormat.format(date); } // EditText监听器 class TextChange implements TextWatcher { @Override public void afterTextChanged(Editable arg0) { } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence cs, int start, int before, int count) { boolean Sign1 = et_usernick.getText().length() > 0; boolean Sign2 = et_usertel.getText().length() > 0; boolean Sign3 = et_password.getText().length() > 0; if (Sign1 & Sign2 & Sign3) { btn_register.setEnabled(true); } // 在layout文件中,对Button的text属性应预先设置默认值,否则刚打开程序的时候Button是无显示的 else { btn_register.setEnabled(false); } } } public void back(View view) { finish(); } }