这里是在做安卓的一个VIEW,想在这个VIEW里面设置一个Mouselistener的监听器,用了this.addmouselistener()但是他总是提醒addMouseListener不能添加给继承了view的类对象,而且我尝试着用getparent的方法也不能实现,该怎么办啊。。
import java.util.List;
import java.awt.*;
import com.example.mobiledoctor.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
/**
*
*
* @author Administrator
*
*/
public class HomeColumnar extends View {
private List<Score> score;
private float tb;
private float interval_left_right;
private Paint paint_date, paint_rectf_gray, paint_rectf_blue;
private int fineLineColor = 0x5faaaaaa;
private int blueLineColor = 0xff00ffff;
public HomeColumnar(Context context, List<Score> score) {//构造方法
super(context);
init(score);
}
public void init(List<Score> score) {
if (null == score || score.size() == 0)
return;
this.score = score;
Resources res = getResources();
tb = res.getDimension(R.dimen.historyscore_tb);
interval_left_right = tb * 5.0f;
paint_date = new Paint();
paint_date.setStrokeWidth(tb * 0.1f);
paint_date.setTextSize(tb * 1.2f);
paint_date.setColor(fineLineColor);
paint_date.setTextAlign(Align.CENTER);
paint_rectf_gray = new Paint();
paint_rectf_gray.setStrokeWidth(tb * 0.1f);
paint_rectf_gray.setColor(fineLineColor);
paint_rectf_gray.setStyle(Style.FILL);
paint_rectf_gray.setAntiAlias(true);
paint_rectf_blue = new Paint();
paint_rectf_blue.setStrokeWidth(tb * 0.1f);
paint_rectf_blue.setColor(blueLineColor);
paint_rectf_blue.setStyle(Style.FILL);
paint_rectf_blue.setAntiAlias(true);
setLayoutParams(new LayoutParams(
(int) (this.score.size() * interval_left_right),
LayoutParams.MATCH_PARENT));
}
protected void onDraw(Canvas c) {
if (null == score || score.size() == 0)
return;
drawDate(c);
drawRectf(c);
};
//画长方形
public void drawRectf(Canvas c) {
for (int i = 0; i < score.size(); i++)
{
//监听的对象应该是每一个RectF,是这样一个对象
//指的是外面的长方形框架
RectF f = new RectF();
f.set(tb * 0.2f + interval_left_right * i, getHeight() - tb * 11.0f, tb * 3.2f + interval_left_right*i, getHeight() - tb * 2.0f);
c.drawRoundRect(f, tb * 0.3f, tb * 0.3f, paint_rectf_gray);
float base = score.get(i).score * (tb * 10.0f / 100);
//代表数据长度的蓝色的长方形部分
RectF f1 = new RectF();
f1.set(tb*0.2f+interval_left_right*i, getHeight()-(base+tb*1.5f), tb*3.2f+interval_left_right*i, getHeight()-tb*1.5f);
c.drawRoundRect(f1, tb * 0.3f, tb * 0.3f, paint_rectf_blue);
//为这个长方形设置一个监听器!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
f.
}
}
public void drawDate(Canvas c) {
//利用循环写日期的部分
for (int i = 0; i < score.size(); i++)
{
String date = score.get(i).date;
String date_1 = date.substring(date.indexOf("-") + 1, date.length());
c.drawText(date_1, tb * 1.7f + interval_left_right * i,
getHeight(), paint_date);
}
}
}
想实现的是能够监听到点击每个长方形的作用,因为不能直接给RectF添加监听器,就想给整体添加,然后再找到x,y再判断,但是现在怎么也添加不上啊,求帮助啊
------解决思路----------------------
android是OnTouchListener不是onMouselistener
第二行import java.awt.*; 导错包了把!