gpt4 book ai didi

android - 样式 EditText 内容 'on the fly' ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:00 25 4
gpt4 key购买 nike

我正在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com