C#遞歸遍歷窗體一切textbox控件並設置textbox事宜的辦法。本站提示廣大學習愛好者:(C#遞歸遍歷窗體一切textbox控件並設置textbox事宜的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#遞歸遍歷窗體一切textbox控件並設置textbox事宜的辦法正文
明天偶遇以github上gesturelock關於手勢鎖的一個例子(有興致的去搜刮下看看),因而下載上去研討,無法根本沒有正文,代碼上存在一些成績(當設置gravity=center_vertical沒法停止手勢選擇,有意中發明的),因而自創這位仁兄的代碼,本身重寫寫了一個,修復了一些成績,參加一些根本的自界說屬性,在此先感激這位兄弟~。
先上圖,默許後果圖:

固然可以自界說數目啊,色彩神馬的,自界說後果圖:


假如你有藝術細胞,可以給我推舉幾個色彩,無法小我審美有成績~
1、全體思緒
a、自界說了一個RelativeLayout(GestureLockViewGroup)在外面會依據傳入的每行的個數,生成多個GestureLockView(就是下面一個個小圈圈),然後會主動停止結構,外面的寬度,間距,內圓的直徑,箭頭的年夜小神馬的都是百分比完成的,所以年夜膽的設置你愛好的個數,只需你沒有密集恐怖症~
b、GestureLockView有三個狀況,沒有手指觸碰、手指觸碰、和手指抬起,會依據這三個狀況繪制分歧的後果,和抬起時的小箭頭須要扭轉的角度,會依據用戶選擇的GestureLockView,停止盤算,在GestureLockViewGroup為每一個GestureLockView設置
c、GestureLockViewGroup重要就是斷定用戶ACTION_MOVE,ACTION_DOWN , ACTION_UP時轉變選中的GestureLockView的狀況,而且記載上去,供給必定的回調。
上面開端看代碼:
2、聲明一些用戶可以設置的屬性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="color_no_finger_inner_circle" format="color" />
<attr name="color_no_finger_outer_circle" format="color" />
<attr name="color_finger_on" format="color" />
<attr name="color_finger_up" format="color" />
<attr name="count" format="integer" />
<attr name="tryTimes" format="integer" />
<declare-styleable name="GestureLockViewGroup">
<attr name="color_no_finger_inner_circle" />
<attr name="color_no_finger_outer_circle" />
<attr name="color_finger_on" />
<attr name="color_finger_up" />
<attr name="count" />
<attr name="tryTimes" />
</declare-styleable>
</resources>
用戶可以用過在xml文件中設置這些屬性,轉變外不雅,最多測驗考試次數和數目等。
3、GestureLockView
package com.zhy.zhy_gesturelockview.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.view.View;
public class GestureLockView extends View
{
private static final String TAG = "GestureLockView";
/**
* GestureLockView的三種狀況
*/
enum Mode
{
STATUS_NO_FINGER, STATUS_FINGER_ON, STATUS_FINGER_UP;
}
/**
* GestureLockView確當前狀況
*/
private Mode mCurrentStatus = Mode.STATUS_NO_FINGER;
/**
* 寬度
*/
private int mWidth;
/**
* 高度
*/
private int mHeight;
/**
* 外圓半徑
*/
private int mRadius;
/**
* 畫筆的寬度
*/
private int mStrokeWidth = 2;
/**
* 圓心坐標
*/
private int mCenterX;
private int mCenterY;
private Paint mPaint;
/**
* 箭頭(小三角最長邊的一半長度 = mArrawRate * mWidth / 2 )
*/
private float mArrowRate = 0.333f;
private int mArrowDegree = -1;
private Path mArrowPath;
/**
* 內圓的半徑 = mInnerCircleRadiusRate * mRadus
*
*/
private float mInnerCircleRadiusRate = 0.3F;
/**
* 四個色彩,可由用戶自界說,初始化時由GestureLockViewGroup傳入
*/
private int mColorNoFingerInner;
private int mColorNoFingerOutter;
private int mColorFingerOn;
private int mColorFingerUp;
public GestureLockView(Context context , int colorNoFingerInner , int colorNoFingerOutter , int colorFingerOn , int colorFingerUp )
{
super(context);
this.mColorNoFingerInner = colorNoFingerInner;
this.mColorNoFingerOutter = colorNoFingerOutter;
this.mColorFingerOn = colorFingerOn;
this.mColorFingerUp = colorFingerUp;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mArrowPath = new Path();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
// 取長和寬中的小值
mWidth = mWidth < mHeight ? mWidth : mHeight;
mRadius = mCenterX = mCenterY = mWidth / 2;
mRadius -= mStrokeWidth / 2;
// 繪制三角形,初始時是個默許箭頭朝上的一個等腰三角形,用戶繪制停止後,依據由兩個GestureLockView決議須要扭轉若干度
float mArrowLength = mWidth / 2 * mArrowRate;
mArrowPath.moveTo(mWidth / 2, mStrokeWidth + 2);
mArrowPath.lineTo(mWidth / 2 - mArrowLength, mStrokeWidth + 2
+ mArrowLength);
mArrowPath.lineTo(mWidth / 2 + mArrowLength, mStrokeWidth + 2
+ mArrowLength);
mArrowPath.close();
mArrowPath.setFillType(Path.FillType.WINDING);
}
@Override
protected void onDraw(Canvas canvas)
{
switch (mCurrentStatus)
{
case STATUS_FINGER_ON:
// 繪制外圓
mPaint.setStyle(Style.STROKE);
mPaint.setColor(mColorFingerOn);
mPaint.setStrokeWidth(2);
canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
// 繪制內圓
mPaint.setStyle(Style.FILL);
canvas.drawCircle(mCenterX, mCenterY, mRadius
* mInnerCircleRadiusRate, mPaint);
break;
case STATUS_FINGER_UP:
// 繪制外圓
mPaint.setColor(mColorFingerUp);
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeWidth(2);
canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
// 繪制內圓
mPaint.setStyle(Style.FILL);
canvas.drawCircle(mCenterX, mCenterY, mRadius
* mInnerCircleRadiusRate, mPaint);
drawArrow(canvas);
break;
case STATUS_NO_FINGER:
// 繪制外圓
mPaint.setStyle(Style.FILL);
mPaint.setColor(mColorNoFingerOutter);
canvas.drawCircle(mCenterX, mCenterY, mRadius, mPaint);
// 繪制內圓
mPaint.setColor(mColorNoFingerInner);
canvas.drawCircle(mCenterX, mCenterY, mRadius
* mInnerCircleRadiusRate, mPaint);
break;
}
}
/**
* 繪制箭頭
* @param canvas
*/
private void drawArrow(Canvas canvas)
{
if (mArrowDegree != -1)
{
mPaint.setStyle(Paint.Style.FILL);
canvas.save();
canvas.rotate(mArrowDegree, mCenterX, mCenterY);
canvas.drawPath(mArrowPath, mPaint);
canvas.restore();
}
}
/**
* 設置以後形式偏重繪界面
*
* @param mode
*/
public void setMode(Mode mode)
{
this.mCurrentStatus = mode;
invalidate();
}
public void setArrowDegree(int degree)
{
this.mArrowDegree = degree;
}
public int getArrowDegree()
{
return this.mArrowDegree;
}
}
正文很具體,重要就是onDraw時,斷定以後狀況,繪制分歧的顯示後果;狀況的轉變都是GestureLockViewGroup的onTouchEvent中設置的。
4、GestureLockViewGroup
package com.zhy.zhy_gesturelockview.view;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import com.zhy.zhy_gesturelockview.R;
import com.zhy.zhy_gesturelockview.view.GestureLockView.Mode;
/**
* 全體包括n*n個GestureLockView,每一個GestureLockView間距離mMarginBetweenLockView,
* 最外層的GestureLockView與容器存在mMarginBetweenLockView的外邊距
*
* 關於GestureLockView的邊長(n*n): n * mGestureLockViewWidth + ( n + 1 ) *
* mMarginBetweenLockView = mWidth ; 得:mGestureLockViewWidth = 4 * mWidth / ( 5
* * mCount + 1 ) 注:mMarginBetweenLockView = mGestureLockViewWidth * 0.25 ;
*
* @author zhy
*
*/
public class GestureLockViewGroup extends RelativeLayout
{
private static final String TAG = "GestureLockViewGroup";
/**
* 保留一切的GestureLockView
*/
private GestureLockView[] mGestureLockViews;
/**
* 每一個邊上的GestureLockView的個數
*/
private int mCount = 4;
/**
* 存儲謎底
*/
private int[] mAnswer = { 0, 1, 2, 5, 8 };
/**
* 保留用戶選中的GestureLockView的id
*/
private List<Integer> mChoose = new ArrayList<Integer>();
private Paint mPaint;
/**
* 每一個GestureLockView中央的間距 設置為:mGestureLockViewWidth * 25%
*/
private int mMarginBetweenLockView = 30;
/**
* GestureLockView的邊長 4 * mWidth / ( 5 * mCount + 1 )
*/
private int mGestureLockViewWidth;
/**
* GestureLockView無手指觸摸的狀況下內圓的色彩
*/
private int mNoFingerInnerCircleColor = 0xFF939090;
/**
* GestureLockView無手指觸摸的狀況下外圓的色彩
*/
private int mNoFingerOuterCircleColor = 0xFFE0DBDB;
/**
* GestureLockView手指觸摸的狀況下內圓和外圓的色彩
*/
private int mFingerOnColor = 0xFF378FC9;
/**
* GestureLockView手指抬起的狀況下內圓和外圓的色彩
*/
private int mFingerUpColor = 0xFFFF0000;
/**
* 寬度
*/
private int mWidth;
/**
* 高度
*/
private int mHeight;
private Path mPath;
/**
* 指引線的開端地位x
*/
private int mLastPathX;
/**
* 指引線的開端地位y
*/
private int mLastPathY;
/**
* 指引下的停止地位
*/
private Point mTmpTarget = new Point();
/**
* 最年夜測驗考試次數
*/
private int mTryTimes = 4;
/**
* 回調接口
*/
private OnGestureLockViewListener mOnGestureLockViewListener;
public GestureLockViewGroup(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public GestureLockViewGroup(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
/**
* 取得一切自界說的參數的值
*/
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.GestureLockViewGroup, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.GestureLockViewGroup_color_no_finger_inner_circle:
mNoFingerInnerCircleColor = a.getColor(attr,
mNoFingerInnerCircleColor);
break;
case R.styleable.GestureLockViewGroup_color_no_finger_outer_circle:
mNoFingerOuterCircleColor = a.getColor(attr,
mNoFingerOuterCircleColor);
break;
case R.styleable.GestureLockViewGroup_color_finger_on:
mFingerOnColor = a.getColor(attr, mFingerOnColor);
break;
case R.styleable.GestureLockViewGroup_color_finger_up:
mFingerUpColor = a.getColor(attr, mFingerUpColor);
break;
case R.styleable.GestureLockViewGroup_count:
mCount = a.getInt(attr, 3);
break;
case R.styleable.GestureLockViewGroup_tryTimes:
mTryTimes = a.getInt(attr, 5);
default:
break;
}
}
a.recycle();
// 初始化畫筆
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
// mPaint.setStrokeWidth(20);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
// mPaint.setColor(Color.parseColor("#aaffffff"));
mPath = new Path();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
// Log.e(TAG, mWidth + "");
// Log.e(TAG, mHeight + "");
mHeight = mWidth = mWidth < mHeight ? mWidth : mHeight;
// setMeasuredDimension(mWidth, mHeight);
// 初始化mGestureLockViews
if (mGestureLockViews == null)
{
mGestureLockViews = new GestureLockView[mCount * mCount];
// 盤算每一個GestureLockView的寬度
mGestureLockViewWidth = (int) (4 * mWidth * 1.0f / (5 * mCount + 1));
//盤算每一個GestureLockView的間距
mMarginBetweenLockView = (int) (mGestureLockViewWidth * 0.25);
// 設置畫筆的寬度為GestureLockView的內圓直徑略微小點(不愛好的話,隨意設)
mPaint.setStrokeWidth(mGestureLockViewWidth * 0.29f);
for (int i = 0; i < mGestureLockViews.length; i++)
{
//初始化每一個GestureLockView
mGestureLockViews[i] = new GestureLockView(getContext(),
mNoFingerInnerCircleColor, mNoFingerOuterCircleColor,
mFingerOnColor, mFingerUpColor);
mGestureLockViews[i].setId(i + 1);
//設置參數,重要是定位GestureLockView間的地位
RelativeLayout.LayoutParams lockerParams = new RelativeLayout.LayoutParams(
mGestureLockViewWidth, mGestureLockViewWidth);
// 不是每行的第一個,則設置地位為前一個的左邊
if (i % mCount != 0)
{
lockerParams.addRule(RelativeLayout.RIGHT_OF,
mGestureLockViews[i - 1].getId());
}
// 從第二行開端,設置為上一行統一地位View的上面
if (i > mCount - 1)
{
lockerParams.addRule(RelativeLayout.BELOW,
mGestureLockViews[i - mCount].getId());
}
//設置右下左上的邊距
int rightMargin = mMarginBetweenLockView;
int bottomMargin = mMarginBetweenLockView;
int leftMagin = 0;
int topMargin = 0;
/**
* 每一個View都有右外邊距和底外邊距 第一行的有上外邊距 第一列的有左外邊距
*/
if (i >= 0 && i < mCount)// 第一行
{
topMargin = mMarginBetweenLockView;
}
if (i % mCount == 0)// 第一列
{
leftMagin = mMarginBetweenLockView;
}
lockerParams.setMargins(leftMagin, topMargin, rightMargin,
bottomMargin);
mGestureLockViews[i].setMode(Mode.STATUS_NO_FINGER);
addView(mGestureLockViews[i], lockerParams);
}
Log.e(TAG, "mWidth = " + mWidth + " , mGestureViewWidth = "
+ mGestureLockViewWidth + " , mMarginBetweenLockView = "
+ mMarginBetweenLockView);
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
// 重置
reset();
break;
case MotionEvent.ACTION_MOVE:
mPaint.setColor(mFingerOnColor);
mPaint.setAlpha(50);
GestureLockView child = getChildIdByPos(x, y);
if (child != null)
{
int cId = child.getId();
if (!mChoose.contains(cId))
{
mChoose.add(cId);
child.setMode(Mode.STATUS_FINGER_ON);
if (mOnGestureLockViewListener != null)
mOnGestureLockViewListener.onBlockSelected(cId);
// 設置指引線的終點
mLastPathX = child.getLeft() / 2 + child.getRight() / 2;
mLastPathY = child.getTop() / 2 + child.getBottom() / 2;
if (mChoose.size() == 1)// 以後添加為第一個
{
mPath.moveTo(mLastPathX, mLastPathY);
} else
// 非第一個,將二者應用線連上
{
mPath.lineTo(mLastPathX, mLastPathY);
}
}
}
// 指引線的起點
mTmpTarget.x = x;
mTmpTarget.y = y;
break;
case MotionEvent.ACTION_UP:
mPaint.setColor(mFingerUpColor);
mPaint.setAlpha(50);
this.mTryTimes--;
// 回調能否勝利
if (mOnGestureLockViewListener != null && mChoose.size() > 0)
{
mOnGestureLockViewListener.onGestureEvent(checkAnswer());
if (this.mTryTimes == 0)
{
mOnGestureLockViewListener.onUnmatchedExceedBoundary();
}
}
Log.e(TAG, "mUnMatchExceedBoundary = " + mTryTimes);
Log.e(TAG, "mChoose = " + mChoose);
// 將起點設置地位為終點,即撤消指引線
mTmpTarget.x = mLastPathX;
mTmpTarget.y = mLastPathY;
// 轉變子元素的狀況為UP
changeItemMode();
// 盤算每一個元素中箭頭須要扭轉的角度
for (int i = 0; i + 1 < mChoose.size(); i++)
{
int childId = mChoose.get(i);
int nextChildId = mChoose.get(i + 1);
GestureLockView startChild = (GestureLockView) findViewById(childId);
GestureLockView nextChild = (GestureLockView) findViewById(nextChildId);
int dx = nextChild.getLeft() - startChild.getLeft();
int dy = nextChild.getTop() - startChild.getTop();
// 盤算角度
int angle = (int) Math.toDegrees(Math.atan2(dy, dx)) + 90;
startChild.setArrowDegree(angle);
}
break;
}
invalidate();
return true;
}
private void changeItemMode()
{
for (GestureLockView gestureLockView : mGestureLockViews)
{
if (mChoose.contains(gestureLockView.getId()))
{
gestureLockView.setMode(Mode.STATUS_FINGER_UP);
}
}
}
/**
*
* 做一些需要的重置
*/
private void reset()
{
mChoose.clear();
mPath.reset();
for (GestureLockView gestureLockView : mGestureLockViews)
{
gestureLockView.setMode(Mode.STATUS_NO_FINGER);
gestureLockView.setArrowDegree(-1);
}
}
/**
* 檢討用戶繪制的手勢能否准確
* @return
*/
private boolean checkAnswer()
{
if (mAnswer.length != mChoose.size())
return false;
for (int i = 0; i < mAnswer.length; i++)
{
if (mAnswer[i] != mChoose.get(i))
return false;
}
return true;
}
/**
* 檢討以後右邊能否在child中
* @param child
* @param x
* @param y
* @return
*/
private boolean checkPositionInChild(View child, int x, int y)
{
//設置了內邊距,即x,y必需落入下GestureLockView的外部中央的小區域中,可以經由過程調劑padding使得x,y落入規模不變年夜,或許不設置padding
int padding = (int) (mGestureLockViewWidth * 0.15);
if (x >= child.getLeft() + padding && x <= child.getRight() - padding
&& y >= child.getTop() + padding
&& y <= child.getBottom() - padding)
{
return true;
}
return false;
}
/**
* 經由過程x,y取得落入的GestureLockView
* @param x
* @param y
* @return
*/
private GestureLockView getChildIdByPos(int x, int y)
{
for (GestureLockView gestureLockView : mGestureLockViews)
{
if (checkPositionInChild(gestureLockView, x, y))
{
return gestureLockView;
}
}
return null;
}
/**
* 設置回調接口
*
* @param listener
*/
public void setOnGestureLockViewListener(OnGestureLockViewListener listener)
{
this.mOnGestureLockViewListener = listener;
}
/**
* 對外頒布設置謎底的辦法
*
* @param answer
*/
public void setAnswer(int[] answer)
{
this.mAnswer = answer;
}
/**
* 設置最年夜試驗次數
*
* @param boundary
*/
public void setUnMatchExceedBoundary(int boundary)
{
this.mTryTimes = boundary;
}
@Override
public void dispatchDraw(Canvas canvas)
{
super.dispatchDraw(canvas);
//繪制GestureLockView間的連線
if (mPath != null)
{
canvas.drawPath(mPath, mPaint);
}
//繪制指引線
if (mChoose.size() > 0)
{
if (mLastPathX != 0 && mLastPathY != 0)
canvas.drawLine(mLastPathX, mLastPathY, mTmpTarget.x,
mTmpTarget.y, mPaint);
}
}
public interface OnGestureLockViewListener
{
/**
* 零丁選中元素的Id
*
* @param position
*/
public void onBlockSelected(int cId);
/**
* 能否婚配
*
* @param matched
*/
public void onGestureEvent(boolean matched);
/**
* 跨越測驗考試次數
*/
public void onUnmatchedExceedBoundary();
}
}
正文極端具體,用極端不外分~重要就是onTouchEvent中對用戶選擇的GestureLockView停止斷定,和轉變GestureLockView狀況等。
5、結構文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:zhy="http://schemas.android.com/apk/res/com.zhy.zhy_gesturelockview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.zhy.zhy_gesturelockview.view.GestureLockViewGroup
android:id="@+id/id_gestureLockViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F2F2F7"
android:gravity="center_vertical"
zhy:count="3"
zhy:tryTimes="5" />
<!-- zhy:color_no_finger_inner_circle="#ff085D58"
zhy:color_no_finger_outer_circle="#ff08F0E0"
zhy:color_finger_on="#FF1734BF" -->
</RelativeLayout>
有興致的可以自界說屬性,把正文的代碼添出來就行,固然你也能夠甚麼都不設置,純真設置寬度和高度,我認為默許後果也是不錯的 ~
6、挪用
package com.zhy.zhy_gesturelockview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.zhy.zhy_gesturelockview.view.GestureLockViewGroup;
import com.zhy.zhy_gesturelockview.view.GestureLockViewGroup.OnGestureLockViewListener;
public class MainActivity extends Activity
{
private GestureLockViewGroup mGestureLockViewGroup;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureLockViewGroup = (GestureLockViewGroup) findViewById(R.id.id_gestureLockViewGroup);
mGestureLockViewGroup.setAnswer(new int[] { 1, 2, 3, 4,5 });
mGestureLockViewGroup
.setOnGestureLockViewListener(new OnGestureLockViewListener()
{
@Override
public void onUnmatchedExceedBoundary()
{
Toast.makeText(MainActivity.this, "毛病5次...",
Toast.LENGTH_SHORT).show();
mGestureLockViewGroup.setUnMatchExceedBoundary(5);
}
@Override
public void onGestureEvent(boolean matched)
{
Toast.makeText(MainActivity.this, matched+"",
Toast.LENGTH_SHORT).show();
}
@Override
public void onBlockSelected(int cId)
{
}
});
}
}