繁体   English   中英

使用Android中的文件存储多色文本

[英]Storing multi color text using a file in android

我目前正在研究一个android项目,该项目允许用户使用不同的颜色编写文本并将其存储以供以后使用(即编辑或阅读)。

他们有什么办法可以在Android中用多彩色文本存储文件?

注意:我用Google搜索解决方案,但是找不到有用的东西。

评论者建议使用HTML,这可能是一个不错的选择。 欢迎您尝试使用Html.fromHtml()用简单的HTML格式的文件的内容填充EditText ,也欢迎尝试使用Html.toHtml()EditText的内容生成HTML。 但是,从历史上看,这些方法并不是为实现“往返”而编写的,这意味着EditText的内容可能会从其起点更改为Html.toHtml()之后的内容。生成并保存HTML)和Html.fromHtml() (以使用先前保存的HTML填充EditText )。 如果它们不起作用,则可以检查该span并从中生成HTML标记,或者派生该Html类并尝试根据需要对其进行修改,或者编写自己的代码以获取Spanned对象并将其转换为HTML。

我猜用户必须执行某些操作才能切换颜色? 如果是这样-您可以使用该触发器在切换时存储文本位置/长度并保存文本位置列表-颜色。

问题解决了:

用于将多色文本从EditText存储到txt文件的代码:

protected void onDestroy() {
    super.onDestroy();
    boolean writing_allowed= ExternalStorageWriting.isWritingPossible();
    if(writing_allowed)
    {
        String store= Html.toHtml(et.getEditableText());
        File myExternalFile = new File(getExternalFilesDir(filepath), filename3);
        try {
            FileOutputStream fos = new FileOutputStream(myExternalFile);
            fos.write(store.getBytes());
            fos.close();
        } catch (Exception e) {
            Toast.makeText(Notes.this, "Something went wrong...", Toast.LENGTH_SHORT).show();
        }
    }
}

用于读取该txt文件并将其显示在EditText中的代码:

private void setNotes()
{
    String myData="";
    try {
        File myExternalFile = new File(getExternalFilesDir(filepath), filename3);
        FileInputStream fis = new FileInputStream(myExternalFile);
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br =
                new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null) {
            myData = myData + strLine;
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Spanned htmlText = Html.fromHtml(myData);
    et.setText(htmlText);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM