我正在使用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();
}
}
}
});
答案 0 :(得分:3)
这是你的代码.....返回true
而不是false
public boolean onTouch(View v, MotionEvent event) {
xaxis = event.getX();
yaxis = event.getY();
xx = (int) xaxis;
yy = (int) yaxis;
return false;
} });
答案 1 :(得分:2)
当您调用第一个onClickListener时,只会调用第一次触摸。之后,通常会调用onClickListener。 您可以尝试将onClickListener交换为onTouchListener。
答案 2 :(得分: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();
}
}
}
});