diff --git a/README.md b/README.md index e0a6444..94aa4b6 100644 --- a/README.md +++ b/README.md @@ -30,4 +30,20 @@ verificationCodeView.setOnClickListener(new View.OnClickListener() { } }); -``` \ No newline at end of file +``` + +####请求网络验证码时 + +``` + +``` + + +Thanks + +[CaptchaImageView](https://github.com/jineshfrancs/CaptchaImageView) \ No newline at end of file diff --git a/app/src/main/java/sgffsg/com/verifycodeview/MainActivity.java b/app/src/main/java/sgffsg/com/verifycodeview/MainActivity.java index 9a14e57..d55fc44 100644 --- a/app/src/main/java/sgffsg/com/verifycodeview/MainActivity.java +++ b/app/src/main/java/sgffsg/com/verifycodeview/MainActivity.java @@ -1,26 +1,48 @@ package sgffsg.com.verifycodeview; +import android.os.Handler; +import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.FrameLayout; +import android.widget.ProgressBar; import android.widget.Toast; +import java.util.Random; + public class MainActivity extends AppCompatActivity { - private VerificationCodeView verificationCodeView; - private Button btnSubmit; - private EditText edit_input; + private VerificationCodeView verificationCodeView,net_verificationCodeView; + private Button btnSubmit,btnNetSubmit; + private EditText edit_input,net_edit_input; + private ProgressBar progressBar; + private FrameLayout netVcViewLayout; + private Handler handler=new Handler(){ + @Override + public void handleMessage(Message msg) { + String code= (String) msg.obj; + progressBar.setVisibility(View.GONE); + net_verificationCodeView.setVisibility(View.VISIBLE); + net_verificationCodeView.setvCode(code); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); verificationCodeView = (VerificationCodeView) findViewById(R.id.verifycodeview); + net_verificationCodeView = (VerificationCodeView) findViewById(R.id.net_verifycodeview); btnSubmit= (Button) findViewById(R.id.btn_submit); + btnNetSubmit= (Button) findViewById(R.id.net_btn_submit); edit_input= (EditText) findViewById(R.id.edit_input); + net_edit_input= (EditText) findViewById(R.id.net_edit_input); + progressBar= (ProgressBar) findViewById(R.id.net_pregressbar); + netVcViewLayout= (FrameLayout) findViewById(R.id.net_verifycodeview_layut); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -40,9 +62,66 @@ public void onClick(View view) { verificationCodeView.refreshCode(); } }); - } + btnNetSubmit.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String input=net_edit_input.getText().toString().trim().toLowerCase(); + String code= net_verificationCodeView.getvCode().toLowerCase(); + if (!TextUtils.isEmpty(input)&&input.equals(code)){ + Toast.makeText(MainActivity.this,"Verify Success!Welcome",Toast.LENGTH_SHORT).show(); + }else { + Toast.makeText(MainActivity.this,"Verification code error!Try Again",Toast.LENGTH_SHORT).show(); + } + } + }); + netVcViewLayout.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + net_edit_input.setText(""); + progressBar.setVisibility(View.VISIBLE); + net_verificationCodeView.setVisibility(View.GONE); + loadNetCode(); + } + }); + loadNetCode(); + } + /** + * 模拟加载网络验证码 + * load network verification code + */ + private void loadNetCode(){ + handler.postDelayed(new Runnable() { + @Override + public void run() { + Message message=handler.obtainMessage(); + message.obj=getCharAndNumr(); + handler.sendMessage(message); + } + },1000); + } + /** + * java生成随机数字和字母组合 + * @return 随机验证码 + */ + public String getCharAndNumr() { + String val = ""; + Random random = new Random(); + for (int i = 0; i < 4; i++) { + // 输出字母还是数字 + String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; + // 字符串 + if ("char".equalsIgnoreCase(charOrNum)) { + // 取得大写字母还是小写字母 + int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; + val += (char) (choice + random.nextInt(26)); + } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字 + val += String.valueOf(random.nextInt(10)); + } + } + return val; + } } diff --git a/app/src/main/java/sgffsg/com/verifycodeview/VerificationCodeView.java b/app/src/main/java/sgffsg/com/verifycodeview/VerificationCodeView.java index 0aa3191..67a4bd0 100644 --- a/app/src/main/java/sgffsg/com/verifycodeview/VerificationCodeView.java +++ b/app/src/main/java/sgffsg/com/verifycodeview/VerificationCodeView.java @@ -1,6 +1,7 @@ package sgffsg.com.verifycodeview; import android.content.Context; +import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; @@ -49,6 +50,7 @@ public class VerificationCodeView extends View { private int mWidth; //控件高度 private int mHeight; + private boolean isInited=false;//是否初始化完成 private Bitmap mbitmap; private Bitmap codebitmap; @@ -58,6 +60,7 @@ public class VerificationCodeView extends View { private ArrayList mPaths = new ArrayList(); private float offset=5;//扭曲偏移 private String vCode; + private boolean isNetCode=false;//是否是网络请求到验证码 public VerificationCodeView(Context context) { this(context,null); @@ -69,6 +72,7 @@ public VerificationCodeView(Context context, AttributeSet attrs) { public VerificationCodeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + init(attrs); initView(); } @@ -102,9 +106,15 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { mBounds=new RectF(getLeft(),getTop(),getRight(),getBottom()); mWidth= (int) (mBounds.right-mBounds.left); mHeight= (int) (mBounds.bottom-mBounds.top); + isInited=true; createCodeBitmap(); } + private void init(AttributeSet attrs) { + TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.VerificationCodeView); + isNetCode = typedArray.getBoolean(R.styleable.VerificationCodeView_isNetCode,false); + } + /** * 初始化 */ @@ -143,6 +153,8 @@ protected void onDraw(Canvas canvas) { * 生成验证码图片 */ private void createCodeBitmap() { + if (!isInited) + return; mPaths.clear(); // 生成干扰线坐标 for(int i=0;i<2;i++){ @@ -162,20 +174,20 @@ private void createCodeBitmap() { tempCode=getCharAndNumr(); //画背景 myCanvas.drawColor(YELLOW_BG_COLOR); - - textPaint.getTextBounds(tempCode,0,codeNum,textBound); - float charLength=(textBound.width())/codeNum; - for (int i=0;i0){ + textPaint.getTextBounds(tempCode,0,codeNum,textBound); + float charLength=(textBound.width())/codeNum; + for (int i=0;i + + + + + + +