开发者

Drawing a view into custom EditText view canvas

开发者 https://www.devze.com 2023-03-23 13:45 出处:网络
I am trying to create a custom EditText view. The function of this is, as we type it will display encrypted string on screen (similar to password display, instead of *** I want to display encrypted te

I am trying to create a custom EditText view. The function of this is, as we type it will display encrypted string on screen (similar to password display, instead of *** I want to display encrypted text).

I don't want to use canvas.drawText() inside my onDraw(). I want to use TextView only to draw my text.(Reason is to

Here my code of custom view. I am facing problem, nothing is displayed inside my EditText.

public class MyEditText extends EditText {

Context context = null;
private TextView tv = null;
LinearLayout ll = null;

public TamilEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

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

public TamilEditText(Context context) {
    super(context);
    this.context = context;
    init(context);
}

private void init(Context context) {
    LayoutInflater li = LayoutInflater.from(context);
    ll 开发者_运维知识库= (LinearLayout) li.inflate(R.layout.txtlayout, null);
    tv = (TextView) ll.findViewById(R.id.txt);
}

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

    tv.draw(canvas);
    String str = encrypt(getText().toString());
    tv.setText(str);
}

private String encrypt(String str) {
    return str;
}

}

Thanks in advance.


This wont work because tv isn't attached to view hierarych so it's width and height is 0. TextView uses special class (Layout). to measure and draw Text, so you'd have to probably also modify this class.

0

精彩评论

暂无评论...
验证码 换一张
取 消