2

我正在开发一个 android 应用程序并使用 AChartEngine 来绘制图表。除了我在 OnTouch() 方法中捕获屏幕上的触摸坐标外,一切都正常工作。第一次触摸它不会返回任何坐标,并且 Toast 显示在左上角(由 Toast.setGravity() 设置)。在随后的触摸中,它工作得非常好,只是不是在我第一次点击或触摸屏幕时。我哪里错了?

mChartView.setOnClickListener(new View.OnClickListener() {
             Toast test;
            public void onClick(View v) {
              SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
              v.setOnTouchListener(new OnTouchListener() {
               public boolean onTouch(View v, MotionEvent event) {

                      xaxis =  event.getX();
                      yaxis =  event.getY(); 
                      xx = (int) xaxis;
                    yy = (int) yaxis;
            return false;
                    }
                });


              if (seriesSelection != null) {

                  int status_index = seriesSelection.getSeriesIndex();
                  int equipment_index = seriesSelection.getPointIndex();
                  double duration_value = seriesSelection.getValue();

                  if(status_index == 0)
                  {
                      if(equipment_index == 0)
                      {
                          duration_value = EquipmentHoursDuration[0][1];
                      }
                      if(equipment_index == 1)
                      {
                          duration_value = EquipmentHoursDuration[1][1];
                      }
                      if(equipment_index == 2)
                      {
                          duration_value = EquipmentHoursDuration[2][1];
                      }
                      if(equipment_index == 3)
                      {
                          duration_value = EquipmentHoursDuration[3][1];
                      }
                      if(equipment_index == 4)
                      {
                          duration_value = EquipmentHoursDuration[4][1];
                      }
                      String final_status = titles[status_index];
               String final_equipment = EquipmentNamePartial[equipment_index];
                    test = Toast.makeText(
                            SalesBar.this,
                              "Status: " + final_status + '\n'
                            + " Equipment: " + final_equipment + '\n'
                            + " Duration: " + duration_value + " hours", Toast.LENGTH_SHORT);


                   test.setGravity(Gravity.TOP|Gravity.LEFT, xx, yy);

                      test.show();
                  }

                  else
                  {
                 String final_status = titles[status_index];
                 String final_equipment = EquipmentNamePartial[equipment_index];
                 if(equipment_index == 0)
              {
                  duration_value = EquipmentHoursDuration[0][0];
              }
              if(equipment_index == 1)
              {
                  duration_value = EquipmentHoursDuration[1][0];
              }
              if(equipment_index == 2)
              {
                  duration_value = EquipmentHoursDuration[2][0];
              }
              if(equipment_index == 3)
              {
                  duration_value = EquipmentHoursDuration[3][0];
              }
              if(equipment_index == 4)
              {
                  duration_value = EquipmentHoursDuration[4][0];
              }



                test = Toast.makeText(
                        SalesBar.this,
                          "Status: " + final_status + '\n'
                        + " Equipment: " + final_equipment + '\n'
                        + " Duration: " + duration_value + " hours", Toast.LENGTH_SHORT);

                     test.setGravity(Gravity.TOP|Gravity.LEFT, xx, yy);

                      test.show();
                  }
              }
            }
          });
4

3 回答 3

3

这是你的代码.....返回true而不是false

 public boolean onTouch(View v, MotionEvent event) {

                      xaxis =  event.getX();
                      yaxis =  event.getY(); 
                      xx = (int) xaxis;
                    yy = (int) yaxis;
            return false;

} });

于 2012-09-28T07:01:29.863 回答
2

当你调用第一个 onClickListener 时,只有第一次触摸。之后,将照常调用 onClickListener。可能您可以尝试将 onClickListener 交换为 onTouchListener。

于 2012-09-28T05:52:08.550 回答
2
mChartView.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                     if (MotionEvent.ACTION_DOWN == event.getAction()) {
                        xaxis =  event.getRawX();

                yaxis =  event.getRawY(); 
                xx = (int) xaxis;
                yy = (int) yaxis;
                    } else if (MotionEvent.ACTION_UP == event.getAction()) {
                        xaxis =  event.getX();
                          yaxis =  event.getY(); 
                          xx = (int) xaxis;
                        yy = (int) yaxis;
                    }
                    else{}

                    return false;
                }
            });


         mChartView.setOnClickListener(new View.OnClickListener() {
             Toast test;
            public void onClick(View v) {
              SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();




              if (seriesSelection != null) {

                  int status_index = seriesSelection.getSeriesIndex();
                  int equipment_index = seriesSelection.getPointIndex();
                  double duration_value = seriesSelection.getValue();

                  if(status_index == 0)
                  {
                      if(equipment_index == 0)
                      {
                          duration_value = EquipmentHoursDuration[0][1];
                      }
                      if(equipment_index == 1)
                      {
                          duration_value = EquipmentHoursDuration[1][1];
                      }
                      if(equipment_index == 2)
                      {
                          duration_value = EquipmentHoursDuration[2][1];
                      }
                      if(equipment_index == 3)
                      {
                          duration_value = EquipmentHoursDuration[3][1];
                      }
                      if(equipment_index == 4)
                      {
                          duration_value = EquipmentHoursDuration[4][1];
                      }
                      String final_status = titles[status_index];
                     String final_equipment = EquipmentNamePartial[equipment_index];
                    test = Toast.makeText(
                            SalesBar.this,
                              "Status: " + final_status + '\n'
                            + " Equipment: " + final_equipment + '\n'
                            + " Duration: " + duration_value + " hours", Toast.LENGTH_SHORT);



                     test.setGravity(Gravity.TOP|Gravity.LEFT, xx, yy);

                      test.show();
                  }

                  else
                  {
                 String final_status = titles[status_index];
                 String final_equipment = EquipmentNamePartial[equipment_index];
                 if(equipment_index == 0)
              {
                  duration_value = EquipmentHoursDuration[0][0];
              }
              if(equipment_index == 1)
              {
                  duration_value = EquipmentHoursDuration[1][0];
              }
              if(equipment_index == 2)
              {
                  duration_value = EquipmentHoursDuration[2][0];
              }
              if(equipment_index == 3)
              {
                  duration_value = EquipmentHoursDuration[3][0];
              }
              if(equipment_index == 4)
              {
                  duration_value = EquipmentHoursDuration[4][0];
              }



                test = Toast.makeText(
                        SalesBar.this,
                          "Status: " + final_status + '\n'
                        + " Equipment: " + final_equipment + '\n'
                        + " Duration: " + duration_value + " hours", Toast.LENGTH_SHORT);





                     test.setGravity(Gravity.TOP|Gravity.LEFT, xx, yy);

                      test.show();
                  }
              }
            }
          });
于 2012-09-28T06:20:54.630 回答