获取java代码中的参考,以更改自定义编辑文本的文本颜色和背景颜色

时间:2015-08-29 20:55:19

标签: android android-layout android-fragments

public 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.FILL_AND_STROKE);
    mPaint.setColor(Color.BLACK); //SET YOUR OWN COLOR HERE
}

@Override
protected void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    super.onDraw(canvas);
}

**我使用此类进行自定义编辑文本,我可以更改xml中的属性,但我没有对这个自定义edittext的引用进行基因测试。如何在java中正确引用此编辑文本?我的xml看起来像这样**

    <com.example.goh2.pronoornotepad.LinedEditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#ffff1904"
        android:background="#ffffcc4b"
        android:gravity="top|left"
        android:singleLine="false"
        android:id="@+id/et_textEditor"
        android:text=""
     />

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

LinedEditText myview = (LinedEditText) findViewById(R.id.et_textEditor);

如果这不起作用,那么它通常是xml层次结构的问题。查看此帖子:How to pass view reference to android custom view?