对于EDITTEXT粗体/正常状态 [英] Bold/Normal states for Edittext

查看:137
本文介绍了对于EDITTEXT粗体/正常状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我EDITTEXT,我创建了用户的选择要能够所选文本设置为粗体,但用户也应该能够取消粗体再次同选定的文本。

本的功能还包括斜体,下划线,但中风以后会加入的。

在code,使文本加粗的作品,但我不知道如何取消粗体所选文本或如何检查是否文本已经大胆的线索。

  CharacterStyle CS;
    INT开始= editText.getSelectionStart();
    INT结束= editText.getSelectionEnd();
    SpannableStringBuilder SSB =新SpannableStringBuilder(editText.getText());    开关(item.getItemId()){        案例R.id.bold:
            CS =新StyleSpan(Typeface.BOLD);
            ssb.setSpan(CS,开始,结束,1);
            editText.setText(SSB);
            返回true;


解决方案

这个算法在网络上找到解决:的 HTTPS://$c$c.google.com/archive/p/droid-writer/

  INT selectionStart = editText.getSelectionStart();
INT选定结束= editText.getSelectionEnd();如果(selectionStart>选定结束){
        INT TEMP =选定结束;
        选定结束= selectionStart;
        selectionStart =温度;
    }
    如果(选定结束> selectionStart){
        Spannable海峡= editText.getText();
        布尔存在= FALSE;
        StyleSpan [] styleSpans;        开关(item.getItemId()){
            案例R.id.bold:
                styleSpans = str.getSpans(selectionStart,选定结束,StyleSpan.class);                //如果所选文本的一部分上已经有大胆的风格,然后
                //我们需要将其禁用
                的for(int i = 0; I< styleSpans.length;我++){
                    如果(styleSpans [I] .getStyle()== android.graphics.Typeface.BOLD){
                        str.removeSpan(styleSpans [I]);
                        存在= TRUE;
                    }
                }                //否则我们设置就可以了大胆的风格
                如果(!存在){
                    str.setSpan(新StyleSpan(android.graphics.Typeface.BOLD),selectionStart,选定结束,
                            Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }                editText.setSelection(selectionStart,选定结束);
                打破;

For my Edittext, I'm creating the option for the user to be able to set selected text to bold, but the user should also be able to "unbold" the same selected text again.

The functionality for this includes also italic, underline, stroke but will be added on later.

The code that makes the text bold works, but I have no clue about how to unbold the selected text or how to check for if the text is already bold.

  CharacterStyle cs;
    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    SpannableStringBuilder ssb = new    SpannableStringBuilder(editText.getText());

    switch(item.getItemId()) {

        case R.id.bold:


            cs = new StyleSpan(Typeface.BOLD);
            ssb.setSpan(cs, start, end, 1);
            editText.setText(ssb);
            return true;

解决方案

SOLVED with this algorithm found on the web: https://code.google.com/archive/p/droid-writer/

int selectionStart = editText.getSelectionStart();
int selectionEnd = editText.getSelectionEnd();

if (selectionStart > selectionEnd) {
        int temp = selectionEnd;
        selectionEnd = selectionStart;
        selectionStart = temp;
    }


    if (selectionEnd > selectionStart) {
        Spannable str = editText.getText();
        boolean exists = false;
        StyleSpan[] styleSpans;

        switch (item.getItemId()) {
            case R.id.bold:
                styleSpans = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);

                // If the selected text-part already has BOLD style on it, then
                // we need to disable it
                for (int i = 0; i < styleSpans.length; i++) {
                    if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
                        str.removeSpan(styleSpans[i]);
                        exists = true;
                    }
                }

                // Else we set BOLD style on it
                if (!exists) {
                    str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd,
                            Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }

                editText.setSelection(selectionStart, selectionEnd);
                break;

这篇关于对于EDITTEXT粗体/正常状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆