|
|
|
|
@ -17,7 +17,10 @@
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
|
import android.text.Layout;
|
|
|
|
|
import android.text.Selection;
|
|
|
|
|
import android.text.Spanned;
|
|
|
|
|
@ -30,21 +33,35 @@ import android.view.KeyEvent;
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
import android.view.MenuItem.OnMenuItemClickListener;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
|
import android.text.Editable;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
import androidx.appcompat.widget.AppCompatEditText;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class NoteEditText extends EditText {
|
|
|
|
|
public class NoteEditText extends AppCompatEditText {
|
|
|
|
|
private static final String TAG = "NoteEditText";
|
|
|
|
|
private int mIndex;
|
|
|
|
|
private int mSelectionStartBeforeDelete;
|
|
|
|
|
|
|
|
|
|
private static final String SCHEME_TEL = "tel:" ;
|
|
|
|
|
private static final String SCHEME_HTTP = "http:" ;
|
|
|
|
|
private static final String SCHEME_EMAIL = "mailto:" ;
|
|
|
|
|
// 字数显示相关变量
|
|
|
|
|
private Paint mPaint;
|
|
|
|
|
private String mCharacterCountText = "字符数:0";
|
|
|
|
|
private int mTextColor = 0xFF333333; // 深灰色
|
|
|
|
|
private float mTextSize = 16f; // sp,字体放大
|
|
|
|
|
private int mPadding = 12; // dp,边距调整
|
|
|
|
|
private boolean mIsBold = true; // 字体加粗标志
|
|
|
|
|
|
|
|
|
|
private static final String SCHEME_TEL = "tel:";
|
|
|
|
|
private static final String SCHEME_HTTP = "http:";
|
|
|
|
|
private static final String SCHEME_EMAIL = "mailto:";
|
|
|
|
|
|
|
|
|
|
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
|
|
|
|
|
static {
|
|
|
|
|
@ -57,29 +74,21 @@ public class NoteEditText extends EditText {
|
|
|
|
|
* Call by the {@link NoteEditActivity} to delete or add edit text
|
|
|
|
|
*/
|
|
|
|
|
public interface OnTextViewChangeListener {
|
|
|
|
|
/**
|
|
|
|
|
* Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
|
|
|
|
|
* and the text is null
|
|
|
|
|
*/
|
|
|
|
|
void onEditTextDelete(int index, String text);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
|
|
|
|
|
* happen
|
|
|
|
|
*/
|
|
|
|
|
void onEditTextEnter(int index, String text);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hide or show item option when text change
|
|
|
|
|
*/
|
|
|
|
|
void onTextChange(int index, boolean hasText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OnTextViewChangeListener mOnTextViewChangeListener;
|
|
|
|
|
|
|
|
|
|
// 用于计算字数
|
|
|
|
|
private int mCharacterCount = 0;
|
|
|
|
|
|
|
|
|
|
public NoteEditText(Context context) {
|
|
|
|
|
super(context, null);
|
|
|
|
|
mIndex = 0;
|
|
|
|
|
initPaint();
|
|
|
|
|
initTextWatcher();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setIndex(int index) {
|
|
|
|
|
@ -92,18 +101,60 @@ public class NoteEditText extends EditText {
|
|
|
|
|
|
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs) {
|
|
|
|
|
super(context, attrs, android.R.attr.editTextStyle);
|
|
|
|
|
initPaint();
|
|
|
|
|
initTextWatcher();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
|
initPaint();
|
|
|
|
|
initTextWatcher();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化画笔用于绘制字数
|
|
|
|
|
private void initPaint() {
|
|
|
|
|
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
|
mPaint.setTextSize(sp2px(mTextSize)); // 设置更大的字体大小
|
|
|
|
|
mPaint.setColor(mTextColor);
|
|
|
|
|
|
|
|
|
|
// 设置字体加粗
|
|
|
|
|
if (mIsBold) {
|
|
|
|
|
// 方法1:使用Typeface设置粗体
|
|
|
|
|
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
|
|
|
|
|
|
|
// 方法2:如果需要使用原始字体但加粗,可以这样设置
|
|
|
|
|
// mPaint.setTypeface(Typeface.create(mPaint.getTypeface(), Typeface.BOLD));
|
|
|
|
|
|
|
|
|
|
// 方法3:使用Fake Bold,注意这与setTypeface是不同的方法
|
|
|
|
|
// mPaint.setFakeBoldText(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化文本变化监听器
|
|
|
|
|
private void initTextWatcher() {
|
|
|
|
|
this.addTextChangedListener(new TextWatcher() {
|
|
|
|
|
@Override
|
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
|
String processedText = cutOfimage(operateText(s.toString()));
|
|
|
|
|
mCharacterCount = processedText.length();
|
|
|
|
|
mCharacterCountText = "字符数:" + mCharacterCount;
|
|
|
|
|
invalidate(); // 刷新视图以重绘字数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
|
switch (event.getAction()) {
|
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
|
|
|
|
|
|
int x = (int) event.getX();
|
|
|
|
|
int y = (int) event.getY();
|
|
|
|
|
x -= getTotalPaddingLeft();
|
|
|
|
|
@ -117,7 +168,6 @@ public class NoteEditText extends EditText {
|
|
|
|
|
Selection.setSelection(getText(), off);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -148,7 +198,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "OnTextViewChangeListener was not seted");
|
|
|
|
|
Log.d(TAG, "OnTextViewChangeListener was not set");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case KeyEvent.KEYCODE_ENTER:
|
|
|
|
|
@ -158,7 +208,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
setText(getText().subSequence(0, selectionStart));
|
|
|
|
|
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text);
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "OnTextViewChangeListener was not seted");
|
|
|
|
|
Log.d(TAG, "OnTextViewChangeListener was not set");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
@ -214,4 +264,51 @@ public class NoteEditText extends EditText {
|
|
|
|
|
}
|
|
|
|
|
super.onCreateContextMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String operateText(String str) {
|
|
|
|
|
Pattern p = Pattern.compile("\\s+"); // 简化正则表达式
|
|
|
|
|
Matcher m = p.matcher(str);
|
|
|
|
|
return m.replaceAll("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String cutOfimage(String str) {
|
|
|
|
|
String dest = str;
|
|
|
|
|
int index1 = dest.indexOf("[local]");
|
|
|
|
|
int index2 = dest.indexOf("[/local]");
|
|
|
|
|
while (index1 != -1 && index2 != -1) {
|
|
|
|
|
dest = dest.substring(0, index1) + dest.substring(index2 + 8);
|
|
|
|
|
index1 = dest.indexOf("[local]");
|
|
|
|
|
index2 = dest.indexOf("[/local]");
|
|
|
|
|
}
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写onDraw方法,在右下角绘制字数
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDraw(Canvas canvas) {
|
|
|
|
|
super.onDraw(canvas);
|
|
|
|
|
|
|
|
|
|
// 计算绘制位置:右下角,调整边距
|
|
|
|
|
float x = getWidth() - mPaint.measureText(mCharacterCountText) - dp2px(mPadding);
|
|
|
|
|
float y = getHeight() - dp2px(mPadding);
|
|
|
|
|
|
|
|
|
|
canvas.drawText(mCharacterCountText, x, y, mPaint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// dp转px工具方法
|
|
|
|
|
private int dp2px(float dpValue) {
|
|
|
|
|
float scale = getContext().getResources().getDisplayMetrics().density;
|
|
|
|
|
return (int) (dpValue * scale + 0.5f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sp转px工具方法
|
|
|
|
|
private int sp2px(float spValue) {
|
|
|
|
|
float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
|
|
|
|
|
return (int) (spValue * fontScale + 0.5f);
|
|
|
|
|
}
|
|
|
|
|
}
|