剛接觸Android不久,不知道哪裡的問題,求大神指點。
public class Map extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View root = inflater.inflate(R.layout.map, container, false);
FrameLayout frameLayout = (FrameLayout) root
.findViewById(R.id.maplayout);
/**
* 新建一個圖標
*
* @author prohan
* @param Context
* context int x ,int y 添加的點的左上角位置 int id 設置節點ID
*/
final Myview myview = new Myview(this.getActivity(), 830, 880);
myview.setOnClickListener(this);
myview.setId(10);
frameLayout.addView(myview);
return root;
}
public void onAttach(Activity activity) {
super.onAttach(activity);
}
class Myview extends View {
public int bitmapx;
public int bitmapy;
public Myview(Context context, int x, int y) {
super(context);
this.bitmapx = x;
this.bitmapy = y;
}
@Override
protected void onDraw(Canvas canvas) {
Paint point = new Paint();
point.setAlpha(150);
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
R.drawable.point);
canvas.drawBitmap(bitmap, null, new Rect(bitmapx, bitmapy,
bitmapx + 60, bitmapy + 100), point);
// canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
// canvas.drawBitmap(bitmap, bitmapx, bitmapy, point);
}
}
// Onclick中的方法
private void alertmessage() {
AlertDialog alert = new AlertDialog.Builder(this.getActivity())
.create();
alert.setIcon(R.drawable.ic_launcher);// 設置對話框的圖標
alert.setTitle("當前站點信息:");
alert.setMessage("XXXXXXXXXXXXXXXX");
alert.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case 10:
Toast.makeText(getActivity(), "#" + v.getId() + "#" + "8號樓監測站",
Toast.LENGTH_SHORT).show();
alertmessage();
break;
default:
break;
}
}
}
0917Android基礎自定義View的點擊事件
Android 自定義View——自定義點擊事件
----------------------biu~biu~biu~~~在下問答機器人小D,這是我依靠自己的聰明才智給出的答案,如果不正確,你來咬我啊!