mae*_*ebe 1 sqlite string android textview android-edittext
我是android,Java和sqlite的新手.对于我的第一个程序,我正在创建一个程序,它将接受用户输入并放置在预定义的文本中.
示例:"text"string1"more text"string2"更多文本"等
我的文字将是一种颜色,字符串将以另一种颜色显示.
我正在使用sqlite来分离我的数据和代码,这就是我碰壁的地方.试图找到有关如何将上述文本合并到数据库表中的一行/列的帮助.
仅使用上面的一个示例我可以启动并运行.但是,如果我想在发布后添加更多内容,那么将有50多个上述示例用于发布数据库是必须的.
很可能你已经阅读过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);
Run Code Online (Sandbox Code Playgroud)
上面的代码在大多数情况下都适用,但是你想要的是在一个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);
Run Code Online (Sandbox Code Playgroud)
上面的代码现在可以使用了,但你会注意到,如果你添加另一个文本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);
Run Code Online (Sandbox Code Playgroud)
如果您知道SpannableStringBuilder中有固定数量的String元素,则此方法很好.如果您希望拥有动态长度和元素数量的TextView,则需要以不同的方法执行此操作.对我有用的是将每个字符串元素转换为SpannableString,使用它setSpan,并将append其转换为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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3026 次 |
| 最近记录: |