在自定义视图中覆盖onTouchEvent时,onClickListener不起作用

时间:2018-11-30 14:22:41

标签: android kotlin android-custom-view android-touch-event

我想在单击按钮时开始绘制线条,而在将鼠标悬停在另一个按钮上时结束绘制线条。我已经重写了onTouchEvent来画这条线,但是问题是现在onTouchListener甚至没有触发。将newView移到背景使其可以工作,但是我希望绘制的线在前景上。我认为问题在于,当我从代码中添加视图时,newView的onTouchEvent相对于另一个ViewGroup中的Button具有某种优先级。 Component Tree,将newView添加到frameLayout1

class MainActivity : AppCompatActivity() {

    private val paint = Paint()
    private val array1 = FloatArray(2)
    private val array2 = FloatArray(2)
    private var buttonwidth = 0;
    private var buttonheight=0;
    private var buttonIsTouched=false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main);
        button1.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                buttonIsTouched=true
                Toast.makeText(this,"ora",Toast.LENGTH_SHORT).show()
            }
            if (event.action == MotionEvent.ACTION_UP) {
                buttonIsTouched=false
                Toast.makeText(this,"muda",Toast.LENGTH_SHORT).show()
            }
            false
        }

    }

    override fun onResume() {
        super.onResume()

        buttonwidth = button1.width;
        buttonheight = button1.height;
        array1[0]=button1.x
        array1[1]=button1.y
        array2[0]=button2.x
        array2[1]=button2.y
        val newView = MyView(this)
        newView.bringToFront()
        newView.layoutParams= FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT)
        frameLayout1.addView(newView)
        Toast.makeText(this,array1[0].toString()+" "+array1[1].toString(),Toast.LENGTH_SHORT).show()

    }

    internal inner class MyView(context: Context)// TODO Auto-generated constructor stub
        : View(context) {
        private var startX: Int = 0
        private var startY: Int = 0
        private var endX: Int = 0
        private var endY: Int = 0
        override fun onDraw(canvas: Canvas) {

            super.onDraw(canvas)
            paint.strokeWidth=21f
            paint.color = Color.parseColor("#CD5C5C")
            //canvas.drawLine(array1[0]+20f+buttonwidth/2,array1[1]+20f+buttonheight/2,array2[0]+20f+buttonwidth/2,array2[1]+20f+buttonheight/2,paint)
            canvas.drawLine(startX.toFloat(),startY.toFloat(),endX.toFloat(),endY.toFloat(),paint)
        }

        override fun onTouchEvent(event: MotionEvent): Boolean {
            super.onTouchEvent(event)
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        startX = event.x.toInt()
                        startY = event.y.toInt()
                        endX = event.x.toInt()
                        endY = event.y.toInt()
                        invalidate()
                    }
                    MotionEvent.ACTION_MOVE -> {
                        endX = event.x.toInt()
                        endY = event.y.toInt()
                        invalidate()
                    }
                    MotionEvent.ACTION_UP -> {
                        endX = event.x.toInt()
                        endY = event.y.toInt()
                        invalidate()

                    }
                }

            return true

        }
    }

}

1 个答案:

答案 0 :(得分:-2)

您没有在

之后引用该按钮

setContentView(R.layout.activity_main)

喜欢

button1 = findViewById(R.id.btn1)