<small id='NmPvF'></small><noframes id='NmPvF'>

<legend id='NmPvF'><style id='NmPvF'><dir id='NmPvF'><q id='NmPvF'></q></dir></style></legend>
<tfoot id='NmPvF'></tfoot>
      <bdo id='NmPvF'></bdo><ul id='NmPvF'></ul>

      1. <i id='NmPvF'><tr id='NmPvF'><dt id='NmPvF'><q id='NmPvF'><span id='NmPvF'><b id='NmPvF'><form id='NmPvF'><ins id='NmPvF'></ins><ul id='NmPvF'></ul><sub id='NmPvF'></sub></form><legend id='NmPvF'></legend><bdo id='NmPvF'><pre id='NmPvF'><center id='NmPvF'></center></pre></bdo></b><th id='NmPvF'></th></span></q></dt></tr></i><div id='NmPvF'><tfoot id='NmPvF'></tfoot><dl id='NmPvF'><fieldset id='NmPvF'></fieldset></dl></div>
      2. Android - 如何使edittext中的所有行都加下划线?

        Android - How to make all lines in an edittext underlined?(Android - 如何使edittext中的所有行都加下划线?)
            <tbody id='eKkpd'></tbody>
          • <i id='eKkpd'><tr id='eKkpd'><dt id='eKkpd'><q id='eKkpd'><span id='eKkpd'><b id='eKkpd'><form id='eKkpd'><ins id='eKkpd'></ins><ul id='eKkpd'></ul><sub id='eKkpd'></sub></form><legend id='eKkpd'></legend><bdo id='eKkpd'><pre id='eKkpd'><center id='eKkpd'></center></pre></bdo></b><th id='eKkpd'></th></span></q></dt></tr></i><div id='eKkpd'><tfoot id='eKkpd'></tfoot><dl id='eKkpd'><fieldset id='eKkpd'></fieldset></dl></div>

            <small id='eKkpd'></small><noframes id='eKkpd'>

            <tfoot id='eKkpd'></tfoot>

              <legend id='eKkpd'><style id='eKkpd'><dir id='eKkpd'><q id='eKkpd'></q></dir></style></legend>
                <bdo id='eKkpd'></bdo><ul id='eKkpd'></ul>
                  本文介绍了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"
                  />
                  

                  这只会使第一行加下划线.即使编辑文本中只有一行,是否可以将所有行都加下划线?

                  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; i < 5; i++) 但随后我收到此错误:

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

                  04-28 08:29:05.093: E/AndroidRuntime(14398):java.lang.IndexOutOfBoundsException: 2, 1 04-28 08:29:05.093:E/AndroidRuntime(14398):在android.text.PackedIntVector.getValue(PackedIntVector.java:70) 04-2808:29:05.093: E/AndroidRuntime(14398): 在android.text.DynamicLayout.getLineTop(DynamicLayout.java:367) 04-2808:29:05.093: E/AndroidRuntime(14398): 在android.text.Layout.getLineBottom(Layout.java:831) 04-28 08:29:05.093:E/AndroidRuntime(14398):在android.text.Layout.getLineBounds(Layout.java:437) 04-28 08:29:05.093:E/AndroidRuntime(14398):在android.widget.TextView.getLineBounds(TextView.java:4122) 04-2808: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 baseline = getLineBounds(i, r);

                  我还在视图中设置了 android:lines="5".

                  I have also set android:lines="5" in the view.

                  推荐答案

                  如果你不介意下划线与 EditText 中的文本颜色相同,你真的应该只是使用内置 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
                  Line #2
                  Line #3
                  Line #4
                  Line #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));
                  }
                  

                  与您当前方法的主要区别在于,这只会在实际文本下划线,而不是整个文本行.例如,如果您运行我的代码片段,您会发现下划线在被 <br/> 断开时并没有延伸到行尾..但是,根据您之后的行为,这可能不是您想要的.

                  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 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() 因为它总是会返回包含实际文本的行数.这意味着您必须用换行符填充"任何剩余空间才能获得所需的效果,这听起来有点恶心......更好的选择可能是将水平线的绘制基于 <的总高度代码>编辑文本.一个简单的示例,您显然可以根据自己的喜好对其进行调整:

                  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);
                      }
                  }
                  

                  结果如下:

                  这篇关于Android - 如何使edittext中的所有行都加下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                1. <tfoot id='RshlQ'></tfoot>
                    <tbody id='RshlQ'></tbody>
                    <bdo id='RshlQ'></bdo><ul id='RshlQ'></ul>

                    <i id='RshlQ'><tr id='RshlQ'><dt id='RshlQ'><q id='RshlQ'><span id='RshlQ'><b id='RshlQ'><form id='RshlQ'><ins id='RshlQ'></ins><ul id='RshlQ'></ul><sub id='RshlQ'></sub></form><legend id='RshlQ'></legend><bdo id='RshlQ'><pre id='RshlQ'><center id='RshlQ'></center></pre></bdo></b><th id='RshlQ'></th></span></q></dt></tr></i><div id='RshlQ'><tfoot id='RshlQ'></tfoot><dl id='RshlQ'><fieldset id='RshlQ'></fieldset></dl></div>
                    <legend id='RshlQ'><style id='RshlQ'><dir id='RshlQ'><q id='RshlQ'></q></dir></style></legend>

                        <small id='RshlQ'></small><noframes id='RshlQ'>