ChangeElectronicCardAdressInfoActivity.java 6.25 KB
package com.hjx.personalcenter.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.hjx.personalcenter.R;
import com.hjx.personalcenter.http.HttpCode;
import com.hjx.personalcenter.http.HttpManager;
import com.hjx.personalcenter.util.AlertUtils;
import com.hjx.personalcenter.util.Judgment;
import com.hjx.personalcenter.util.PhoneNumCheckUtils;

import org.json.JSONObject;

/**
 * Created by h on 2017/8/9.
 */

public class ChangeElectronicCardAdressInfoActivity extends Activity implements View.OnClickListener{
    private ImageView back_forgit;
    private EditText forot_pwd_phone,forot_pwd_anthcode,adress,newadress;
    private Button forot_pwd_sub,forot_pwd_getanthcode;
    private String typeStr = "register";
    private int i = 0;
    private Thread  thread;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
                JSONObject jsonObject;
                String status;
                switch (msg.what) {
                    case HttpCode.AUTHCODE_SUCESS1:
                        jsonObject = (JSONObject) msg.obj;
                        status = jsonObject.optString("status");
                        if (status.equals("100")) {
                            AlertUtils.showToast(ChangeElectronicCardAdressInfoActivity.this, "验证码发送成功");
                        }
                        //AlertUtils.showToast(ForgotPassword.this, jsonObject.optString("message"));
                        break;
                    case HttpCode.AUTHCODE_FAIL1:
                        jsonObject = (JSONObject) msg.obj;
                        AlertUtils.showToast(ChangeElectronicCardAdressInfoActivity.this, jsonObject.optString("验证码发送失败,请检查网络"));
                        break;
                    case 1:
                        forot_pwd_getanthcode.setEnabled(false);
                        forot_pwd_getanthcode.setClickable(false);
                        forot_pwd_getanthcode.setText(Integer.toString(i--)+" s");
                        if (i<=0){
                            forot_pwd_getanthcode.setEnabled(true);
                            forot_pwd_getanthcode.setClickable(true);
                            forot_pwd_getanthcode.setText("获取验证码");

                        }
                        break;
                }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_card_validation);
        initView();
        setLister();
    }
    //获取验证码
    private void getauthcode() {
        forot_pwd_anthcode.requestFocus();
        String forot_pwd_phone1 = forot_pwd_phone.getText().toString().trim();
        HttpManager.getInstance().authCode(typeStr, forot_pwd_phone1, handler, this);
        i = 60;
        if(thread == null){
            thread = new Thread( new ThreadShow());
            thread.start();
        }

    }
    private void initView() {
        forot_pwd_phone = (EditText) findViewById(R.id.et_phonenumber);
        forot_pwd_anthcode = (EditText) findViewById(R.id.et_authcode);
        adress = (EditText) findViewById(R.id.et_newpassword);
        newadress = (EditText) findViewById(R.id.et_again_newpassword);
        forot_pwd_sub = (Button) findViewById(R.id.btn_ok);
        forot_pwd_getanthcode = (Button) findViewById(R.id.btn_authcode);
        back_forgit= (ImageView) findViewById(R.id.cancel);
    }
    private void setLister() {
        back_forgit.setOnClickListener(this);
        forot_pwd_sub.setOnClickListener(this);
        forot_pwd_getanthcode.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_ok:
                String forot_pwd_phone1 = forot_pwd_phone.getText().toString().trim();
                String forot_pwd_anthcode1 = forot_pwd_anthcode.getText().toString().trim();
                String forot_pwd_pwd3 = adress.getText().toString().trim();
                String forot_pwd_pwd4 = newadress.getText().toString().trim();
                if (TextUtils.isEmpty(forot_pwd_phone1)||
                        TextUtils.isEmpty(forot_pwd_pwd3) || TextUtils.isEmpty(forot_pwd_pwd4)){
                    AlertUtils.showToast(ChangeElectronicCardAdressInfoActivity.this, "请将必填项填写完整");
                    return;
                }else if (Judgment.getInstance().isPhoneNum(forot_pwd_phone1)){
                    AlertUtils.showToast(ChangeElectronicCardAdressInfoActivity.this, "手机号码输入有误");

                } else {
                    //修改地址接口
                    HttpManager.getInstance().changecardadressinfo(this,111,forot_pwd_phone1,forot_pwd_anthcode1,forot_pwd_pwd3+forot_pwd_pwd4);
                }
                break;
            case R.id.btn_authcode:
                forot_pwd_phone1 = forot_pwd_phone.getText().toString().trim();
                if (!PhoneNumCheckUtils.isPhone(forot_pwd_phone1)){
                    AlertUtils.showToast(ChangeElectronicCardAdressInfoActivity.this, "请输入正确的手机号");
                }else {
                    forot_pwd_anthcode.requestFocus();
                    getauthcode();

                }

                break;

            case R.id.cancel:
                finish();
                break;
        }
    }
    // 线程类 定时器
    class ThreadShow implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                    System.out.println("send...");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("thread error...");
                }
            }
        }
    }

}