如何在EditText中显示行号

时间:2017-11-30 17:32:13

标签: android android-edittext

这里我正在尝试构建一个编辑文本,其中显示新行遇到时的行号。我按照此Using this Link.中的说明制作了自己的自定义编辑文字 这是我的代码

public class MyEditText extends android.support.v7.widget.AppCompatEditText {

    private Rect rect;
    private Paint paint;

    public MyEditText(Context context) {
        super(context);
        rect = new Rect();
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);
        paint.setTextSize(20);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int baseline = getBaseline();
        for (int i = 0; i < getLineCount(); i++) {
            canvas.drawText("" + (i+1), rect.left, baseline, paint);
            baseline += getLineHeight();
        }
        super.onDraw(canvas);
    }
}

这是MainActvity的XML布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <android.support.v7.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Type here"
            android:inputType="textMultiLine"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:id="@+id/editor_area"
            android:backgroundTint="@android:color/transparent"
            />
    </ScrollView>
</LinearLayout>

这是主要活动代码

public class MainActivity extends AppCompatActivity {
Button button;
    TextView textView;
    MyEditText myEditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myEditText = (MyEditText) findViewById(R.id.editor_area);

             // Now what should be further steps to achieve the task
    }
}

我无法弄清楚接下来应该做什么来做这个行号。我在stackoverflow上看到很多答案,但没有解决查询,我是android的新手。请帮忙。

由于

编辑:根据以下答案进行更改后,这就是我在logcat窗口中获得的内容

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aman.demo/com.example.aman.demo.MainActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.example.aman.demo.MyEditText
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                    Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.example.aman.demo.MyEditText
                                                    Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.example.aman.demo.MyEditText
                                                    Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                                                       at java.lang.Class.getConstructor0(Class.java:2320)
                                                       at java.lang.Class.getConstructor(Class.java:1725)
                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:615)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                                                       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
                                                       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                       at com.example.aman.demo.MainActivity.onCreate(MainActivity.java:18)
                                                       at android.app.Activity.performCreate(Activity.java:6975)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

3 个答案:

答案 0 :(得分:1)

自定义EditText xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical|horizontal"
    android:fillViewport="true"
    tools:context="com.wade.webview.HtmlEditActivity">
    <com.wade.webview.LineNumberEditText
        android:id="@+id/lineNumberEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ***android:paddingLeft="100px"***
        android:gravity="top|left"
        ***android:scrollbars="vertical|horizontal"***
        android:text=""
        android:ellipsize="end"
        />
</ScrollView>

自定义EditText java     包com.wade.webview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;

public class LineNumberEditText extends android.support.v7.widget.AppCompatEditText {
    private Rect rect;
    private Paint paint;
    final LineNumberEditText me;

    public LineNumberEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        me = this;
        rect = new Rect();
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextSize(40);
        **setHorizontallyScrolling(true);
        setMovementMethod(new ScrollingMovementMethod());**
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int baseline = getBaseline();
        for (int i = 0; i < getLineCount(); i++) {
            canvas.drawText(String.format("%04d:", (i+1)), rect.left, baseline, paint);
            baseline += getLineHeight();
        }
    }
}

请参阅https://www.github.com/wade-fs/wade_webview

答案 1 :(得分:0)

在您的布局XML中,您没有使用您创建的子类EditText,您需要通过your.package.declaration.MyEditText替换android.support.v7.widget.AppCompatEditText来声明它。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <your.package.declaration.MyEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Type here"
            android:inputType="textMultiLine"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:id="@+id/editor_area"
            android:backgroundTint="@android:color/transparent"
            />
    </ScrollView>
</LinearLayout>

如果您想要&#34;键入此处&#34;不能成为文本的一部分取代android:text by android:hint

此外,您的MyEditText会错过XML通胀所必需的构造函数(Context,AttributeSet),因此请将其添加到类中:

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    rect = new Rect();
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.BLACK);
    paint.setTextSize(20);
}

最后一点,在自己绘制数字之前调用super.onDraw。

超级实现可能会绘制白色背景,删除数字

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int baseline = getBaseline();
    for (int i = 0; i < getLineCount(); i++) {
        canvas.drawText("" + (i+1), rect.left, baseline, paint);
        baseline += getLineHeight();
    }
}

结果是: Working

不幸的是,绘图是与文本连接,我会说增加文本paddiing但是没有用。

答案 2 :(得分:0)

在xml上替换它:

<android.support.v7.widget.AppCompatEditText

对此:

<your.package.MyEditText

同时将此参数添加到您的editext

 <your.package.MyEditText 
    android:inputType="textMultiLine"
     <!-- Multiline input -->
     android:lines="8"
     <!-- Total Lines prior display --> 
     android:minLines="6"
     <!-- Minimum lines --> 
    android:gravity="top|left" 
    <!-- Cursor Position -->
     android:maxLines="10" 
    <!-- Maximum Lines --> 
    android:layout_height="wrap_content" 
    <!-- Height determined by content --> 
    android:layout_width="match_parent"
     <!-- Fill entire width --> 
     android:scrollbars="vertical" 
    <!-- Vertical Scroll Bar --> />

你的崩溃是因为你需要xml文件中的构造函数,添加:

public MyEditText(Context context, AttributeSet attrs) {
    super(context,attrs);
    init();
}

public MyEditText(Context context) {
    super(context);
    init();
}

private void init(){

    rect = new Rect();
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.BLACK);
    paint.setTextSize(20);
}