当用户键入时更改文本颜色?

发布于 2024-12-09 19:36:51 字数 244 浏览 3 评论 0原文

当用户在 EditText 中输入时,我希望某些关键字改变颜色。 当应用程序发现用户输入关键字并使用 Html.fromHtml 时,我可以在关键字周围放置 HTML 标签,但用户将自己输入真正的 HTML 标签,这会弄乱它。所以我想我应该使用 Spannable 吗?

我到底应该如何扫描 EditText 来查找这些关键字?意思是——什么是最有效的方法?我在想也许是所有关键字的数组 - 然后循环查看是否找到任何匹配项。关于如何解决这个问题有什么想法吗?

While the user is typing in an EditText I would like certain keywords to change colors.
I could place HTML tags around the keywords when the app discovers the user typed it and use Html.fromHtml but the user will be entering real HTML tags themselves and that would mess it up. So I guess I should use Spannable?

And how exactly should I scan the EditText for these keywords? Meaning - what is the most efficient way? I was thinking maybe an array of all the keywords - and then loop through and see if any matches are found . Any ideas on how to approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑕疵 2024-12-16 19:36:51

您好,在 EditText 上使用 TextWatcher 查看用户正在输入的内容,并使用 Linkify 检查模式。
https://developer.android.com/reference/android/text/util /Linkify.html

EditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

}

使用 onTextChanged 侦听器检查用户正在输入的内容。

模式匹配示例..
int flags = Pattern.CASE_INSENSITIVE;
模式 p = Pattern.compile("\[0-9]*\", flags);
Linkify.addLinks(myTextView, p,
“内容://com.foo”);

Hi Use TextWatcher on EditText to see what user is typing and use Linkify to check pattern.
https://developer.android.com/reference/android/text/util/Linkify.html

EditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

}

Use onTextChanged Listener to check what user is typing.

Sample for Pattern matching..
int flags = Pattern.CASE_INSENSITIVE;
Pattern p = Pattern.compile("\[0-9]*\", flags);
Linkify.addLinks(myTextView, p,
"content://com.foo");

财迷小姐 2024-12-16 19:36:51

创建一个模式匹配器并将模式与 onTextChanged 侦听器中的文本进行匹配

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.EditText;

public class ColorEditText extends Activity {

private Pattern helloPattern = Pattern.compile("Hello");
private Pattern simplePattern = Pattern.compile("Simple");
private SpannableStringBuilder spannable;
private EditText edit;
private TextWatcher colorChangeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_edit_text);

    edit =(EditText) findViewById(R.id.edit);

    colorChangeText = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
            int count) {

            edit.removeTextChangedListener(colorChangeText);    

            Log.d("Changes",edit.getText().toString());

            spannable = new SpannableStringBuilder( edit.getText().toString() );

            Matcher o = helloPattern.matcher(spannable);    


            while (o.find()) {

                spannable.setSpan(new StyleSpan(Typeface.BOLD), o.start(), o.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable.setSpan(new ForegroundColorSpan(Color.rgb(103, 58,  183)), o.start(), o.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

            Matcher n = simplePattern.matcher(spannable);

            while (n.find()) {
                 spannable.setSpan(new StyleSpan(Typeface.BOLD), n.start(), n.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                 spannable.setSpan(new ForegroundColorSpan(Color.rgb(0, 105,  92)), n.start(), n.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

            edit.setText(spannable);
            edit.setSelection(spannable.length());

            edit.addTextChangedListener(colorChangeText);
        }



        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    };

    edit.addTextChangedListener(colorChangeText);

}

}

Create a Pattern Matcher and match pattern with Text in onTextChanged Listener

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.EditText;

public class ColorEditText extends Activity {

private Pattern helloPattern = Pattern.compile("Hello");
private Pattern simplePattern = Pattern.compile("Simple");
private SpannableStringBuilder spannable;
private EditText edit;
private TextWatcher colorChangeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_edit_text);

    edit =(EditText) findViewById(R.id.edit);

    colorChangeText = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
            int count) {

            edit.removeTextChangedListener(colorChangeText);    

            Log.d("Changes",edit.getText().toString());

            spannable = new SpannableStringBuilder( edit.getText().toString() );

            Matcher o = helloPattern.matcher(spannable);    


            while (o.find()) {

                spannable.setSpan(new StyleSpan(Typeface.BOLD), o.start(), o.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable.setSpan(new ForegroundColorSpan(Color.rgb(103, 58,  183)), o.start(), o.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

            Matcher n = simplePattern.matcher(spannable);

            while (n.find()) {
                 spannable.setSpan(new StyleSpan(Typeface.BOLD), n.start(), n.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                 spannable.setSpan(new ForegroundColorSpan(Color.rgb(0, 105,  92)), n.start(), n.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

            edit.setText(spannable);
            edit.setSelection(spannable.length());

            edit.addTextChangedListener(colorChangeText);
        }



        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    };

    edit.addTextChangedListener(colorChangeText);

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文