当自定义子视图被触摸时如何实现父活动代码?

发布于 2024-12-27 04:19:42 字数 1670 浏览 2 评论 0原文

我有一个自定义 ImageView('CustomImageView') 和一个活动“ImageViewActivity”的线性布局中的编辑文本。编辑文本最初设置为不可见。当触摸 customimageview 并调用 onDraw() 时,我希望将编辑文本的可见性设置为可见。我应该把这个代码放在哪里? ImageViewActivity 的代码:

public class ImageViewActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_imageview);


    }
}

CustomImageView 的代码是:

public class CustomImageView extends ImageView {

Paint paint = new Paint();
float xp = -1, yp = -1;
private Options opt;


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

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

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

public void init() {

    opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;

    paint.setAntiAlias(true);
    paint.setColor(Color.RED);

    paint.setStyle(Paint.Style.FILL);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {

        xp = event.getX();
        yp = event.getY();

        invalidate();

    } 
    return true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //some code
        setMeasuredDimension(width, height);

}



@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    if (xp >= 0 && yp > 0) {

            canvas.drawCircle(xp, yp, 20, paint);

        }
    }


}

I have a custom ImageView('CustomImageView') and an Edit-text in a linear layout of an activity 'ImageViewActivity'. The Edit-text is initially set invisible. When the customimageview is touched, and onDraw() is called, I want the visibility of the Edit-text to be set visible. Where should I put the code for this?
Code for ImageViewActivity:

public class ImageViewActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_imageview);


    }
}

And the code for CustomImageView is:

public class CustomImageView extends ImageView {

Paint paint = new Paint();
float xp = -1, yp = -1;
private Options opt;


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

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

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

public void init() {

    opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;

    paint.setAntiAlias(true);
    paint.setColor(Color.RED);

    paint.setStyle(Paint.Style.FILL);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {

        xp = event.getX();
        yp = event.getY();

        invalidate();

    } 
    return true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //some code
        setMeasuredDimension(width, height);

}



@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    if (xp >= 0 && yp > 0) {

            canvas.drawCircle(xp, yp, 20, paint);

        }
    }


}

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

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

发布评论

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

评论(2

一人独醉 2025-01-03 04:19:42

在您的customImageview类上实现触摸监听器,然后调用edittext来隐藏它或根据您的需要显示它,对于自定义视图上的触摸结果,您必须在您的自定义视图而不是活动上定义触摸监听器,并且您可以在touchevent中管理父活动的视图自定义视图或其他方法是在您的活动上实现触摸监听器,并找到被触摸的视图(如果触摸的视图是您的自定义视图),然后使您的 Edittext 可见。然后您将不需要在 onDraw() 中对编辑文本执行任何操作。

Implement touch listener on your customImageview class and then call the edittext to hide it or show is as you need, for results of touch on custom view you have to define touchlistener on your customview not of activity and you can manage views of parent activity in touchevent of customview OR other method is implement touch listener on you activity and find the view which is touched if touched view is your custom view then make visible your Edittext. Then you will not need to do anything in onDraw() as regarding the edittext.

半寸时光 2025-01-03 04:19:42

我建议您在特定视图上检测到触摸动作后将视图设置为可见。具体来说,用户如何触摸它并不重要,因为一旦他们触摸它,您就想显示您的视图(至少这是您所说的您想要的)。

I'd recommend setting the view to visible once you have detected a touch motion on your particular view. Specifically it doesn't matter how the user touches it, since once they touch it you want to show your view (at least that is what you said you wanted).

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