0

我正在制作一个应用程序,在该应用程序中,我使用Spannble类在 中创建荧光笔机制EditText,效果很好。现在,我想将跨度位置存储在 SQLiteDatabase 中,以便我可以重新加载它们并让跨度文本在TextView.

这是我TextView的荧光笔机制 -

package com.Swap.RR;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Typeface;

import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher;

import android.text.style.BackgroundColorSpan;

import android.view.Menu;
import android.view.View;

import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.EditText;
import android.widget.ToggleButton;

public class Add_New_Note extends Activity {

private EditText note;
private int mStart;
private Typeface Roboto_lt;
private int end;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add__new__note);

    note = (EditText) findViewById(R.id.notecontent);
    ToggleButton highlighter = (ToggleButton) findViewById(R.id.highlightToggle);
    Roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
    note.setTypeface(Roboto_lt);

    mStart = -1;    
    final TextWatcher highlightWatcher = new TextWatcher() {


       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
       }

       public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        if(mStart > 0)
        {
            end = note.getText().length();
            note.getText().setSpan(new BackgroundColorSpan(getResources().getColor(R.color.highlighter_green)), mStart, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
       public void afterTextChanged(Editable s) {
       }
    };

    highlighter.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton button, boolean isChecked) {


            if (isChecked == true) {  
                //start highlighting when the ToggleButton is ON
                if(mStart == -1) mStart = note.getText().length();
                else mStart = -1;
                mStart = note.getText().length();

                note.addTextChangedListener(highlightWatcher);
            } else {
                //stop highlighting when the ToggleButton is OFF
                note.removeTextChangedListener(highlightWatcher);
            }
        }
    });

} 


public void noteDone(View v) {

    String noteContent = note.getText().toString();

    Intent i = new Intent("com.Swap.Notes_page");

    i.putExtra("NOTE", noteContent);

    setResult(RESULT_OK, i);
    finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_add__new__note, menu);
    return true;
}
}

请帮助我就如何做到这一点提出一些建议。谢谢!

4

1 回答 1

1

您最好的选择是使用Html.toHtml()将其转换Spannable为 HTML。然后,您可以在稍后查询数据库时使用Html.fromHtml()将 HTML 转换回 a Spannable。但是,您需要进行充分的测试,因为toHtml()并且fromHtml()不能保证支持相同的 HTML 标记。

于 2014-04-14T19:12:30.340 回答