【问题标题】:How to bold or italic or underline the selected text in Edittext programatically如何以编程方式在Edittext中加粗或斜体或下划线
【发布时间】:2016-08-28 09:11:51
【问题描述】:

我必须开发一个像便笺这样的应用程序。用户在 Edittext 中输入文本,在选择文本时,我将打开弹出窗口以选择(粗体、斜体、下划线)之类的选项。当用户选择选项时,我需要更改所选文本..

有人经历过吗?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我从用户那里得到了选定的文本。如何在edittext中加粗显示?
  • 也许你会从这个 https://github.com/wasabeef/richeditor-android 或在谷歌搜索 RichEdittext 得到帮助
  • 我会检查并回复你

标签: android android-edittext bold


【解决方案1】:

你试过了吗?

editText.setTypeface(null, Typeface.BOLD_ITALIC);
editText.setTypeface(null, Typeface.BOLD);
editText.setTypeface(null, Typeface.ITALIC);
editText.setTypeface(null, Typeface.NORMAL);

【讨论】:

    【解决方案2】:

    科特林

    更改Typeface.BOLDTypeface.ITALICUnderlineSpan()

    val spannableString: Spannable = SpannableStringBuilder(editText.text)
            spannableString.setSpan(
            StyleSpan(Typeface.BOLD),
                editText.selectionStart,
                editText.selectionEnd,
                0
            )
    editText.setText(spannableString)
    

    【讨论】:

    • 它不断格式化编辑文本中的整个字符串
    【解决方案3】:

    如果您找到了弹出选项的方法以及如何检索所选选项(粗体,斜体,下划线),然后使用它来格式化文本

    private void formatText(EditText editText) {
        int end = editText.length();
    
        //get the selected text position as integer
        start = editText.getSelectionStart();
        stop = editText.getSelectionEnd();
        //check if the user started the selection from the left or the right
        if (start > stop) {
            stop = editText.getSelectionStart();
            start = editText.getSelectionEnd();
        }
    
            //gets the texts as html incase if previous formatting exists
        String textBefore = Html.toHtml(new SpannableString(text.getText().subSequence(0, start)));
        String selectedText = Html.toHtml(new SpannableString(text.getText().subSequence(start, stop)));
        String textAfter = Html.toHtml(new SpannableString(text.getText().subSequence(stop, end)));
        //Check if the selected text is empty ie if the did not select anything
        if (!selectedText.equals("") || start != stop) {
           //format the text
           String formatted = "<b>" + selectedText + "</b>";
           //build back the text
            StringBuilder builder = new StringBuilder();
            builder.append(textBefore);
            builder.append(Html.toHtml(formatted));
            builder.append(textAfter);
    
            editText.setText(Html.fromHtml(builder.toString()));
            //make the cursor stay after the selected text
            //you can also make the selected text selected here
            editText.setSelection(stop); 
            //clear your local variables
            textbefore = "";
            textafter = "";
            selectedText = "";
        }
        else {
            //you can also use snack bar here
            Toast.makeText(context, "select a text", Toast.LENGTH_SHORT).show();
        }
    }
    

    我认为这应该适合你

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2019-04-24
      • 1970-01-01
      • 2020-07-26
      相关资源
      最近更新 更多