可以设置“TextView”的文本。从类中“扩展视图”

发布于 2024-11-30 23:11:41 字数 2485 浏览 1 评论 0原文

我在布局 xml 中插入了一个名为“disegno”的自定义视图:

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="?windowsBackground">
    <it.package.myapp.Disegno
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/finestraDisegno"
       android:isScrollContainer="true" />
    <FrameLayout
       android:layout_height="0dp"
       android:layout_width="0dp"
       ...
    <EditText
       ...
       android:id="@+id/et_MOD_x">
       </EditText>

然后我实现了扩展视图类来填充自定义布局:

package it.package.myapp;
import android.content.Context;
...
public class Disegno extends View {
    public Disegno(Context context) { 
            this(context, null, 0); }
    public Disegno(Context context, AttributeSet attrs) {
            this(context, attrs, 0);    
            setContentView(R.layout.main);  }
    public Disegno(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
            init(attrs);    }
    ...
    @Override
    protected void onDraw(Canvas canvas) {
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            canvas.translate(mPosX, mPosY);
            canvas.scale(mScaleFactor, mScaleFactor);
            // chiamo routine assi cartesiani
            AssiCartesiani(canvas);
            // chiamo routine polilinea
            Polilinea(canvas); }
    ...

现在我将通过“findViewById(R. Id.et_MOD_x)”像这样更改代码:

@Override
protected void onDraw(Canvas canvas) {
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            canvas.translate(mPosX, mPosY);
            canvas.scale(mScaleFactor, mScaleFactor);
            // chiamo routine assi cartesiani
            AssiCartesiani(canvas);
            // chiamo routine polilinea
            Polilinea(canvas);

            EditText et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x)
            et_MOD_x.setText("abcd");

            }

但没有“setContentView(...)”是不可能的! 总结:我想从“扩展视图”类中设置“EditText”的文本。我想指出一个外部布局......嗯! 有什么办法可以做到这一点吗?!

I've insert a custom View called "disegno" inside a Layout xml:

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="?windowsBackground">
    <it.package.myapp.Disegno
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/finestraDisegno"
       android:isScrollContainer="true" />
    <FrameLayout
       android:layout_height="0dp"
       android:layout_width="0dp"
       ...
    <EditText
       ...
       android:id="@+id/et_MOD_x">
       </EditText>

Then I've implemented the extends View class for populate the custom layout:

package it.package.myapp;
import android.content.Context;
...
public class Disegno extends View {
    public Disegno(Context context) { 
            this(context, null, 0); }
    public Disegno(Context context, AttributeSet attrs) {
            this(context, attrs, 0);    
            setContentView(R.layout.main);  }
    public Disegno(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
            init(attrs);    }
    ...
    @Override
    protected void onDraw(Canvas canvas) {
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            canvas.translate(mPosX, mPosY);
            canvas.scale(mScaleFactor, mScaleFactor);
            // chiamo routine assi cartesiani
            AssiCartesiani(canvas);
            // chiamo routine polilinea
            Polilinea(canvas); }
    ...

Now I would to set the text of the textView "et_MOD_x" by "findViewById(R.Id.et_MOD_x)" changing the code like this:

@Override
protected void onDraw(Canvas canvas) {
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            canvas.translate(mPosX, mPosY);
            canvas.scale(mScaleFactor, mScaleFactor);
            // chiamo routine assi cartesiani
            AssiCartesiani(canvas);
            // chiamo routine polilinea
            Polilinea(canvas);

            EditText et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x)
            et_MOD_x.setText("abcd");

            }

but without "setContentView(...)" is not possible!
Summing: I would to set the text of a "EditText" from within an "extends View" class. I'd to pointing to an external layout... mmmhh!
There is any way to do this?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-12-07 23:11:41

自定义视图中有一个引用

public class Disegno extends View {
    private TextView textView;

    public void setTextView(TextView tv){
        textview = tv;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        textview.setText("Your text here!") 
    }

}

在 Activity.onCreate() 的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.my_layout);

    EditText et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x);
    Disegno finestraDisegno = (Disegno)findViewById(R.Id.finestraDisegno);
    finestraDisegno.setTextView(et_MOD_x) ;


}

have a reference in your custom view

public class Disegno extends View {
    private TextView textView;

    public void setTextView(TextView tv){
        textview = tv;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        textview.setText("Your text here!") 
    }

}

in your Activity.onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.my_layout);

    EditText et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x);
    Disegno finestraDisegno = (Disegno)findViewById(R.Id.finestraDisegno);
    finestraDisegno.setTextView(et_MOD_x) ;


}
沦落红尘 2024-12-07 23:11:41

可能这不是正确的方法,但您可以:

Get your editText in your activity and declare it as static.

public static EditText et_MOD_x;
et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x)

并在您的 View 类中设置文本:

YourActivity.et_MOD_x.setText("abcd");

希望它有帮助..

May be this is not the right way but you can :

Get your editText in your activity and declare it as static.

public static EditText et_MOD_x;
et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x)

and In your View class set the text by :

YourActivity.et_MOD_x.setText("abcd");

Hope it helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文