我是android的新手,我正在使用aChartEngine创建一个条形图。我想在用户点击图表时捕获x和y值。我查看了来自aChartEngine的演示,让我的图表创建得很好。但是当我点击graphy时,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();
}
}
答案 0 :(得分:5)
您是否可以检查代码中某些对象的定义,例如XYMultipleSeriesRenderer? 也许你忘了将setClickEnabled方法设置为true?!
或许你应该尝试:
setOnTouchListener
使用方法:
public boolean onTouch(View v, MotionEvent event)
它对我有用。
答案 1 :(得分:0)
将setSelectableBuffer
方法的参数值更改为更大的数字。