如何使用HTML标记将文本文本加下划线

时间:2018-01-22 16:27:20

标签: android android-edittext

我是android的初学者所以请原谅我,如果我的问题是愚蠢的。我想知道如何使用html标签使编辑文本的文本加下划线。 例如,使用TextView,我可以像textView.setText(Html.fromHtml("<u>Hi</u>"))那样进行操作,但如何将其与EditText一起使用?

虽然我可以像editText.setText(Html.fromHtml("<u>Hi</u>"))那样做,但如何为用户输入的文字加下划线

6 个答案:

答案 0 :(得分:2)

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private EditText passwordEditText;
    private TextView textView;

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

        /* Initializing views */
        passwordEditText = (EditText) findViewById(R.id.password);


        /* Set Text Watcher listener */
        passwordEditText.addTextChangedListener(passwordWatcher);
    }

    private final TextWatcher passwordWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String incomingText = passwordEditText.getText();
            passwordEditText.setText(Html.fromHtml("<u>"+incomingText+"</u>"));
        }

        public void afterTextChanged(Editable s) {
            if (s.length() == 0) {
                textView.setVisibility(View.GONE);
            } else{
                textView.setText("You have entered : " + passwordEditText.getText());
            }
        }
    };
}

答案 1 :(得分:2)

Html.fromHtml(mString)方法为deprecated。避免使用它,改为使用Spannable

String mText="Hi";
SpannableString content = new SpannableString(mText);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mView.setText(content); //set it to EditText or TextView

这是在输入时设置它的一个例子,

 et_my_text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            stringBeforeChange = s.toString(); //temp variable
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!et_my_text.getText().toString().equalsIgnoreCase(stringBeforeChange)) { //this prevents infinite loop
                SpannableString content = new SpannableString(s.toString());
                content.setSpan(new UnderlineSpan(), 0, s.toString().length(), 0);
                et_my_text.setText(content);
                et_my_text.setSelection(et_my_text.getText().length()); //this keeps cursor at the end of editText
            }
        }
    });

答案 2 :(得分:1)

以下是如何将文本更改为用户类型:

TextWatcher tw = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) { 
            editText.removeTextChangedListener(tw);

            String text = editText.getText().toString(); 
            editText.setText(Html.fromHtml("<u>" + text + "</u>");

            editText.addTextChangedListener(tw);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

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

editText.addTextChangedListener(tw);

答案 3 :(得分:0)

虽然可以使用editext执行此操作,但我建议您使用RichEdititor,因为如果您尝试加载大型HTML,本机EditText开始变慢。 Wasabeef有一个很棒的library

enter image description here

RichEditor editor = (RichEditor) findViewById(R.id.editor);

        findViewById(R.id.action_underline).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditor.setUnderline();
            }
        });

您还可以添加其他内容,例如subscriptsuperscriptstrikethrough e.t.c

答案 4 :(得分:0)

处理文本更改的一种非常方便的方法是使用“com.jakewharton.rxbinding.widget”

RxTextView.textChanges(editText).map(text -> Html.fromHtml("<u>" + text + "</u>")).subscribe();

(我没有测试过代码,但它应该像那样工作)

您可以将它包含在您的依赖项中:

implementation "com.jakewharton.rxbinding:rxbinding:${rxBindingsVersion}"

答案 5 :(得分:0)

使用TextWatcherSpannableString

    et_user_email.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
        }

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

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

            et_user_email.removeTextChangedListener(this);

            SpannableString content = new SpannableString(s);
            content.setSpan(new UnderlineSpan(), 0, s.length(), 0);

            et_user_email.setText(content);
            et_user_email.setSelection(s.length());

            et_user_email.addTextChangedListener(this);

        }
    });

结果 -

edittext spannable