3.仿微信--注册界面 布局实现
2015-09-14 16:32
681 查看
首先,导入baidumapapi_v3_2_0.jar,easemobchat_2.1.5.jar,fastjson-1.1.34.android.jar,locSDK_3.3.jar把这些包导入到libs目录下。(主要是为了方便后边使用)
然后编写代码:
1.注册界面RegisterActivity.java
2.布局文件activity_register.xml
3.AndroidManifest.xml
进行注册
4.使用到的资源文件
dimens.xml
drawable:
top_bar_back.xml
edittext_login.xml
btn_register_bg.xml
yuanjiao_1.xml
yuanjiao_2.xml
yuanjiao_3.xml
5.最后,在登录界面LoginActivity.java添加如下代码
然后编写代码:
1.注册界面RegisterActivity.java
import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; 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.TextUtils; import android.text.TextWatcher; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.view.Window; 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.JSONException; import com.alibaba.fastjson.JSONObject; import com.easemob.EMError; import com.easemob.chat.EMChatManager; import com.easemob.exceptions.EaseMobException; import com.th.myphone.loginutils.DemoApplication; import com.th.myphone.loginutils.LoadDataFromServer; import com.th.myphone.loginutils.LoadDataFromServer.DataCallBack; /** * 注册页 * */ public class RegisterActivity extends Activity { 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; ProgressDialog dialog; private String imageName; 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.activity_register); dialog = new ProgressDialog(RegisterActivity.this); 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() { @SuppressLint("SdCardPath") @Override public void onClick(View v) { dialog.setMessage("正在注册..."); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.show(); String usernick = et_usernick.getText().toString().trim(); final String password = et_password.getText().toString().trim(); String usertel = et_usertel.getText().toString().trim(); Map<String, String> map = new HashMap<String, String>(); if ((new File("/sdcard/fanxin/" + imageName)).exists()) { map.put("file", "/sdcard/fanxin/" + imageName); map.put("image", imageName); } else { map.put("image", "false"); } map.put("usernick", usernick); map.put("usertel", usertel); map.put("password", password); LoadDataFromServer registerTask = new LoadDataFromServer( RegisterActivity.this, Constant.URL_Register_Tel, map); registerTask.getData(new DataCallBack() { @SuppressLint("ShowToast") @Override public void onDataCallBack(JSONObject data) { try { int code = data.getInteger("code"); if (code == 1) { String hxid = data.getString("hxid"); register(hxid, password); } else if (code == 2) { dialog.dismiss(); Toast.makeText(RegisterActivity.this, "该手机号码已被注册...", Toast.LENGTH_SHORT) .show(); } else if (code == 3) { dialog.dismiss(); Toast.makeText(RegisterActivity.this, "服务器端注册失败...", Toast.LENGTH_SHORT) .show(); } else if (code == 4) { dialog.dismiss(); Toast.makeText(RegisterActivity.this, "头像传输失败...", Toast.LENGTH_SHORT).show(); } else if (code == 5) { dialog.dismiss(); Toast.makeText(RegisterActivity.this, "返回环信id失败...", Toast.LENGTH_SHORT) .show(); } else { dialog.dismiss(); Toast.makeText(RegisterActivity.this, "服务器繁忙请重试...", Toast.LENGTH_SHORT) .show(); } } catch (JSONException e) { dialog.dismiss(); Toast.makeText(RegisterActivity.this, "数据解析错误...", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } }); } }); } // 拍照部分 private void showCamera() { final AlertDialog dlg = new AlertDialog.Builder(this).create(); dlg.show(); Window window = dlg.getWindow(); // *** 主要就是在这里实现这种效果的. // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容 window.setContentView(R.layout.alertdialog); // 为确认按钮添加事件,执行退出应用操作 TextView tv_paizhao = (TextView) window.findViewById(R.id.tv_content1); tv_paizhao.setText("拍照"); tv_paizhao.setOnClickListener(new View.OnClickListener() { @SuppressLint("SdCardPath") public void onClick(View v) { imageName = getNowTime() + ".png"; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 指定调用相机拍照后照片的储存路径 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/fanxin/", imageName))); startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO); dlg.cancel(); } }); TextView tv_xiangce = (TextView) window.findViewById(R.id.tv_content2); tv_xiangce.setText("相册"); tv_xiangce.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { getNowTime(); imageName = getNowTime() + ".png"; Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, PHOTO_REQUEST_GALLERY); dlg.cancel(); } }); } @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("/sdcard/fanxin/", 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("/sdcard/fanxin/" +imageName); iv_photo.setImageBitmap(bitmap); break; } super.onActivityResult(requestCode, resultCode, data); } } @SuppressLint("SdCardPath") 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("/sdcard/fanxin/", imageName))); intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, PHOTO_REQUEST_CUT); } @SuppressLint("SimpleDateFormat") 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.setTextColor(0xFFFFFFFF); btn_register.setEnabled(true); } // 在layout文件中,对Button的text属性应预先设置默认值,否则刚打开程序的时候Button是无显示的 else { btn_register.setTextColor(0xFFD0EFC6); btn_register.setEnabled(false); } } } /** * 注册 * * @param view */ public void register(final String hxid, final String password) { // String st1 = getResources().getString( // R.string.User_name_cannot_be_empty); // String st2 = getResources() // .getString(R.string.Password_cannot_be_empty); // String st3 = getResources().getString( // R.string.Confirm_password_cannot_be_empty); // String st4 = getResources().getString(R.string.Two_input_password); // String st5 = getResources().getString(R.string.Is_the_registered); final String st6 = getResources().getString( R.string.Registered_successfully); if (!TextUtils.isEmpty(hxid) && !TextUtils.isEmpty(password)) { final String st7 = getResources().getString( R.string.network_anomalies); final String st8 = getResources().getString( R.string.User_already_exists); final String st9 = getResources().getString( R.string.registration_failed_without_permission); final String st10 = getResources().getString( R.string.Registration_failed); new Thread(new Runnable() { public void run() { try { // 调用sdk注册方法 EMChatManager.getInstance().createAccountOnServer(hxid, password); runOnUiThread(new Runnable() { public void run() { if (!RegisterActivity.this.isFinishing()) dialog.dismiss(); // 保存用户名 DemoApplication.getInstance().setUserName(hxid); Toast.makeText(getApplicationContext(), st6, 0) .show(); finish(); } }); } catch (final EaseMobException e) { runOnUiThread(new Runnable() { public void run() { if (!RegisterActivity.this.isFinishing()) dialog.dismiss(); int errorCode = e.getErrorCode(); if (errorCode == EMError.NONETWORK_ERROR) { Toast.makeText(getApplicationContext(), st7, Toast.LENGTH_SHORT).show(); } else if (errorCode == EMError.USER_ALREADY_EXISTS) { Toast.makeText(getApplicationContext(), st8, Toast.LENGTH_SHORT).show(); } else if (errorCode == EMError.UNAUTHORIZED) { Toast.makeText(getApplicationContext(), st9, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), st10 + e.getMessage(), Toast.LENGTH_SHORT).show(); } } }); } } }).start(); } } public void back(View view) { finish(); } }
2.布局文件activity_register.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:background="#ffffff" android:orientation="vertical" > <RelativeLayout android:id="@+id/title" android:layout_width="match_parent" android:layout_height="@dimen/height_top_bar" android:background="@color/common_top_bar_blue" > <ImageView android:id="@+id/iv_back" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:onClick="back" android:paddingBottom="5dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="5dp" android:scaleType="centerInside" android:src="@drawable/top_bar_back" /> <View android:id="@+id/view_temp" android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:layout_toRightOf="@id/iv_back" android:background="#14191A" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/view_temp" android:text="填写手机号" android:textColor="#ffffff" android:textSize="18sp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="12dp" > <RelativeLayout android:layout_width="match_parent" android:layout_height="64dp" > <ImageView android:id="@+id/iv_photo" android:layout_width="64dp" android:layout_height="64dp" android:layout_alignParentRight="true" android:src="@drawable/icon_register" /> <EditText android:id="@+id/et_usernick" android:layout_width="match_parent" android:layout_height="48dp" android:layout_alignParentBottom="true" android:layout_marginRight="10dp" android:layout_toLeftOf="@id/iv_photo" android:background="@drawable/edittext_login" android:gravity="center_vertical" android:hint="例如:陈晨" android:paddingLeft="90dp" android:singleLine="true" android:textColorHint="#DDDDDD" android:textSize="16sp" android:textCursorDrawable="@null"/> <TextView android:id="@+id/tv_usernick" android:layout_width="90dp" android:layout_height="48dp" android:layout_alignLeft="@id/et_usernick" android:layout_alignParentBottom="true" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="昵称" android:textColor="#353535" android:textSize="16sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="国家和地区" android:textColor="#353535" android:textSize="16sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:text="中国" android:textColor="#45c01a" android:textSize="16sp" /> <View android:layout_width="match_parent" android:layout_height="0.1dp" android:layout_alignParentBottom="true" android:background="#dbdbdb" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" > <EditText android:id="@+id/et_usertel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="@drawable/edittext_login" android:hint="你的手机号码" android:inputType="phone" android:paddingLeft="90dp" android:singleLine="true" android:textColorHint="#DDDDDD" android:textSize="16sp" android:textCursorDrawable="@null"/> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:layout_alignLeft="@id/et_usertel" android:layout_centerVertical="true" android:layout_marginLeft="24dp" android:text="+86" android:textColor="#353535" android:textSize="16sp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" > <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="@drawable/edittext_login" android:hint="填写密码" android:paddingLeft="90dp" android:password="true" android:singleLine="true" android:textColorHint="#DDDDDD" android:textSize="16sp" android:textCursorDrawable="@null"/> <TextView android:id="@+id/tv_password" android:layout_width="90dp" android:layout_height="wrap_content" android:layout_alignLeft="@id/et_password" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="密码" android:textColor="#353535" android:textSize="16sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="44dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:orientation="horizontal" > <ImageView android:id="@+id/iv_hide" android:layout_width="44dp" android:layout_height="44dp" android:padding="10dp" android:src="@drawable/agt" /> <ImageView android:id="@+id/iv_show" android:layout_width="44dp" android:layout_height="44dp" android:padding="10dp" android:src="@drawable/agu" android:visibility="gone" /> </LinearLayout> </RelativeLayout> <Button android:id="@+id/btn_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:background="@drawable/btn_register_bg" android:enabled="false" android:paddingBottom="7dp" android:paddingTop="7dp" android:text="注册" android:textColor="@android:color/white" android:textSize="18sp" /> <TextView android:id="@+id/tv_xieyi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="14sp" /> </LinearLayout> </LinearLayout>
3.AndroidManifest.xml
进行注册
<activity android:name=".RegisterActivity"/>
4.使用到的资源文件
dimens.xml
<dimen name="height_top_bar">48dp</dimen>colors.xml
<color name="common_top_bar_blue">#22292c</color>
drawable:
top_bar_back.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/ac1" /> <item android:state_selected="true" android:drawable="@drawable/ac1" /> <item android:drawable="@drawable/ac0" /> </selector>
edittext_login.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/wo" android:state_focused="true"/> <item android:drawable="@drawable/wp"/> </selector>
btn_register_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/yuanjiao_3" android:state_pressed="true"/> <item android:drawable="@drawable/yuanjiao_1" android:state_enabled="false"/> <item android:drawable="@drawable/yuanjiao_2" android:state_enabled="true"/> <item android:drawable="@drawable/yuanjiao_1"/> </selector>
yuanjiao_1.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#A2E08D" /> <corners android:bottomLeftRadius="1dp" android:bottomRightRadius="1dp" android:topLeftRadius="1dp" android:topRightRadius="1dp" /> </shape>
yuanjiao_2.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#45C01A" /> <corners android:bottomLeftRadius="1dp" android:bottomRightRadius="1dp" android:topLeftRadius="1dp" android:topRightRadius="1dp" /> </shape>
yuanjiao_3.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#027A0B" /> <corners android:bottomLeftRadius="1dp" android:bottomRightRadius="1dp" android:topLeftRadius="1dp" android:topRightRadius="1dp" /> </shape>
5.最后,在登录界面LoginActivity.java添加如下代码
btn_qtlogin.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); } });
相关文章推荐
- 微信公众平台有可能代替App Store
- 微信的加入是在为原生态APP做增量
- 微信成为一个商户自助管理的开放平台
- 微信成了剩男剩女相亲的重要工具
- 微信公众平台深度开发Java版 v2.0
- 判断UserAgent是否来自微信
- 微信公众号开发问题与注意事项和解决方案
- 微信支付开发中遇到的问题以及注意事项和解决方案
- genymotion 4.2.2 支持微信、QQ
- 2.仿微信--登录界面(LoginActivity)布局界面的实现
- java给微信发送get和post请求
- 微信nickname乱码及mysql编码格式设置(utf8mb4)
- 【微信】绕过限制,在PC上调试微信投票页、砍价页
- Android 微信 支付 登录 注意问题
- java 小程序
- 微信成了剩男剩女相亲的重要工具
- 微信成为一个商户自助管理的开放平台
- 很多企业把微信当作时髦的营销工具
- 每个人都可以用QQ号码打造自己的微信公众账号
- 陪聊式微信对话满足了许多用户希望私密聊天的需求