Android可以在EditText中绘制行号吗?

时间:2017-08-06 03:22:03

标签: android android-edittext android-canvas

排除单词换行?

我想要做的是在每行的左边有一个扩展EditText个行号的类。从表面上看,通过迭代0super.getLineCount()行并在一行开头绘制索引非常简单。

但是我没有看到简单的说法;跳过包裹的行(不以\n结尾的行)。例如,在vim中,它希望:

example

我正在使用上面的场景(行数没有跳过包裹的行)

protected void onDraw(Canvas canvas) {

...         
        int count = getLineCount();
        for (int i = 0; i < count; ++i) {
            getLineBounds(i, mRect);
            String num = String.valueOf(i + 1); 
            mPaint.getTextBounds(num, 0, num.length(), mNumberRect);
            canvas.drawText(num, NUMBER_LEFT_PAD, mRect.top + mTextHeight - 5, mPaint);
        }   

... 

    super.onDraw(canvas);
}

我如何检测哪些行是自动换行?哪些是换行符?

3 个答案:

答案 0 :(得分:2)

关闭迈克所说的,你可以通过检查每条渲染线来查看它是否是一条实际线(前面有一个换行符\n)或一条包含字的行并且只是递增/绘制行号,如果它是实际行。

protected void onDraw(Canvas canvas) {
    int count = getLineCount();
    int lineNumber = 1;
    for (int i = 0; i < count; ++i) {
        getLineBounds(i, mRect);
        String num = String.valueOf(i + 1); 
        mPaint.getTextBounds(num, 0, num.length(), mNumberRect);
        if (i == 0) {
            canvas.drawText(num, NUMBER_LEFT_PAD, mRect.top + mTextHeight - 5, mPaint);
            ++lineNumber;
        } else if (getText().charAt(getLayout().getLineStart(i) - 1) == '\n') {
            canvas.drawText(num, NUMBER_LEFT_PAD, mRect.top + mTextHeight - 5, mPaint);
            ++lineNumber;
        }
    }   
    super.onDraw(canvas);
}

答案 1 :(得分:0)

改进@Lrdwhyt答案中的代码,我实现了EditText的子类,如下所示。它还根据行号文本宽度设置EditText的左填充。

<html lang = "en">
   <head>
      <!-- Meta tags -->
      <meta charset = "utf-8">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
      
      <!-- Bootstrap CSS -->
      <link rel = "stylesheet" 
         href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" 
         integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
         crossorigin = "anonymous">
      
   </head>

  
   
   <body>
      <div class = "container">
         <div id = "carouselwithIndicators" class = "carousel slide w-50" data-ride = "carousel">
            <ol class = "carousel-indicators">
               <li data-target = "#carouselExampleIndicators" data-slide-to = "0" class = "active"></li>
               <li data-target = "#carouselExampleIndicators" data-slide-to = "1"></li>
               <li data-target = "#carouselExampleIndicators" data-slide-to = "2s"></li>
            </ol>
            
            <div class =" carousel-inner">
               <div class = "carousel-item active">
                  <img class = "d-block w-100" 
                     src = "https://www.tutorialspoint.com/bootstrap/images/slide1.png" 
                     alt = "First slide">
               </div>
               
               <div class = "carousel-item">
                  <img class = "d-block w-100" 
                     src = "https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" 
                     alt = "Second slide">
               </div>
               <div class = "carousel-item">
                  <img class = "d-block w-100" 
                     src = "https://www.tutorialspoint.com/bootstrap/images/slide3.png" 
                     alt = "Third slide">
               </div>
            </div>
            
            <a class = "carousel-control-prev" href = "#carouselwithIndicators" role = "button" data-slide = "prev">
               <span class = "carousel-control-prev-icon" aria-hidden = "true"></span>
               <span class = "sr-only">Previous</span>
            </a>
            
            <a class = "carousel-control-next" href = "#carouselwithIndicators" role = "button" data-slide = "next">
               <span class = "carousel-control-next-icon" aria-hidden = "true"></span>
               <span class = "sr-only">Next</span>
            </a>
         </div>
      </div>
      
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js" 
         integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" 
         integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" 
         integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" 
         crossorigin = "anonymous">
      </script>
      
   </body>
</html>

答案 2 :(得分:-1)

我推荐它。(draw line numbers in edittext

[1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7000000000000002, 1.8, 1.9, 2.0]
1.7