【问题标题】:Can we write colorful text in the same EditText? [duplicate]我们可以在同一个 EditText 中编写彩色文本吗? [复制]
【发布时间】:2017-05-15 22:30:41
【问题描述】:

我使用 Android Studio。我想在同一个EditText 中写下如下照片中的彩色文字。我已经研究过,但我什么也没发现。谁能帮帮我?

示例输出如下。

这是我的代码:anaM_LV_BG_text 是一个列表视图 name_title 和内容是edittext renk_koduları_string_text 是一个包含 html 颜色的数组

   private void registerClickCallBack_text() {
        anaM_LV_BG_renk_text.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(DisplayNote.this, position + 1 + " . seçildi", Toast.LENGTH_SHORT).show();
                name_title.setTextColor(Color.parseColor(renk_koduları_string_text[position]));
                content.setTextColor(Color.parseColor(renk_koduları_string_text[position]));
               SharedPreferences preferences_text = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor_text = preferences_text.edit();

                String color_text= renk_koduları_string_text[position];
                editor_text.putString("MyColorText",color_text);
                editor_text.commit();

            }
        });
    }

【问题讨论】:

标签: android android-edittext


【解决方案1】:

使用SpannableString

EditText editText = (EditText)findViewById(R.id.my_edit_text);

Spannable spannable = new SpannableString("This is a wonderful day");        
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.RED), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(spannable);

【讨论】:

  • 我可以用这个吗?
  • 我使用列表视图。当我单击列表视图颜色更改。我有来自 xml 文件的 color_array。我使用 setOnItemClickListener 并从 edittext.getText().toString 获取文本。我可以像下面这样使用这条线吗? spannable.setSpan(new ForegroundColorSpan(array_color[position]), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
【解决方案2】:

您需要做的就是创建一个带有“html 标记”的字符串,当您将它集成到EditText 时,只需使用以下方法:Html.fromHtml()

例子:

String strData =  "<b>Hi</b> <i>I am</i> programmer"</br>
myEditText.setText(Html.fromHtml(strData));

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 1970-01-01
    • 2017-03-07
    • 2014-05-11
    • 2017-09-21
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    相关资源
    最近更新 更多