我是android新手,正在使用achartengine创建一个条形图。我想在用户单击图表时捕获x和y值。我已经看了从阿卡通的演示,并有我的图表创造罚款。但是,当我单击图形时onclicklistner不起作用。我已经在onresume方法中定义了侦听器,但它不起作用。任何想法都将不胜感激。我是不是丢了什么东西?
这是我的onresume方法,它取自xychartbuilder

@Override
protected void onResume() {
  super.onResume();
Toast.makeText(Clickable2.this, "In OnResume", Toast.LENGTH_SHORT).show();

  if (mChartView == null) {

Toast.makeText(Clickable2.this, "mChartView is null", Toast.LENGTH_SHORT).show();

    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
    mChartView = ChartFactory.getBarChartView(this,mDatasetMethod(titles,x,values),renderer,Type.DEFAULT);
    renderer.setClickEnabled(true);
    renderer.setSelectableBuffer(100);

    //OnClickListener
    mChartView.setOnClickListener(new View.OnClickListener() {
     // @Override
      public void onClick(View v) {
 Toast.makeText(Clickable2.this, "ON CLICK", Toast.LENGTH_SHORT).show();
        SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
        double[] xy = mChartView.toRealPoint(0);
        if (seriesSelection == null) {
          Toast.makeText(Clickable2.this, "No chart element was clicked", Toast.LENGTH_SHORT)
              .show();
        } else {
          Toast.makeText(
                  Clickable2.this,
              "Chart element in series index " + seriesSelection.getSeriesIndex()
                  + " data point index " + seriesSelection.getPointIndex() + " was clicked"
                  + " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
                  + " clicked point value X=" + (float) xy[0] + ", Y=" + (float) xy[1], Toast.LENGTH_SHORT).show();
        }
      }
    });

    //LongClickListener
    mChartView.setOnLongClickListener(new View.OnLongClickListener() {
    //  @Override
      public boolean onLongClick(View v) {
        SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
  Toast.makeText(Clickable2.this, "ON LONG CLICK", Toast.LENGTH_SHORT).show();
        if (seriesSelection == null) {
          Toast.makeText(Clickable2.this, "No chart element was long pressed",
              Toast.LENGTH_SHORT);
          return false; // no chart element was long pressed, so let something
          // else handle the event
        } else {
          Toast.makeText(Clickable2.this, "Chart element in series index "
              + seriesSelection.getSeriesIndex() + " data point index "
              + seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
          return true; // the element was long pressed - the event has been
          // handled
        }
      }
    });

    mChartView.addZoomListener(new ZoomListener() {
      public void zoomApplied(ZoomEvent e) {
        String type = "out";
        if (e.isZoomIn()) {
          type = "in";
        }
        System.out.println("Zoom " + type + " rate " + e.getZoomRate());
      }

      public void zoomReset() {
        System.out.println("Reset");
      }
    }, true, true);

    mChartView.addPanListener(new PanListener() {
      public void panApplied() {
        System.out.println("New X range=[" + renderer.getXAxisMin() + ", " + renderer.getXAxisMax()
            + "], Y range=[" + renderer.getYAxisMax() + ", " + renderer.getYAxisMax() + "]");
      }
    });

    layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    boolean enabled = mDataset.getSeriesCount() > 0;
  } else {
    mChartView.repaint();
 Toast.makeText(Clickable2.this, "mChartView is NOT null", Toast.LENGTH_SHORT).show();
  }
}

最佳答案

你能检查一下你的代码来定义一个像xymultipleseresender这样的对象吗?
也许你忘了将setClickEnabled方法设置为true?!
或者你应该试试:

setOnTouchListener

使用方法:
public boolean onTouch(View v, MotionEvent event)

对我有用。

10-08 02:57