Android的 - 如何使一个EditText所有行强调? [英] Android - How to make all lines in an edittext underlined?

查看:722
本文介绍了Android的 - 如何使一个EditText所有行强调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在教程中,我创建的布局:

From the tutorial I have created the layout:

  public static class LinedEditText extends EditText {
        private Rect mRect;
        private Paint mPaint;

        // we need this constructor for LayoutInflater
        public LinedEditText(Context context, AttributeSet attrs) {
            super(context, attrs);

            mRect = new Rect();
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(0x80000000);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            int count = getLineCount();
            Rect r = mRect;
            Paint paint = mPaint;

            for (int i = 0; i < count; i++) {
                int baseline = getLineBounds(i, r);
                canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            }

            super.onDraw(canvas);
        }
    }

<view xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.bbbfr.mynotepad.NotepadText$LinedEditText"
    android:id="@+id/note"
    android:background="#ffd6e5"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp"
    android:scrollbars="vertical"
    android:fadingEdge="vertical"
    android:gravity="top"
    android:textSize="22sp"
    android:textColor="#000000"
    android:inputType="textMultiLine"
    android:capitalize="sentences"
/>

这使得只有第一行强调。是否有可能让所有的线条强调,即使在edtittext只有一条线?

This makes only the first line underlined. Is it possible to make all the lines underlined, even if there is only one line in the edtittext?

我试图改变循环例如的for(int i = 0;我小于5;我++)后来我收到此错误:

I tried changing the loop e.g. for (int i = 0; i < 5; i++) but then I receive this error:

八月四日至28日:29:05.093:E / AndroidRuntime(14398):   java.lang.IndexOutOfBoundsException:2,1 4月28号08:29:05.093:   E / AndroidRuntime(14398):在   android.text.PackedIntVector.getValue(PackedIntVector.java:70)04-28   08:29:05.093:E / AndroidRuntime(14398):在   android.text.DynamicLayout.getLineTop(DynamicLayout.java:367)04-28   08:29:05.093:E / AndroidRuntime(14398):在   android.text.Layout.getLineBottom(Layout.java:831)8月4号至28日:29:05.093:   E / AndroidRuntime(14398):在   android.text.Layout.getLineBounds(Layout.java:437)8月4号至28日:29:05.093:   E / AndroidRuntime(14398):在   android.widget.TextView.getLineBounds(TextView.java:4122)04-28   08:29:05.093:E / AndroidRuntime(14398):在   com.bbbfr.mynotepad.NotepadText $ LinedEditText.onDraw(NotepadText.java:56)

04-28 08:29:05.093: E/AndroidRuntime(14398): java.lang.IndexOutOfBoundsException: 2, 1 04-28 08:29:05.093: E/AndroidRuntime(14398): at android.text.PackedIntVector.getValue(PackedIntVector.java:70) 04-28 08:29:05.093: E/AndroidRuntime(14398): at android.text.DynamicLayout.getLineTop(DynamicLayout.java:367) 04-28 08:29:05.093: E/AndroidRuntime(14398): at android.text.Layout.getLineBottom(Layout.java:831) 04-28 08:29:05.093: E/AndroidRuntime(14398): at android.text.Layout.getLineBounds(Layout.java:437) 04-28 08:29:05.093: E/AndroidRuntime(14398): at android.widget.TextView.getLineBounds(TextView.java:4122) 04-28 08:29:05.093: E/AndroidRuntime(14398): at com.bbbfr.mynotepad.NotepadText$LinedEditText.onDraw(NotepadText.java:56)

这一行: INT基准= getLineBounds(I,R);

我还设置机器人:行=5视图

推荐答案

<打击>如果你不介意的下划线具有相同颜色作为文字的EditText ,你应该只使用内置的<一个href="http://developer.android.com/reference/android/text/style/UnderlineSpan.html"><$c$c>UnderlineSpan,无论是通过 Html.fromHtml创造它自己或间接(...)

If you don't mind the underline having the same colour as the text in the EditText, you should really just use the built-in UnderlineSpan, either by creating it yourself or indirectly through Html.fromHtml(...).

private void createUnderlinedText() {
    String text = "I am underlined text\nLine #2\nLine #3\nLine #4\nLine #5";

    EditText underlineSpanEditText = (EditText) findViewById(R.id.underlinespan_edittext);
    SpannableStringBuilder sb = new SpannableStringBuilder(text);
    sb.setSpan(new UnderlineSpan(), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    underlineSpanEditText.setText(sb);

    EditText htmlUnderlineEditText = (EditText) findViewById(R.id.html_underline_edittext);
    String html = "<u>I am underlined text</br>Line #2</br>Line #3</br>Line #4</br>Line #5</u>";
    htmlUnderlineEditText.setText(Html.fromHtml(html));
}

与您当前做法的主要区别是,这只会强调实际文本,而不是整个文本行。例如,如果你运行我的code片段中,你会发现下划线没有延伸到当它由或断了线的末端&LT; BR /&GT; 。然而,这取决于你的行为后,这可能不是你要找的内容。

The main difference with your current approach is that this will only underline the actual text, and not the whole text line. For example, if you run my code snippet, you will find that the underline does not extend to the end of the line when it's broken off by the \n or <br/>. However, depending on the behaviour your after, this may not be what you're looking for.

编辑:所以,如果我理解正确的话,你基本上要保持在绘制水平线条的的EditText ,无论羯羊有文字或不?在你的问题的底线的一部分是一种误解,因为,事实证明,有没有什么与它(在这个词的传统意义上:)。)

So if I understand you correctly, you basically want to keep drawing horizontal lines in your EditText, no matter wether there is text or not? The 'underline' part in your question was kind of misleading, since, as it turns out, that has little to do with it (in the traditional meaning of the word :)).

不管怎么说,你不能使用 getLineCount()因为这将始终返回包含实际的文本行数。这将意味着你将不得不'补'新行字符的任意remaing空间,以获得预期的效果,这听起来有点恶心的......一个更好的选择可能是基水平线条的图纸上的总高度的EditText 。一个简单的例子,你可以明显调整,以自己的喜好:

Anyways, you can't use getLineCount() since that will always return the number of lines that contain actual text. That would mean you would have to 'fill' any remaing space with new line characters to get the desired effect, which sounds kind of yucky... A better alternative is probably to base the drawing of horizontal lines on the total height of the EditText. A quick example, which you can obviously tweak to your own liking:

public class LinedEditText extends EditText {
    private Paint mPaint = new Paint();

    public LinedEditText(Context context) {
        super(context);
        initPaint();
    }

    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPaint();
    }

    private void initPaint() {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x80000000);
    }

    @Override protected void onDraw(Canvas canvas) {
        int left = getLeft();
        int right = getRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int height = getHeight();
        int lineHeight = getLineHeight();
        int count = (height-paddingTop-paddingBottom) / lineHeight;

        for (int i = 0; i < count; i++) {
            int baseline = lineHeight * (i+1) + paddingTop;
            canvas.drawLine(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
        }

        super.onDraw(canvas);
    }
}

结果是这样的:

The result looks like this:

这篇关于Android的 - 如何使一个EditText所有行强调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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