|
|
@ -36,18 +36,29 @@ import net.micode.notes.R;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/*在这段代码中,定义了一个名为NoteEditText的类,它继承自EditText。这个类用于在EditText的基础上进行一些扩展。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
其中,mIndex和mSelectionStartBeforeDelete是两个成员变量,用于存储索引和删除前的光标位置。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
接下来定义了几个常量,分别表示不同的Scheme,如电话、网址和电子邮件。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
最后,通过一个Map,将每个Scheme与对应的资源ID关联起来。这样可以方便地根据Scheme获取相应的资源ID,用于后续的处理。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class NoteEditText extends EditText {
|
|
|
|
public class NoteEditText extends EditText {
|
|
|
|
private static final String TAG = "NoteEditText";
|
|
|
|
private static final String TAG = "NoteEditText";
|
|
|
|
private int mIndex;
|
|
|
|
private int mIndex;
|
|
|
|
private int mSelectionStartBeforeDelete;
|
|
|
|
private int mSelectionStartBeforeDelete;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义了几种Scheme的常量值
|
|
|
|
private static final String SCHEME_TEL = "tel:" ;
|
|
|
|
private static final String SCHEME_TEL = "tel:" ;
|
|
|
|
private static final String SCHEME_HTTP = "http:" ;
|
|
|
|
private static final String SCHEME_HTTP = "http:" ;
|
|
|
|
private static final String SCHEME_EMAIL = "mailto:" ;
|
|
|
|
private static final String SCHEME_EMAIL = "mailto:" ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义了一个Map,用于存储Scheme和对应的资源ID的映射关系
|
|
|
|
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
|
|
|
|
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
|
|
|
|
// 将不同Scheme和对应的资源ID放入Map中
|
|
|
|
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel);
|
|
|
|
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel);
|
|
|
|
sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web);
|
|
|
|
sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web);
|
|
|
|
sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email);
|
|
|
|
sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email);
|
|
|
@ -58,47 +69,57 @@ public class NoteEditText extends EditText {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public interface OnTextViewChangeListener {
|
|
|
|
public interface OnTextViewChangeListener {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
|
|
|
|
* 当发生{@link KeyEvent#KEYCODE_DEL}事件且当前文本为空时删除当前编辑的文本
|
|
|
|
* and the text is null
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void onEditTextDelete(int index, String text);
|
|
|
|
void onEditTextDelete(int index, String text);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
|
|
|
|
* 当发生{@link KeyEvent#KEYCODE_ENTER}事件时,在当前编辑的文本之后添加一个编辑文本
|
|
|
|
* happen
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void onEditTextEnter(int index, String text);
|
|
|
|
void onEditTextEnter(int index, String text);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Hide or show item option when text change
|
|
|
|
* 当文本改变时,隐藏或显示项目选项
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void onTextChange(int index, boolean hasText);
|
|
|
|
void onTextChange(int index, boolean hasText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*这是一个自定义的NoteEditText类,继承自EditText,并且实现了一些特定的功能和事件处理。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
以下是对代码的逐行注释:*/
|
|
|
|
private OnTextViewChangeListener mOnTextViewChangeListener;
|
|
|
|
private OnTextViewChangeListener mOnTextViewChangeListener;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是NoteEditText类的构造函数之一,它接受一个Context参数,并调用其父类构造函数EditText(Context context, AttributeSet attrs)。另外,它初始化了一个名为mIndex的成员变量,并设置为0。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public NoteEditText(Context context) {
|
|
|
|
public NoteEditText(Context context) {
|
|
|
|
super(context, null);
|
|
|
|
super(context, null);
|
|
|
|
mIndex = 0;
|
|
|
|
mIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是一个用于设置mIndex成员变量的方法,用于设置编辑文本的索引
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void setIndex(int index) {
|
|
|
|
public void setIndex(int index) {
|
|
|
|
mIndex = index;
|
|
|
|
mIndex = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是一个用于设置OnTextViewChangeListener监听器的方法,用于监听文本变化事件。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
|
|
|
|
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
|
|
|
|
mOnTextViewChangeListener = listener;
|
|
|
|
mOnTextViewChangeListener = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是NoteEditText类的构造函数之一,它接受一个Context和AttributeSet参数,并调用其父类构造函数EditText(Context context, AttributeSet attrs, int defStyle)。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs) {
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs, android.R.attr.editTextStyle);
|
|
|
|
super(context, attrs, android.R.attr.editTextStyle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是onTouchEvent方法的重写,用于处理触摸事件,并可以控制光标的位置。
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
switch (event.getAction()) {
|
|
|
|
switch (event.getAction()) {
|
|
|
@ -120,7 +141,9 @@ public class NoteEditText extends EditText {
|
|
|
|
|
|
|
|
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是onKeyDown方法的重写,用于处理按键事件,例如删除键和回车键。
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
switch (keyCode) {
|
|
|
|
switch (keyCode) {
|
|
|
@ -137,7 +160,9 @@ public class NoteEditText extends EditText {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是onKeyUp方法的重写,用于处理按键释放事件,例如删除键和回车键的释放操作。
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
|
|
|
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
|
|
|
switch(keyCode) {
|
|
|
|
switch(keyCode) {
|
|
|
@ -166,7 +191,9 @@ public class NoteEditText extends EditText {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onKeyUp(keyCode, event);
|
|
|
|
return super.onKeyUp(keyCode, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是onFocusChanged方法的重写,用于处理焦点改变事件,并根据当前文本内容的有无来显示或隐藏项目选项。
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
|
|
|
|
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
|
|
|
|
if (mOnTextViewChangeListener != null) {
|
|
|
|
if (mOnTextViewChangeListener != null) {
|
|
|
@ -178,7 +205,11 @@ public class NoteEditText extends EditText {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.onFocusChanged(focused, direction, previouslyFocusedRect);
|
|
|
|
super.onFocusChanged(focused, direction, previouslyFocusedRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
这是onCreateContextMenu方法的重写,用于创建上下文菜单选项,例如处理URL链接的点击事件。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
通过上述重写方法,在相应的事件发生时,可以根据需要执行自定义的逻辑,并通过OnTextViewChangeListener监听器回调相关事件。
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void onCreateContextMenu(ContextMenu menu) {
|
|
|
|
protected void onCreateContextMenu(ContextMenu menu) {
|
|
|
|
if (getText() instanceof Spanned) {
|
|
|
|
if (getText() instanceof Spanned) {
|
|
|
|