在扩展ImageView的类中设置OnClick

时间:2016-08-21 10:40:21

标签: android imageview ontouchlistener

我有一个名为examplecanvas.java的类,它扩展了ImageView。该类由XML中的自定义视图调用。在那个XML中有一个按钮,我想在examplecanvas.java中设置OnClick。

我已将OnClickListener放在OnTouchEvent中,但它不起作用。这是我的代码:

examplecanvas.java(扩展ImageView):

public class examplecanvas extends ImageView {

    float xPos, yPos;
    private PointF point;

    public examplecanvas(Context context) {
        super(context);
    }

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

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

   @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {

        Button savepoints = (Button) findViewById(R.id.btnpdf );
        savepoints    public void onClick(View ImageView) {
            Toast.makeText(getContext(), "points saved", Toast.LENGTH_SHORT).show();
        }
    });

        float x = event.getX();
        float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            point = new PointF(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            point.set(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            point.set(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
            point.set(x, y);
            invalidate();
            break;
        }
        return true;
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {

        super.onDraw(canvas);

        float flat = (float)doublelat;
        float flon = (float)doublelon;

        canvas.drawCircle(flat, flon, 100, paint);

            if (point != null) {
                canvas.drawCircle(point.x, point.y, 100, paint);
                canvas.save();
            }
    }
}

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.joanzapata.pdfview.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <view
        class="com.myapp.trial10.examplecanvas"
        android:layout_gravity="center"
        android:id="@+id/yourID2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"
        android:text="save"
        android:id="@+id/btnpdf"
        android:layout_gravity="center_horizontal|bottom" />


 </FrameLayout>

活动:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import com.joanzapata.pdfview.PDFView;

public class pdfview3 extends ActionBarActivity {


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


        String assetFileName = "proposed.pdf"; //The name of the asset to open
        int pageNumber = 0; //Start at the first page

        PDFView pdfView = (PDFView) findViewById(R.id.pdfView); //Fetch the view
        pdfView.fromAsset(assetFileName) //Fill it with data
            .defaultPage(pageNumber) //Set default page number
            .onPageChange(null) //PageChangeListener
            .enableSwipe(false)
            .load();


    }
}

1 个答案:

答案 0 :(得分:0)

所以你想为ImageView添加onClickListener? -add setOnClickListener(this);在创造 -make类实现OnClickListener - 实现onClick方法 -rename视图在xml中使用View(或直接放入package.examplecanvas而不是view并删除class = ...)