2012-01-02 47 views
1

我很新的android,Java和sqlite。对于我的第一个程序,我创建了一个程序,该程序将用户输入并放置在预定义的文本中。在Android中结合多个字符串和文本

例:“文本”字符串1“更多的文本”字符串2“更文本”等

我的文字将是一个颜色和字符串将在另一种颜色显示。

我正在使用sqlite来分离我的数据和代码,这是我撞墙的地方。试图找到关于如何将我的上述文本合并到我的数据库表中的一行/列的帮助。

只使用一个上面的例子,我可以得到这个启动和运行。但是将会有超过50个以上的例子来发布数据库,特别是当我想在发布之后添加更多数据库时。

+0

阅读相关SO线程 - http://stackoverflow.com/questions/4283755/android-question-implementing-a-rich-text-editor – adatapost 2012-01-02 02:45:29

回答

6

很可能您已经阅读了SpannableStringBuilder,它允许您为TextView内容中的文本添加颜色。看看下面的代码:

SpannableStringBuilder ssb = new SpannableStringBuilder(<your text>); 
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0)); 

ssb.setSpan(fcs, 0, ssb.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(ssb); 

上面的代码将在大多数情况下工作,但是你想要的是有一个TextView的不同交错的颜色。然后,你应该做到以下几点:

String text = your_text + text_from_database; 
SpannableStringBuilder ssb = new SpannableStringBuilder(text); 
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0)); 
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255 0)); 

ssb.setSpan(fcs, 0, your_text, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
ssb.setSpan(fcs2, your_text.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(ssb); 

上面的代码将现在的工作,但你会发现,如果添加另一个文本your_another_text,并希望使用以前fcs实例第二次,此前有色your_text现在将失去其格式(颜色)。这次您需要创建另一个ForegroundColorSpan fcs3来格式化SpannableStringBuilder的第三部分。这里的关键是只在setSpan方法中使用字符样式一次。如下所示:

String testTest = "abcdefgh"; 
String testTest2 = "ijklmnop"; 
String testTest3 = "qrstuvwxyz"; 

SpannableStringBuilder ssb = new SpannableStringBuilder(testTest+testTest2+testTest3); 
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0)); 
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0)); 
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0)); 


ssb.setSpan(fcs, 0, testTest.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
ssb.setSpan(fcs2, testTest.length(), (testTest+testTest2).length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
ssb.setSpan(fcs3, (testTest+testTest2).length(), ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

test.setText(ssb); 

如果您知道SpannableStringBuilder中有固定数量的String元素,则此方法很好。如果你希望有一个动态长度和元素数量的TextView,你需要用不同的方法做到这一点。对我有用的是将每个字符串元素转换为SpannableString,使用setSpanappend它到TextView。如果您使用循环来构建TextView,这非常有用。

TextView test = (TextView)findViewById(R.id.test); 

String testTest = "abcdefgh"; 
String testTest2 = "ijklmnop"; 
String testTest3 = "qrstuvwxyz"; 

SpannableString ssb = new SpannableString(testTest); 
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0)); 
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0)); 
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0)); 

ssb.setSpan(fcs, 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
test.setText(ssb); 

SpannableString ssb2 = new SpannableString(testTest2); 
ssb2.setSpan(fcs2, 0, ssb2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
test.append(ssb2); 

SpannableString ssb3 = new SpannableString(testTest3); 
ssb3.setSpan(fcs3, 0, ssb3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
test.append(ssb3); 
+0

今天终于到我的程序的一部分,将使用此。为了更清楚我在做什么。我正在接收一个长文本字符串并在其中插入edittexts,这些edittext将具有不同的颜色。文本字符串存储在数据库中,而edittext对它们起作用,但希望将它们从字符串中拉出来,但只能找到一种方法将它们转换为字符串数组,而不是在string.xml中设置单个字符串。顺便说一句,edittext的数量和它们适合文本字符串的位置是动态的。无法告诉程序在哪里放置edittext – maebe 2012-01-22 23:20:42

+0

很好解释,谢谢! – 2012-05-12 17:33:54

相关问题