Android: custom view onClickEvent with X & Y locations

后端 未结 6 640
你的背包
你的背包 2020-12-21 15:12

I\'ve got a custom view and I want to get the X and Y coordinates of a user click. I\'ve found that I can only get the coordinates from the onTouchEvent and an onClickEvent

6条回答
  •  情歌与酒
    2020-12-21 15:42

    I have able solve this problem

    I created an Custom View public class QuestionBuilderView extends View

    In layout File

     < com.example.android.view.QuestionBuilderView
    
            android:id="@+id/questionbuilderview"
    
            android:layout_width="fill_parent"
    
            android:layout_height="wrap_content" />
    

    In an main Activity try to call the custom view and set the Onclick / On Touch Event Listener to the view

     View questionBuilder =  findViewById(R.id.questionbuilderview);
             questionBuilder.setOnTouchListener(new OnTouchListener() {
    
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    // TODO Auto-generated method stub
                    float touched_x = event.getX();
                   float  touched_y = event.getY();
                   Toast.makeText(getApplicationContext(), " "+touched_x +","+touched_y, Toast.LENGTH_LONG).show();
                  //  int action = event.getAction();
    
    
                    return true;
                }
    

提交回复
热议问题