从片段提供文字自定义的TextView [英] Supplying text from fragment to custom TextView

查看:89
本文介绍了从片段提供文字自定义的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何建立一个自定义的TextView构造函数能够从一个片段通过文字?

How do I set up a constructor in a custom TextView to be able to pass text from a fragment?

换句话说,我很困惑如何从我的片段(片段1)到自定义视图(视图1)发送短信:

In other words, I'm confused how to send text from my fragment (Fragment1) to the custom view (View1):

public class View1 extends TextView {

    //constructors:
    public View1(Context context, AttributeSet ats, int ds) {
        super(context, ats, ds);
        init();
    }

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

    public View1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
...
canvas.drawText(myString, margin1, margin2, paint); //myString is from Fragment1
....
}

我问过类似的问题,<一个href="http://stackoverflow.com/questions/14890491/sending-text-from-fragment-to-custom-view-where-i-want-to-use-canvas-drawtext">here,但并没有真正得到多少帮助。例如code将大大有助于清理我的困惑很长的路要走。在此先感谢!

I asked a similar question here, but didn't really get much help. Example code would go a long way towards clearing up my confusion. Thanks in advance!

推荐答案

你是一个的TextView 反正延伸。由于A - ç提到的,你可以使用的getText(),以及的setText()来获取和设置文本。

You are extending a TextView anyway. As A--C mentioned, you can use getText(), as well as setText() to get and set the text.

在你的情况下,我不知道这是否是使用的TextView 来实现自定义的视图/小部件是一个好主意。观点可能是一个更好的起点,如的TextView 携带所有类型的东西,周围的格式,图标/绘制显示,点击/按​​钮逻辑等。

In your context, I am not sure if it is a good idea to use TextView to implement your custom view/widget. View might be a better starting point, as TextView carries all kind of stuff around for formatting, icon/drawable display, click/button logic etc.

您需要的,如果你希望能够让系统实例化/从XML布局抬高你的组件来定义的标准构造。然后你可以使用标准的getter / setter方法​​为您的数据,因为所有其他控件做同样的方式。

You need to define the standard constructors if you want to be able to have the system instantiate/inflate your components from an XML layout. Then you can use standard getters/setters for your data, same way as all other controls do it.

如果你实例化你的部件/查看自己(在code),您可以自由定义任何构造要(我相信)。

If you instantiate your widget/view yourself (in your code), you are free to define whatever constructors you want to (I believe).

这篇关于从片段提供文字自定义的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆