android - 自定义EditText settext不设置文本

标签 android android-edittext android-custom-view

我正在使用自定义编辑文本,但问题是我无法为自定义编辑文本设置文本。这是我从 SO 上的可用答案中尝试的所有内容,

setText not working for a Custom Edittext

This did not work still. I did not get any error,so no clued why it did not work.

Custom TextView - setText() called before constructor

This also did not work.

XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <com.random.practiceproject.LinedEditText
        android:layout_width="301dp"
        android:inputType="text"
        android:layout_height="match_parent" />  


</LinearLayout>

主要 Activity

LinedEditText lt;
    EditText et; //Normal edittext works

    String s = "This is a sample string";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lt = new LinedEditText(this);
        et = (EditText) findViewById(R.id.newt);
        lt.setCursorVisible(true);
        lt.setTextColor(Color.BLACK);
        lt.setText(s,TextView.BufferType.EDITABLE);
        et.setText(s, TextView.BufferType.EDITABLE);
 }

这是代码,

public class LinedEditText extends EditText {

    private static Paint linePaint;

    static {
        linePaint = new Paint();
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
    }

    public LinedEditText(Context context)
    {

        super(context);

    }

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



    @Override
    protected void onDraw(Canvas canvas) {
        Rect bounds = new Rect();
        int firstLineY = getLineBounds(0, bounds);

        /*int firstLineY=0;

        for(int i =0;i<getLineCount();i++)
        {
            firstLineY = (i + 1) * getLineHeight();
        }*/

        int lineHeight = getLineHeight();
        int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);


        for (int i = 0; i < totalLines; i++) {
            int lineY = firstLineY + i * lineHeight;
            canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
        }

        super.onDraw(canvas);
    }


}

最佳答案

问题是您在 onCreate 中创建了 LineEditText 的新实例并使用它,但没有添加到布局中。在布局中还有另一个 LineEditText 实例,您不使用它。

因此您必须替换:

lt = new LinedEditText(this);

与:

lt = (LinedEditText) findViewById(/*provide your id*/)

或者您需要通过 ViewGroup.addView()lt 添加到布局,但我认为您需要使用第一个变体。

关于android - 自定义EditText settext不设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39740058/

相关文章:

android - 操作栏选项卡 CustomView 太长

Android 绑定(bind)适配器不适用于 CustomView

android - 仅允许使用 -Xjvm-default 选项从具有 '@JvmDefault' 成员的接口(interface)继承

Android 文本选择句柄有白色背景(而不是透明的)

java - android中json的解析

android - EditText : Can't type in content, 当 EditText 有时有焦点时

android - 多色编辑文本提示

android - 自定义 View ,如 android 的锁定/解锁屏幕

android - 测试 android 时出现 FitNesse 错误 : "Could not invoke constructor for .." & "The instance ... does not exist"

android - 如何使用 ListView 实现长按?