|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* 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.minodes.notes.ui这个包
|
|
|
|
|
|
|
|
|
|
import android.content.Context;//上面的几个import引入了这个java类将要用到的功能包,以便提供实现便签文本编辑的必要工具
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.text.Layout;
|
|
|
|
|
import android.text.Selection;//选择
|
|
|
|
|
import android.text.Spanned;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.style.URLSpan;
|
|
|
|
|
import android.util.AttributeSet;//属性,特征
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.ContextMenu;
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
import android.view.MenuItem.OnMenuItemClickListener;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.widget.EditText;//编辑文本
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;//上面的几个import引入了这个java类将要用到的功能包,以便提供实现便签文本编辑的必要工具
|
|
|
|
|
//引入包库
|
|
|
|
|
public class NoteEditText extends EditText {//声明了一个公有类NoteEditText,它继承了EditText
|
|
|
|
|
private static final String TAG = "NoteEditText";//标志为字符串"NoteEditText"
|
|
|
|
|
private int mIndex;//建立字符,整型的HASH表,用于进行电话、网站、邮箱的链接。
|
|
|
|
|
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 static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();//语句块:建立一个字符和整数的hash表,用于链接电话,网站,还有邮箱
|
|
|
|
|
static {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Call by the {@link NoteEditActivity} to delete or add edit text
|
|
|
|
|
*/
|
|
|
|
|
public interface OnTextViewChangeListener {//接口:该接口用于实现对TextView组件中的文字信息进行修改
|
|
|
|
|
/**
|
|
|
|
|
* 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);//当触发删除文本KeyEvent时删除文本
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OnTextViewChangeListener mOnTextViewChangeListener;
|
|
|
|
|
|
|
|
|
|
public NoteEditText(Context context) {
|
|
|
|
|
super(context, null);
|
|
|
|
|
mIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setIndex(int index) {
|
|
|
|
|
mIndex = index;
|
|
|
|
|
}//文字更改时隐藏或显示项目选项
|
|
|
|
|
|
|
|
|
|
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
|
|
|
|
|
mOnTextViewChangeListener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs) {
|
|
|
|
|
super(context, attrs, android.R.attr.editTextStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NoteEditText(Context context, AttributeSet attrs, int defStyle) {//自动初始化
|
|
|
|
|
super(context, attrs, defStyle);//根据defstyle自动初始化
|
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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();//减去左边控件的距离
|
|
|
|
|
y -= getTotalPaddingTop();//减去上方控件的距离
|
|
|
|
|
x += getScrollX();//加上滚轮滚过的距离
|
|
|
|
|
y += getScrollY();//加上滚轮滚过的距离
|
|
|
|
|
|
|
|
|
|
Layout layout = getLayout();//用布局控件layout根据x,y的新值设置新的位置
|
|
|
|
|
int line = layout.getLineForVertical(y);//语句:获取纵向的行数
|
|
|
|
|
int off = layout.getOffsetForHorizontal(line, x);//语句:获取横向的偏移量
|
|
|
|
|
Selection.setSelection(getText(), off);//更新光标位置
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.onTouchEvent(event);//语句:这是调用父类的方法,当屏幕有Touch事件时,此方法就会被调用。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {//这个函数规定了当用户按下按键瞬间系统的响应
|
|
|
|
|
switch (keyCode) {//根据按键的KeyCode来处理
|
|
|
|
|
case KeyEvent.KEYCODE_ENTER://按下回车时,如果mOnTextViewChangeListener存在则返回false
|
|
|
|
|
if (mOnTextViewChangeListener != null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case KeyEvent.KEYCODE_DEL://按下删除时设置了光标位置
|
|
|
|
|
mSelectionStartBeforeDelete = getSelectionStart();//“删除”按键
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyUp(int keyCode, KeyEvent event) {//这个函数规定了当用户松开按键瞬间系统的响应
|
|
|
|
|
switch(keyCode) {//据按键的 Unicode 编码值来处理,有删除和进入2种操作
|
|
|
|
|
case KeyEvent.KEYCODE_DEL://若触发修改且文档不为空,则调用前面代码的onEditTextDelete函数进行文本删除
|
|
|
|
|
if (mOnTextViewChangeListener != null) {//若是被修改过
|
|
|
|
|
if (0 == mSelectionStartBeforeDelete && mIndex != 0) {//之前被修改
|
|
|
|
|
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString());//利用上文OnTextViewChangeListener对KEYCODE_DEL按键情况的删除函数进行删除
|
|
|
|
|
return true;//利用上文OnTextViewChangeListener对KEYCODE_DEL按键情况的删除函数进行删除
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "OnTextViewChangeListener was not seted");//其他情况报错,文档的改动监听器并没有建立
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case KeyEvent.KEYCODE_ENTER://若文档改动监听器已建立,则获取当前位置和文本,并根据获取的信息调用onEditTextEnter函数进行文本增添
|
|
|
|
|
if (mOnTextViewChangeListener != null) {//同上也是分为监听器是否建立2种情况
|
|
|
|
|
int selectionStart = getSelectionStart();//获取位置
|
|
|
|
|
String text = getText().subSequence(selectionStart, length()).toString();//获取文本
|
|
|
|
|
setText(getText().subSequence(0, selectionStart));//获取当前文本
|
|
|
|
|
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text);//根据获取的文本设置当前文本
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "OnTextViewChangeListener was not seted");//其他情况报错,文档的改动监听器并没有建立
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return super.onKeyUp(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {//函数功能:当焦点发生变化时,会自动调用该方法来处理焦点改变的事件
|
|
|
|
|
if (mOnTextViewChangeListener != null) {//监听器已建立
|
|
|
|
|
if (!focused && TextUtils.isEmpty(getText())) {//获取焦点文本不为空
|
|
|
|
|
mOnTextViewChangeListener.onTextChange(mIndex, false);//隐藏事件选项
|
|
|
|
|
} else {
|
|
|
|
|
mOnTextViewChangeListener.onTextChange(mIndex, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
super.onFocusChanged(focused, direction, previouslyFocusedRect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreateContextMenu(ContextMenu menu) {//这个重载函数定义了新建文本菜单的过程
|
|
|
|
|
if (getText() instanceof Spanned) {//java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
|
|
|
|
|
int selStart = getSelectionStart();//获取文本开始结尾位置
|
|
|
|
|
int selEnd = getSelectionEnd();//获取文本开始和结尾位置
|
|
|
|
|
|
|
|
|
|
int min = Math.min(selStart, selEnd);//获取开始到结尾的最大、最小值
|
|
|
|
|
int max = Math.max(selStart, selEnd);//开始到结尾的最大值
|
|
|
|
|
|
|
|
|
|
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);//设置url
|
|
|
|
|
if (urls.length == 1) {//设置url的信息的范围值
|
|
|
|
|
int defaultResId = 0;//默认的资源ID值为0
|
|
|
|
|
for(String schema: sSchemaActionResMap.keySet()) {//获取计划表中所有的key值
|
|
|
|
|
if(urls[0].getURL().indexOf(schema) >= 0) {//若url可以添加则在添加后将defaultResId置为key所映射的值
|
|
|
|
|
defaultResId = sSchemaActionResMap.get(schema);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (defaultResId == 0) {
|
|
|
|
|
defaultResId = R.string.note_link_other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(//建立菜单
|
|
|
|
|
new OnMenuItemClickListener() {//新建按键监听器
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {//如果点击菜单执行操作
|
|
|
|
|
// goto a new intent//根据相应的文本设置菜单的按键
|
|
|
|
|
urls[0].onClick(NoteEditText.this);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
super.onCreateContextMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
}
|