一、原理,特点
1.一个线性布局包裹四个edittext 手动补充绘制中间三个点。并增加校验。
2.getset方法,自动跳动到下一个、软键盘的操作响应,删除退格以及下一个可输入的跳转
二、代码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* ip输入不用输入点
* todo 参数适配
*/
public class IPEditTextView extends LinearLayout implements TextWatcher, View.OnTouchListener, View.OnKeyListener, TextView.OnEditorActionListener {
private EditText[] editTexts = new EditText[4];
private int tag;
private Context mContext;
private Paint paint;
public IPEditTextView(Context context) {
super(context);
setWillNotDraw(false);
this.mContext = context;
init();
}
public IPEditTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
this.mContext = context;
init();
}
public IPEditTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);
this.mContext = context;
init();
}
private void init() {
for (int i = 0; i < 4; i++) {
editTexts[i] = createEdit(mContext, i);
this.addView(editTexts[i]);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (TextUtils.isEmpty(s)) return;
if (s.toString().length() > 3 || Integer.parseInt(s.toString().trim()) > 255) {
Toast.makeText(mContext, "最大值为255", Toast.LENGTH_SHORT).show();
editTexts[tag].setText("");
return;
}
if (s.toString().length() == 3) {
tag++;
if (tag >= editTexts.length) tag = editTexts.length - 1;
editTexts[tag].setFocusable(true);
editTexts[tag].requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
/**
* 创建EditText
*
* @param context
* @param tag
* @return
*/
private EditText createEdit(Context context, int tag) {
EditText editText = new EditText(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, dp2px(mContext, 55), 1);
editText.setLayoutParams(layoutParams);
editText.setGravity(Gravity.CENTER);
editText.setMaxLines(1);
editText.setTag(tag);
editText.setTextColor(Color.WHITE);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setBackground(null);
editText.addTextChangedListener(this);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
editText.setOnTouchListener(this);
editText.setOnKeyListener(this);
editText.setOnEditorActionListener(this);
return editText;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
//draw " . "
for (int i = 0; i < editTexts.length - 1; i++) {
canvas.drawCircle(editTexts[i].getMeasuredWidth() * (i + 1), editTexts[i].getMeasuredHeight() / 2 + 5, 2, paint);
}
//draw 框
// paint.setStyle(Paint.Style.STROKE);
// canvas.drawRect(0,0,getWidth(),getHeight(),paint);
}
/**
* editText 触摸事件
*
* @param v editText
* @param event 事件类型
* @return 是否处理
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
tag = (int) v.getTag();
}
return false;
}
/**
* IP Text
*
* @return 新设置的IP
*/
public String getText() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < editTexts.length; i++) {
if (editTexts[i] == null) {
builder.append(".");
continue;
}
String data = editTexts[i].getText().toString();
builder.append(TextUtils.isEmpty(data) ? "" : Integer.parseInt(data));
if (i == (editTexts.length - 1)) continue;
builder.append(".");
}
if (builder.toString().equals("...")) return "";
return builder.toString();
}
/**
* IP Text 自动补充0
*
* @return 新设置的IP
*/
public String getTextAuto() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < editTexts.length; i++) {
if (editTexts[i] == null) {
builder.append("0").append(".");
continue;
}
String data = editTexts[i].getText().toString();
builder.append(TextUtils.isEmpty(data) ? "0" : Integer.parseInt(data));
if (i == (editTexts.length - 1)) continue;
builder.append(".");
}
return builder.toString();
}
/**
* 设置缓存IP
*
* @param text
*/
public void setText(String text) {
if (TextUtils.isEmpty(text.trim())) {
for (int i = 0; i < editTexts.length; i++) {
editTexts[i].setText("");
}
return;
}
String[] split = null;
if (!text.contains(".")) return;
split = text.split("\\.");
int length = Math.min(split.length, editTexts.length);
for (int i = 0; i < length; i++) {
editTexts[i].setText(split[i] == null ? "0" : split[i].toString());
editTexts[i].setSelection(split[i].length());
}
}
/**
* 软键盘删除键监听
*
* @param v
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_UP) {
if (tag < editTexts.length) {
editTexts[tag].setFocusable(true);
editTexts[tag].requestFocus();
}
if (TextUtils.isEmpty(editTexts[tag].getText().toString()) && tag > 0) tag--;
}
return false;
}
public int dp2px(Context context, int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
context.getResources().getDisplayMetrics());
}
/**
* 软键盘填充下一个自动切换
* @param v
* @param actionId
* @param event
* @return
*/
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
//下一个
if (tag < editTexts.length-1) {
editTexts[tag+1].setFocusable(true);
editTexts[tag+1].requestFocus();
tag++;
return true;
}
}
return false;
}
}
网友评论