|
|
|
@ -1,47 +1,77 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
* 版权声明,表明代码版权归属于The MiCode Open Source Community。
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* 声明该文件遵循Apache License 2.0。
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* 除非遵守该许可证,否则不得使用此文件。
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
* 你可以在以下网址获得许可证的副本:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* 除非适用法律要求或书面同意,软件
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* 按“原样”分发,不提供任何形式的保证或条件。
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* 无论是明示的还是暗示的。
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* 请参阅许可证,了解控制权限和限制的具体语言。
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
// 声明该类属于net.micode.notes.ui包。
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
// 导入Context类,用于获取应用程序环境信息。
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
// 导入Rect类,用于处理矩形区域。
|
|
|
|
|
import android.text.Layout;
|
|
|
|
|
// 导入Layout类,用于文本布局。
|
|
|
|
|
import android.text.Selection;
|
|
|
|
|
// 导入Selection类,用于文本选择。
|
|
|
|
|
import android.text.Spanned;
|
|
|
|
|
// 导入Spanned接口,用于处理富文本。
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
// 导入TextUtils类,用于处理文本工具。
|
|
|
|
|
import android.text.style.URLSpan;
|
|
|
|
|
// 导入URLSpan类,用于处理URL样式。
|
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
// 导入AttributeSet类,用于XML属性集。
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
// 导入Log类,用于日志输出。
|
|
|
|
|
import android.view.ContextMenu;
|
|
|
|
|
// 导入ContextMenu类,用于上下文菜单。
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
// 导入KeyEvent类,用于处理按键事件。
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
// 导入MenuItem类,用于菜单项。
|
|
|
|
|
import android.view.MenuItem.OnMenuItemClickListener;
|
|
|
|
|
// 导入OnMenuItemClickListener接口,用于菜单项点击事件。
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
// 导入MotionEvent类,用于处理触摸事件。
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
// 导入EditText类,用于文本编辑。
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
// 导入R类,用于访问资源文件。
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
// 导入HashMap类,用于存储键值对。
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
// 导入Map接口,用于映射。
|
|
|
|
|
|
|
|
|
|
// NoteEditText是继承自EditText的自定义控件,用于编辑便签内容
|
|
|
|
|
public class NoteEditText extends EditText {
|
|
|
|
|
// 类变量,用于日志标记
|
|
|
|
|
private static final String TAG = "NoteEditText";
|
|
|
|
|
private int mIndex; // 便签索引
|
|
|
|
|
private int mSelectionStartBeforeDelete; // 删除前的光标位置
|
|
|
|
|
// 成员变量,记录便签索引
|
|
|
|
|
private int mIndex;
|
|
|
|
|
// 成员变量,记录删除前的光标位置
|
|
|
|
|
|
|
|
|
|
// 定义不同的链接协议
|
|
|
|
|
private static final String SCHEME_TEL = "tel:" ;
|
|
|
|
@ -51,6 +81,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
// 用于存放不同协议对应的操作资源ID
|
|
|
|
|
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
|
|
|
|
|
static {
|
|
|
|
|
// 初始化协议对应的资源ID映射
|
|
|
|
|
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel);
|
|
|
|
|
sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web);
|
|
|
|
|
sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email);
|
|
|
|
@ -64,12 +95,10 @@ public class NoteEditText extends EditText {
|
|
|
|
|
* 当按下删除键且文本为空时,删除当前编辑文本
|
|
|
|
|
*/
|
|
|
|
|
void onEditTextDelete(int index, String text);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当按下回车键时,在当前编辑文本后添加新的编辑文本
|
|
|
|
|
*/
|
|
|
|
|
void onEditTextEnter(int index, String text);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当文本变化时,隐藏或显示项目选项
|
|
|
|
|
*/
|
|
|
|
@ -94,18 +123,18 @@ public class NoteEditText extends EditText {
|
|
|
|
|
mOnTextViewChangeListener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NoteEditText构造函数
|
|
|
|
|
// NoteEditText构造函数,通过AttributeSet传递属性
|
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs) {
|
|
|
|
|
super(context, attrs, android.R.attr.editTextStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NoteEditText构造函数
|
|
|
|
|
// NoteEditText构造函数,通过AttributeSet和defStyle传递属性和样式
|
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理触摸事件
|
|
|
|
|
// 处理触摸事件,用于设置光标位置
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
|
switch (event.getAction()) {
|
|
|
|
@ -128,7 +157,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理按键事件
|
|
|
|
|
// 处理按键事件,用于处理回车和删除键
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
|
switch (keyCode) {
|
|
|
|
@ -148,7 +177,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理按键释放事件
|
|
|
|
|
// 处理按键释放事件,用于处理删除和回车键
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
|
|
|
|
switch(keyCode) {
|
|
|
|
@ -180,7 +209,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
return super.onKeyUp(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理焦点变化事件
|
|
|
|
|
// 处理焦点变化事件,用于更新项目选项的显示
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
|
|
|
|
|
if (mOnTextViewChangeListener != null) {
|
|
|
|
@ -194,7 +223,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
super.onFocusChanged(focused, direction, previouslyFocusedRect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建上下文菜单
|
|
|
|
|
// 创建上下文菜单,用于处理文本的上下文操作
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreateContextMenu(ContextMenu menu) {
|
|
|
|
|
if (getText() instanceof Spanned) {
|
|
|
|
@ -218,20 +247,20 @@ public class NoteEditText extends EditText {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (defaultResId == 0) {
|
|
|
|
|
defaultResId = R.string.note_link_other;
|
|
|
|
|
}
|
|
|
|
|
defaultResId = R.string.note_link_other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加菜单项
|
|
|
|
|
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
|
|
|
|
|
new OnMenuItemClickListener() {
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
|
// 跳转到新的意图
|
|
|
|
|
urls[0].onClick(NoteEditText.this);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// 添加菜单项
|
|
|
|
|
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
|
|
|
|
|
new OnMenuItemClickListener() {
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
|
// 跳转到新的意图
|
|
|
|
|
urls[0].onClick(NoteEditText.this);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
super.onCreateContextMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
super.onCreateContextMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
}
|