本文主要是介绍Android富文本编辑器总结(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android富文本编辑器总结(一)
上一篇就是编号的转换,数字转字母,数字转罗马数字等,比较实用,其中也借鉴了网上的大神的代码,感谢大神们的分享。
下面就需要使用一个简单工厂稍微加工一下,编号的框架就可以搭建起来了。
编号的框架使用的是抽象工厂,即生产工厂的工厂,逻辑也很简单,这个很好地解决了产品簇的问题。比如我想使用有序编号,只需要使用生产有序编号的工厂来获得编号,相反的也可以使用生产无需编号的工厂来获取无需编号。
首先是静态数据接口NoteBookConstants.java
public interface NoteBookConstants {// 序号类型public static final int TYPE_NONE = 0; // 无类型public static final int TYPE_NUMBER = 1; // 数字类型public static final int TYPE_ENGLISH = 2; // 英文字母类型public static final int TYPE_ROMAN = 3; // 罗马数字类型public static final int TYPE_CIRCLE_SOLID_POINT = 1; // 实心圆类型public static final int TYPE_CIRCLE_HOLLOW_POINT = 2; // 空心圆类型public static final int TYPE_SQUARE_SOLID = 3; // 正方形类型// 按键事件public static final int ACTION_DELETE = 1; // 删除public static final int ACTION_ENTER = 2; // 回车public static final int ACTION_LEFT = 3; // 左public static final int ACTION_RIGHT = 4; // 右public static final int ACTION_ORDER_NUM = 5; // 数字public static final int ACTION_ORDER_ROMAN = 6; // 罗马数字public static final int ACTION_ORDER_ENGLISH = 7; // 英文字母public static final int ACTION_TYPE_ORDER = 8; // 有序public static final int ACTION_TYPE_DISORDER = 9; // 无序public static final int ACTION_TOUCH = 10; // 触摸public static final int ACTION_TEXT_CHANGE = 11; // 文本改变public static final int ACTION_LIST_ITEM_CLEAR = 12; // 清除// 序号类别public static final int POINT_TYPE_ORDER = 1; // 有序public static final int POINT_TYPE_DISORDER = 2; // 无序public static final int POINT_TYPE_NONE = 3; // 无编号
}
抽象工厂接口IOrderIndexFactory.java
public interface IOrderIndexFactory extends NoteBookConstants{public IOrderIndex createOrderIndex(int type);
}
有序序号类型工厂OrderIndexFactory.java
public class OrderIndexFactory implements IOrderIndexFactory{@Overridepublic IOrderIndex createOrderIndex(int type) {IOrderIndex orderIndex = null;switch (type) {case TYPE_NUMBER:orderIndex = new NumberIndex();break;case TYPE_ENGLISH:orderIndex = new EnglishLetterIndex();break;case TYPE_ROMAN:orderIndex = new RomanNumberIndex();break;default:break;}return orderIndex;}}
无序图标工厂DisorderIndexFactory.java
public class DisorderIndexFactory implements IOrderIndexFactory{@Overridepublic IOrderIndex createOrderIndex(int type) {IOrderIndex orderIndex = null;switch (type) {case TYPE_CIRCLE_SOLID_POINT:orderIndex = new CircleSolidPointIndex();break;case TYPE_CIRCLE_HOLLOW_POINT:orderIndex = new CircleHollowPointIndex();break;case TYPE_SQUARE_SOLID:orderIndex = new SquareSolidPointIndex();break;default:break;}return orderIndex;}
}
无编号生产工厂NoneIndexFactory.java
public class NoneIndexFactory implements IOrderIndexFactory{@Overridepublic IOrderIndex createOrderIndex(int type) {// TODO Auto-generated method stubreturn new NoneIndex();}
}
最后一个生产工厂的简单工厂IndexFactory.java
public class IndexFactory implements NoteBookConstants{public static IOrderIndexFactory createIndexFactory(int type) {IOrderIndexFactory factory = null;switch (type) {case POINT_TYPE_ORDER:factory = new OrderIndexFactory();break;case POINT_TYPE_DISORDER:factory = new DisorderIndexFactory();break;case POINT_TYPE_NONE:factory = new NoneIndexFactory();break;default:break;}return factory;}
}
以上就是编号的小框架,都是java代码,可以测试一下
IOrderIndexFactory indexFactory = IndexFactory.createIndexFactory(NoteBookConstants.POINT_TYPE_ORDER);
IOrderIndex orderIndex = indexFactory.createOrderIndex(NoteBookConstants.TYPE_ENGLISH);
for (int i = 0; i < 100; i++) {System.out.println(orderIndex.getIndexString(i) + "");
}
总之,不管编辑器的实现方案是什么,这套逻辑基本能够复用,下一篇准备整理一下实现方案。
这篇关于Android富文本编辑器总结(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!