Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 外部数据库中具有HTML字体样式的字符串_Android_Sqlite - Fatal编程技术网

Android 外部数据库中具有HTML字体样式的字符串

Android 外部数据库中具有HTML字体样式的字符串,android,sqlite,Android,Sqlite,嗨,伙计们!我正在开发一个使用外部sqlite数据库的应用程序。我正在使用一个示例源代码,我想从条目中摘录一些粗体或斜体样式或其他颜色 例如,如果我有这样一个条目:黑色是我最喜欢的颜色。我希望我的字符串能够识别HTML标记。在我的例子中,“黑色”一词应该用粗体 我是个新手,我需要详细的帮助。这是我的代码: java public class WordView extends TextView implements OnTouchListener { private PopupView popu

嗨,伙计们!我正在开发一个使用外部sqlite数据库的应用程序。我正在使用一个示例源代码,我想从条目中摘录一些粗体或斜体样式或其他颜色

例如,如果我有这样一个条目:黑色是我最喜欢的颜色。我希望我的字符串能够识别HTML标记。在我的例子中,“黑色”一词应该用粗体

我是个新手,我需要详细的帮助。这是我的代码:

java

public class WordView extends TextView implements OnTouchListener {

private PopupView popup;
private String infoWord;
private ScrollView parent;
private boolean mClickEnabled;
private Typeface transTypeFace, transTypeFaceBold;
private static final Map<String, String[]> FONTS = new HashMap<String, String[]>();
static {
    FONTS.put("sans", new String[] { "DejaVuSansCondensed.ttf", "DejaVuSansCondensed-Bold.ttf"});
    FONTS.put("serif", new String[] { "DejaVuSerifCondensed.ttf", "DejaVuSerifCondensed-Bold.ttf"});
}

private class Type1Span extends ClickableSpan {
    String word;
    public Type1Span(String word) {
        this.word = word;
    }
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(touchedInstance == this);
        ds.setTypeface(transTypeFace);
    }
    @Override
    public void onClick(View widget) {
        if (mClickEnabled) {
            Uri uri = Uri.parse("ecidiomas://" + word);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            widget.getContext().startActivity(intent);
        }
    }
};

private class Type2Span extends Type1Span {
    public Type2Span(String word) {
        super(word);
    }
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setTypeface(transTypeFaceBold);
    }
}

private class TranscriptionTypeSpan extends ForegroundColorSpan {
    public TranscriptionTypeSpan(int color) {
        super(color);
    }
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setTypeface(transTypeFaceBold);
        //ds.setTypeface(Typeface.DEFAULT_BOLD);
    }
}

private Type1Span touchedInstance;

public WordView(Context context) {
    super(context);
    init();
}

public WordView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public WordView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    Context ctx = getContext();
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctx);
    String defaultType = "sans";
    String fontType = sp.getString(App.PreferenceKeys.preference_font_idx, defaultType);
    if (!FONTS.containsKey(fontType)) fontType = defaultType;
    transTypeFace = Typeface.createFromAsset(ctx.getAssets(), FONTS.get(fontType)[0]);
    transTypeFaceBold = Typeface.createFromAsset(ctx.getAssets(), FONTS.get(fontType)[1]);
    setTypeface(transTypeFaceBold);
    setMovementMethod(LinkMovementMethod.getInstance());
    setOnTouchListener(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    ViewParent p = getParent();
    if (p instanceof ScrollView)
        parent = (ScrollView) p;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return event.getAction() == MotionEvent.ACTION_DOWN ?
            true : super.onTouchEvent(event);
}

public void setWordInfo(String word, String translation, String text) {
    infoWord = word;
    translation = translation.trim();
    translation = TextUtils.isEmpty(translation) ? "" : (translation + '\n');
    setText(infoWord.trim().toUpperCase() + "\n" + translation + "\n"+ text);
}

@Override
public void setText(CharSequence text, BufferType type) {
    Integer currentType = LinksFinder.getType(infoWord != null ? infoWord : ""); //XXX
    ArrayList<LinksFinder.LinkSpec> links = LinksFinder.getLinks(text.toString());
    if (links == null) {
        super.setText(text, type);
        return;
    }
    SpannableString ss = new SpannableString(text);
    int links_length = links.size();
    for (int i = 0; i < links_length; ++i) {
        LinksFinder.LinkSpec l = links.get(i);
        CharacterStyle span;
        if (LinksFinder.TRANSCRIPTION == l.type) {
            span = new TranscriptionTypeSpan(0xffc91111);
        } else if (currentType == l.type) {
            span = new Type2Span(l.url);
        } else {
            span = new Type1Span(l.url);
        }
        ss.setSpan(span, l.start, l.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    super.setText(ss, type);
}

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN
            || event.getAction() == MotionEvent.ACTION_MOVE) {

        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= getTotalPaddingLeft();
        y -= getTotalPaddingTop();

        int xx = getRealPosX(x);
        int yy = getRealPosY(y);

        x += getScrollX();
        y += getScrollY();

        Layout layout = getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        Type1Span[] candidates = ((Spannable) getText()).getSpans(off, off, Type1Span.class);
        if (candidates.length > 0) {
            touchedInstance = candidates[0];
        } else {
            touchedInstance = null;
        }
        if (mClickEnabled && popup != null && touchedInstance != null)
            popup.setPopupText(xx, yy, touchedInstance.word);
    } else {
        if (popup != null)
            popup.clear();
        if (touchedInstance != null) {
            touchedInstance = null;
        }
    }
    return false;
}

public void setPopup(PopupView popup) {
    this.popup = popup;
    this.popup.setTypeFace(transTypeFaceBold);
}

private int getRealPosX(int val) {
    return parent == null ? val : val - parent.getScrollX();
}

private int getRealPosY(int val) {
    return parent == null ? val : val - parent.getScrollY();
}

public void setClickEnabled(boolean enabled) {
    mClickEnabled = enabled;
} 
}
公共类WordView扩展了TextView实现的OnTouchListener{
私有弹出视图弹出窗口;
私有字符串信息字;
私有滚动视图父级;
启用私有布尔值;
专用字体转换字体,转换字体粗体;
私有静态最终映射字体=新HashMap();
静止的{
FONTS.put(“sans”,新字符串[]{“DejaVuSansCondensed.ttf”,“DejaVuSansCondensed Bold.ttf”});
FONTS.put(“serif”,新字符串[]{“dejavUserIfCondented.ttf”,“dejavUserIfCondented Bold.ttf”});
}
私有类Type1Span扩展了ClickableSpan{
字符串字;
公共类型1span(字符串字){
这个单词=单词;
}
@凌驾
public void updateDrawState(TextPaint ds){
super.updateDrawState(ds);
ds.setUnderlineText(touchedInstance==this);
设置字体(转换字体);
}
@凌驾
公共void onClick(视图小部件){
如果(mclick已启用){
Uri=Uri.parse(“ecidiomas://”+word);
意图=新意图(Intent.ACTION\u视图,uri);
intent.addCategory(intent.CATEGORY\u默认值);
widget.getContext().startActivity(intent);
}
}
};
私有类Type2Span扩展了Type1Span{
公共类型2span(字符串字){
超级(字);
}
@凌驾
public void updateDrawState(TextPaint ds){
super.updateDrawState(ds);
ds.setTypeface(transTypeFaceBold);
}
}
私有类转录类型span扩展了ForegroundColorSpan{
公共转录类型span(int颜色){
超级(彩色);
}
@凌驾
public void updateDrawState(TextPaint ds){
super.updateDrawState(ds);
ds.setTypeface(transTypeFaceBold);
//ds.setTypeface(Typeface.DEFAULT_BOLD);
}
}
私有类型1跨接状态;
公共WordView(上下文){
超级(上下文);
init();
}
公共WordView(上下文、属性集属性){
超级(上下文,attrs);
init();
}
公共WordView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init();
}
私有void init(){
Context ctx=getContext();
SharedReferences sp=PreferenceManager.GetDefaultSharedReferences(ctx);
字符串defaultType=“sans”;
String fontType=sp.getString(App.PreferenceKeys.preference\u font\u idx,defaultType);
如果(!FONTS.containsKey(fontType))fontType=defaultType;
transTypeFace=Typeface.createFromAsset(ctx.getAssets(),FONTS.get(fontType)[0]);
transTypeFaceBold=Typeface.createFromAsset(ctx.getAssets(),FONTS.get(fontType)[1]);
setTypeface(transTypeFaceBold);
setMovementMethod(LinkMovementMethod.getInstance());
setOnTouchListener(这个);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
ViewParent p=getParent();
如果(滚动视图的p实例)
父项=(滚动视图)p;
}
@凌驾
公共布尔onTouchEvent(运动事件){
return event.getAction()==MotionEvent.ACTION\u是否已关闭?
true:super.onTouchEvent(事件);
}
public void setWordInfo(字符串字、字符串翻译、字符串文本){
infoWord=word;
translation=translation.trim();
translation=TextUtils.isEmpty(translation)?“”:(translation+'\n');
setText(infoWord.trim().toUpperCase()+“\n”+翻译+“\n”+文本);
}
@凌驾
public void setText(CharSequence text,BufferType){
整数currentType=LinksFinder.getType(infoWord!=null?infoWord:);//XXX
ArrayList links=LinksFinder.getLinks(text.toString());
if(links==null){
super.setText(文本,类型);
返回;
}
SpannableString ss=新的SpannableString(文本);
int links_length=links.size();
对于(int i=0;i0){
touchedInstance=候选者[0];
}否则{
touchedInstance=null;
}
如果(mClickEnabled&&popup!=null&&touchedInstance!=null)
setPopupUpText(xx,yy,touchedInstance.word);
}否则{
如果(弹出!=null)
popup.clear();
if(touchedInstance!=null){
touchedInstance=null;
}
}
返回false;
}
公共void setPopup(PopupView弹出窗口){
this.popup=弹出;
这
class WordAdapter extends CursorAdapter implements Filterable, DB {

private String[] QUERY_PROJECTION = new String[] { COLUMN_ID, COLUMN_WORD };
private final int WORD_COLUMN_INDEX;
private SQLiteDatabase db;

public WordAdapter(Context context, Cursor c, SQLiteDatabase db) {
    super(context, c);
    WORD_COLUMN_INDEX = c.getColumnIndexOrThrow(COLUMN_WORD);
    this.db = db;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    final TextView view = (TextView) inflater.inflate(
            R.layout.simple_dropdown_item_1line, parent, false);
    view.setText(cursor.getString(WORD_COLUMN_INDEX));
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ((TextView) view).setText(cursor.getString(WORD_COLUMN_INDEX));
}

@Override
public String convertToString(Cursor cursor) {
    return cursor.getString(WORD_COLUMN_INDEX);
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence s) {
    Cursor c = null;
    if (s != null)
        c = db.query(DB.TABLE_WORDS, QUERY_PROJECTION, getLike(s.toString().toLowerCase()), null, null, null, null);            
    return c;
}

private String getLike(String s) {
    return DB.COLUMN_WORD + ">= '" + s + "' AND " + DB.COLUMN_WORD + "< '" + s + '\u044F' +"'";
}

}