- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在 Android 中开发富文本编辑器。基本上它有粗体、斜体和链接按钮,这些按钮与 EditText 相关联以更改内容的样式。如果您先选择要设置样式的文本,然后使用此方法选择按钮,我的效果会很好:http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext .
我想做的是让它像富文本编辑器一样工作,您可以在其中使用按钮作为切换按钮来设置文本样式,只要您愿意,然后再次单击切换按钮停止使用样式。因此,如果我想输入粗体的“注意这个!”,我会点击“B”按钮,然后开始输入文本,我输入的所有内容都会变成粗体,直到我点击“B” ' 再次按钮。
关于如何实现这一点有什么想法吗?我希望我已经足够清楚了:)
最佳答案
对于那些感兴趣的人,我通过在按下 ToggleButton 时保存光标位置(下面的“styleStart”变量)来实现这一点,然后当用户键入更多字符时,我实际上使用 removeSpan() 删除了匹配的 StyleSpan,然后使用保存的原始光标位置 + 当前键入的字符长度,使用 setSpan() 重新添加它。
您还需要跟踪用户是否更改了光标位置,这样您就不会设置不需要的文本样式。这是 TextWatcher 代码:
final EditText contentEdit = (EditText) findViewById(R.id.content);
contentEdit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//add style as the user types if a toggle button is enabled
ToggleButton boldButton = (ToggleButton) findViewById(R.id.bold);
ToggleButton emButton = (ToggleButton) findViewById(R.id.em);
ToggleButton bquoteButton = (ToggleButton) findViewById(R.id.bquote);
ToggleButton underlineButton = (ToggleButton) findViewById(R.id.underline);
ToggleButton strikeButton = (ToggleButton) findViewById(R.id.strike);
int position = Selection.getSelectionStart(contentEdit.getText());
if (position < 0){
position = 0;
}
if (position > 0){
if (styleStart > position || position > (cursorLoc + 1)){
//user changed cursor location, reset
styleStart = position - 1;
}
cursorLoc = position;
if (boldButton.isChecked()){
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
s.removeSpan(ss[i]);
}
}
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (emButton.isChecked()){
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);
boolean exists = false;
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.ITALIC){
s.removeSpan(ss[i]);
}
}
s.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (bquoteButton.isChecked()){
QuoteSpan[] ss = s.getSpans(styleStart, position, QuoteSpan.class);
for (int i = 0; i < ss.length; i++) {
s.removeSpan(ss[i]);
}
s.setSpan(new QuoteSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (underlineButton.isChecked()){
UnderlineSpan[] ss = s.getSpans(styleStart, position, UnderlineSpan.class);
for (int i = 0; i < ss.length; i++) {
s.removeSpan(ss[i]);
}
s.setSpan(new UnderlineSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (strikeButton.isChecked()){
StrikethroughSpan[] ss = s.getSpans(styleStart, position, StrikethroughSpan.class);
for (int i = 0; i < ss.length; i++) {
s.removeSpan(ss[i]);
}
s.setSpan(new StrikethroughSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//unused
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//unused
}
});
这是 ToggleButton 的点击操作之一:
final ToggleButton boldButton = (ToggleButton) findViewById(R.id.bold);
boldButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
EditText contentText = (EditText) findViewById(R.id.content);
int selectionStart = contentText.getSelectionStart();
styleStart = selectionStart;
int selectionEnd = contentText.getSelectionEnd();
if (selectionStart > selectionEnd){
int temp = selectionEnd;
selectionEnd = selectionStart;
selectionStart = temp;
}
if (selectionEnd > selectionStart)
{
Spannable str = contentText.getText();
StyleSpan[] ss = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);
boolean exists = false;
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
str.removeSpan(ss[i]);
exists = true;
}
}
if (!exists){
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
boldButton.setChecked(false);
}
}
});
可能有更好的解决方案,如果你有的话,很高兴听到!
关于android - 样式 EditText 内容 'on the fly' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3096332/
我在 asp.net 应用程序(使用 IKVM)中使用 flysaucer 和 iText 将 HTML 转换为 PDF。如果我将样式直接放在 html 中,它工作正常(即使样式放在样式标签之间),但
我一直在尝试将带有内容的伪元素添加到正在使用飞碟库转换的页面的 CSS 中。在常规浏览器中以 HTML 格式查看页面时,代码运行良好,我可以看到伪元素 (:before)。然而,当使用 Flying
我使用 Flying Saucer 和 itext 将 html 转换为 pdf,为了在垂直方向写入文本,我尝试了简单的 css: style="writing-mode: tb-rl"; 浏览器的变
我有一个组合框,用户可以在其中选择可用的语言。该应用程序包含每种语言的属性文件。在页面的资源部分,资源包是根据用户配置文档中的语言标记(DE、EN ...)计算的。 有什么简单的方法可以根据组合框的值
我已经在页面上嵌入了Youtube播放器,并使用JavaScript生成了“即时”播放列表。观看期间是否可以在播放列表的末尾添加新视频?我可以从列表的开头删除视频吗? 最佳答案 您可以,但是在尝试在视
...特别是在 Groovy 中(因此是标签)? 在 Java 中你不能这样做......但在动态语言(例如 Python)中你通常可以。 在 Eclipse 中尝试做类似在 Spock 特性(即测试
在编写 shell 脚本(据我所知通常没有 .* 后缀)时,我通常会执行以下操作 1) emacs foo2) 输入 #!/bin/sh3) 关闭并重新打开 emacs 以启用 shell-scrip
嗨,我有一个对象:var myobject = new Object;我想在以这种方式循环 jquery 输入集合时动态填充它的属性: $('.test').each(function(){
我认为可以在 C 中即时连接字符串(字符数组)。 char* str1= "hello" " " "world"; 但是当我尝试以下操作时,我会收到一条错误消息(函数 fopen 的参数太少)。为什么
我有下表: store | dow | turnover ------+-----+----------- 1 | 1 | Eu59.426,00 1 | 2 | Eu33.
我需要使用标准 UNIX diff 命令和 python subprocess 模块创建一个差异文件。问题是我必须在不创建临时文件的情况下比较文件和流。我想过通过 os.mkfifo 方法使用命名管道
Fly-Pie 是一个用于 GNOME 的独特的菜单启动器,以操作鼠标为主的用户可以用它进行一些操作。 应用启动器可以方便地快速切换到一个活动窗口,启动新的应用,等等。 Ulau
strftime(),根据 cpan.org: print strftime($template, @lt); 我只是想不出这个正确的 Perl 代码配方。它在我调用 strftime() 的地方不断
这里有没有人得到 word-wrap: break-word 在 flying-saucer 中工作?我在 flying-saucer 中将 html 渲染为 png。我可以找到一个 3 年前的话题
Windows 7,Emacs 25.1 我需要对我的自定义文本进行“即时”拼写检查(例如,强调不正确的单词)。但我在 中写文字两种语言 : 英语和俄语。我想在两种语言的拼写检查之间轻松切换。 什么是
一直在玩飞碟R8,试过从首页隐藏页眉和页脚 我的PDF。 我关注了 hint试图遵循 的 W3C 规范内容:元素() (W3C running elements)在我的 print.css 中。据描述
我正在使用由按钮触发的 FlyTo 动画来放大 map 。平移缩放动画结束后,无法使用鼠标滚轮缩小。 如果我单击基本的缩小控件,它就可以工作,如果我在触摸屏上测试它,它就可以工作。如果我尝试使用鼠标滚
我无法将Wild-fly容器连接到在其他容器中运行的PostgreSQL服务器。但是,我能够从主机连接到数据库。 最佳答案 看看networking和links。我建议联网。网络内部的容器名称可以由d
我正在使用 Flying Saucer/iText 生成报告。现在报告有一个条件,如果特定条件发生,报告应该移动到 pdf 的下一页,并在 PDF 上添加数据等等。 问候帕万 最佳答案 您必须使用 c
您好,我有一个 tabpanel,其中有两个面板,但是当我尝试使用 tabpanel.show() 显示一个面板时。我收到这条消息 Ext.fly() null or not an object do
我是一名优秀的程序员,十分优秀!