简体   繁体   中英

Set button in custom view (canvas)

I would like to set up a button that would allow me to move to another activity while I finished drawing my painting.

The question is I don't know how to set up this button in the view class because it doesn't listen to any listeners.

In your drawing view, define your interface

public class MyDrawingView extends View
{
    protected MyPaintingListener m_paintingListener;     

    public interface MyPaintingListener 
    {
        // you can define any parameter as per your requirement
        public void paintingEnded();
    }

    public void onCreateView()
    {
        // Create your view
    }

    public void draw()
    {
        // Draw your painting
        // then
        if(m_paintingListener != null)
            m_paintingListener.paintingEnded();
    }

    public void setListener(MyPaintingListener p_listener)
    {
        m_paintingListener = p_listener;
    }
}

In your current Fragment or Activity:

public class MyActivity extends Activity
    implements MyDrawingView.MyPaintingListener
{
    protected MyDrawingView m_drawingView;

    public void OnActivityCreated(Bundle savedInstanceState)
    {
        // In this method or another, create your drawingView 
        m_drawingView = new MyDrawingView();
        m_drawingView.setListener(this);
        m_drawingView.paint();
    }


    @Override
    public void paintingEnded()
    {
        // Set up your button;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM