我有一個自定義的ImageView('CustomImageView')和一個活動'ImageViewActivity'的線性佈局的編輯文本。編輯文本最初設置爲不可見。當觸摸customimageview並調用onDraw()時,我希望將Edit-text的可見性設置爲可見。我應該在哪裏放置這個代碼? 代碼ImageViewActivity:如何在觸摸自定義子視圖時實現父活動代碼?
public class ImageViewActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_imageview);
}
}
而對於CustomImageView代碼:
public class CustomImageView extends ImageView {
Paint paint = new Paint();
float xp = -1, yp = -1;
private Options opt;
public CustomImageView(Context context) {
super(context);
init();
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init() {
opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
xp = event.getX();
yp = event.getY();
invalidate();
}
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//some code
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (xp >= 0 && yp > 0) {
canvas.drawCircle(xp, yp, 20, paint);
}
}
}
將自定義ImageView傳遞給EditText對象。然後將其設置爲一個字段,您可以儘可能多地混淆它。 – AJcodez 2012-01-17 10:41:50
那就是我所做的。謝謝 :) – vishalaksh 2012-01-17 11:20:02