Categories

วันจันทร์ที่ 13 กรกฎาคม พ.ศ. 2552

Modifying Existing Views

การสร้าง widget ใหม่จาก control ที่มีอยู่แล้ว เราจะสร้าง class ขึ้นมาสืบทอดมัน
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
    public MyTextView (Context context, AttributeSet attrs, int defStyle)  {
         super(context, attrs, defStyle);
    }
    public MyTextView (Context context) {
         super(context);
    }
    public MyTextView (Context context, AttributeSet attrs) {
         super(context, attrs);
    }
}
ข้างบนนี่เป็นโครงคร่าว ๆ เราต้องมา Override method เพิ่มอีก เช่น
@Override
public void onDraw(Canvas canvas) {
    [ ... Draw things on the canvas under the text ... ]
    // Render the text as usual using the TextView base class.
    super.onDraw(canvas);
    [ ... Draw things on the canvas over the text ... ]
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
    [ ... Perform some special processing ... ]
    [ ... based on a particular key press ... ]
    // Use the existing functionality implemented by
    // the base class to respond to a key press event.
    return super.onKeyDown(keyCode, keyEvent);
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Search