android 驗證碼,稍微修改

來源:互聯網
上載者:User

package com.zcwfeng.identify;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class VerifyCodeView extends View {

    Paint mPaint;
    private final int TEXT_SIZE = 20;
    
    public VerifyCodeView(Context context) {
        super(context);
        init();
    }

    public VerifyCodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VerifyCodeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mPaint = new Paint();
        /* Sawtooth */
        mPaint.setAntiAlias(false);
        /* Set the frame width of the paint */
        mPaint.setStrokeWidth(4);
        // set the font sieze
        mPaint.setTextSize(TEXT_SIZE);
        mPaint.setTextAlign(Paint.Align.CENTER);
    }

    protected void onDraw(Canvas canvas) {
        /* set backgroud clolr */
        canvas.drawColor(Color.GRAY);

        canvas.save();
        canvas.translate(10, 10);
        /* set paint color */
        mPaint.setColor(Color.BLUE);
        // build random code
        Random random = new Random();
        String sRand = "";
        for (int i = 0; i < 4; i++) {
            String rand = String.valueOf(random.nextInt(10));
            canvas.drawText(rand, TEXT_SIZE * i + 4, 10, mPaint);
            sRand += rand;
        }
        saveCode(sRand);
        canvas.restore();
    }

    /**
     * Save coding in here
     *
     * @param code
     */
    private void saveCode(String code) {
        // TODO
        ViewConstant.IDENTIFY_CODE_VALUE = code;
    }
}

========================================

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:Textview="http://schemas.android.com/apk/res/com.zcwfeng.identify"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="fill_parent">
        <TextView android:id="@+id/txt_date" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Verify" />
        <EditText android:id="@+id/identifyCode"
            android:layout_width="100dp" android:layout_height="wrap_content"
            android:layout_marginLeft="8dp" android:layout_marginRight="8dp" />
        <com.zcwfeng.identify.VerifyCodeView
            android:layout_width="90dp" android:layout_height="28dp"
            android:layout_marginTop="10dp">
        </com.zcwfeng.identify.VerifyCodeView>

        <Button android:id="@+id/print" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="點我驗證"/>
    </LinearLayout>
</LinearLayout>

===========================

package com.zcwfeng.identify;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.print);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText editText = (EditText) findViewById(R.id.identifyCode);
                String code = editText.getText().toString();

                if (code.equals(ViewConstant.IDENTIFY_CODE_VALUE)) {
                    System.err.println(code);
                    Toast.makeText(MainActivity.this, "ok--->" + code, Toast.LENGTH_LONG);

                }else {
                    
                    System.err.println("error------>"+code);
                }
            }
        });

    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.