【问题标题】:Custom EditText settext does not set the text自定义EditText settext 不设置文字
【发布时间】:2016-09-28 06:55:17
【问题描述】:

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

setText not working for a Custom Edittext

这仍然不起作用。我没有收到任何错误,所以不知道为什么它不起作用。

Custom TextView - setText() called before constructor

这也没用。

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>

MainActivity

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


}

【问题讨论】:

  • 简单的 et.setText(s) 不工作吗?
  • @abbath 也没有试过。

标签: android android-edittext android-custom-view


【解决方案1】:

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

所以你必须要么替换:

lt = new LinedEditText(this);

与:

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

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

【讨论】:

  • 刚开始输入相同的:)
  • 我想既然我正在实例化就足够了。
  • @WeirdNerd 没有。要点是,在其生命周期开始时的活动对布局一无所知。但是您可以致电setContentView 来帮助解决这个问题。您可以传递 xml 布局或自定义 ViewGroup。然后活动与它一起工作。在您的情况下,它是带有 LinearLayout 和继承 LinedEditText 的 xml 文件。然后,您需要通过findViewById 方法获取您的意见并与他们合作。但不是那样,而是创建一个单独的 LinedEditText 实例
  • @WeirdNerd ...然后你有 2 个 LinedEditText 实例。一个是从 xml 布局中解析出来的,第二个是由您创建的(通过显式调用一个参数构造函数)。第一个实例你不使用,第二个实例你不附加任何东西(显示它)。
  • 实例化只会为您创建新对象。要么你必须将这个新创建的对象添加回布局,要么你必须首先在 XML 中定义它,而不是为那个对象做一个新的,你必须做一个 findviewbyid 然后获取对它的引用
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 2012-11-03
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多