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);
}
}
});
}
}