2015-06-29 46 views
0

如何設置onClickListener和onTouchListenere在android中的視圖類上?我如何設置onClickListener和onTouchListenere在android中的視圖類上的兩個

我的代碼:

package com.example.circledemo.second; 

import java.util.ArrayList; 

import android.app.Dialog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.TextView; 

import com.example.circledemo.R; 

public class SecondTCanvas extends View { 

    public Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); Context contextData; int layoutWidth, layoutHeight; 

    private Bitmap canvasBitmap; 

    public SecondTCanvas(Context context, float[] values, int width, int height) {  super(context);   contextData = context;  layoutHeight = height;  layoutWidth = width; 

     System.out.println("=== Layout .... Width : " + layoutWidth + " Height : " + layoutHeight);  } 

    public void onDraw(Canvas c) { 

     paint.setColor(Color.BLACK);  c.drawRect(0, 0, getWidth(), getHeight(), paint);  buildDrawingCache(true);  canvasBitmap = getDrawingCache(true); } 

    @Override public boolean onTouchEvent(MotionEvent event) {  if (event.getAction() == MotionEvent.ACTION_DOWN) {    // Get x and y coordinates of where the finger touched   int pixelX = (int) (Math.floor(event.getX()));   int pixelY = (int) (Math.floor(event.getY())); 

      if (canvasBitmap != null) {    } 


      invalidate();  } 

     // If the finger is off the screen  if (event.getAction() == MotionEvent.ACTION_UP) {   // alphaIndex = -1; //This is a flag to reset the view alphas to   // normal   invalidate();  } 

     return true; 

     // return super.onTouchEvent(event); } 


} 
+0

請解釋你的問題 – Lokesh

回答

1

在使用OnTouchListner使用返回False現在你可以處理點擊聽者的點擊。

+0

我在View類中做的主要事情是檢測觸摸時的顏色代碼並打開我的自定義對話框,然後如何在觸摸或關閉圖像時關閉對話框關閉圖像駐留在自定義對話框中 –

相關問題