如题,想开发一款代码编辑器,安卓平台的,如何高亮代码,有没有好用的插件,或者自定义一个EditText的基本思路
参考WebView显示的思路,用JavaScript高亮代码,我在一个项目中涉及了这个功能,你可以参考一下,
https://github.com/ChangJiahong/NoteBlogForAndroid/blob/master/app/src/main/java/top/pythong/noteblog/base/widget/ContentView.kt
setCodeSourceText函数
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) { //隐藏软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否则所有的组件都不会有TouchEvent了
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
/**
* 判断当前点击的控件是否为EditText
* @param v
* @param event
* @return
*/
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) { //点击view==EditText
((TextView) v).setCursorVisible(true);
int[] leftTop = {0, 0};
//获取输入框当前位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
// 点击的是输入框区域,保留点击EditText的事件
return false;
} else {
return true;
}
}
return false;
}
在Android开发中实现代码高亮功能需要自定义EditText,可以使用SpannableStringBuilder和ForegroundColorSpan来实现。具体步骤如下:
1.新建一个类HighLightEditText并继承自EditText; 2.重写构造方法,初始化Paint对象,设置字体,空格宽度等; 3.重写setText方法,在方法内使用SpannableStringBuilder和ForegroundColorSpan来对输入的内容进行高亮处理; 4.可以在onDraw方法中进行光标的处理; 5.在布局文件中使用自定义的HighLightEditText。
以下是代码实现:
HighLightEditText.java:
public class HighLightEditText extends EditText {
private Paint mPaint;
private int mTextColor;
private int mHighLightColor;
public HighLightEditText(Context context) {
super(context);
init();
}
public HighLightEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HighLightEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public HighLightEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
mPaint = getPaint();
mTextColor = getCurrentTextColor();
mHighLightColor = Color.parseColor("#FF4081");
setFontSpacing((float) (getLineHeight() * 0.2)); // 设置行间距
setTextColor(mTextColor);
setHighlightColor(Color.TRANSPARENT);
setLayerType(LAYER_TYPE_HARDWARE, mPaint);
}
@Override
public void setText(CharSequence text, BufferType type) {
if (TextUtils.isEmpty(text)) {
super.setText(text, type);
return;
}
if (text instanceof Spannable) {
super.setText(text, type);
return;
}
SpannableStringBuilder builder = new SpannableStringBuilder(text);
builder.setSpan(new ForegroundColorSpan(mHighLightColor), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
super.setText(builder, BufferType.SPANNABLE);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
if (isFocused()) {
mPaint.setColor(mHighLightColor);
drawableStateChanged();
} else {
mPaint.setColor(mTextColor);
drawableStateChanged();
}
super.onDraw(canvas);
canvas.restore();
}
}
使用方法:
在布局文件中使用HighLightEditText即可:
<com.example.HighLightEditText
android:id="@+id/highlight_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
代码中已经提供了EditText强制隐藏键盘的方法,可以在需要的地方调用:
KeyBoardUtils.hideInputForce(getActivity())
当然,如果需要自己定义高亮的规则,可以在setText方法中对text进行分段处理,然后对每一段单独使用SpannableStringBuilder和ForegroundColorSpan进行高亮处理。