1. 程式人生 > >部落格遷移到 http://www.blog4app.com/

部落格遷移到 http://www.blog4app.com/

1、ContentProvider的使用

       NotePad.java定義了資料庫中唯一的Notes表的若干欄位及其屬性。Notes表實現了BaseColumns介面,即擁有了_id和_count的屬性。資料庫表的Uri的命名規則一般是:content://**/資料庫名   (**代表provider的authorities)。

     NotePadProvider.java繼承自ContentProvider,所以需要實現onCreate()、query()、insert()、delete()、update()和getType()共六個方法。

          onCreate方法在ContentProvider初始化的時候,執行相應的語句,如果初始化成功返回true,否則返回false。一般在該方法裡,初始化資料庫獲取DatabaseHelper的物件,所有資料庫表的建立都是在Databasehelper物件的onCreate方法裡執行的。

          getType方法的作用是:當使用隱式的Intent呼叫activity的時候,該方法的返回值決定了activity是否被選中。隱式呼叫activity方法

            intent.setAction(action);
            intent.setData(data);
            intent.addCategory(category);

           <intent-filter android:label="@string/resolve_edit">
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />
                <action android:name="com.android.notepad.action.EDIT_NOTE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
            </intent-filter>
     getType的方法返回值和mimeType的值對應。

3、擴充套件EditText的LineEditText控制元件。注意getLineCount和getLineBounds兩個方法。

public static class LinedEditText extends EditText {
        private Rect mRect;
        private Paint mPaint;

        // we need this constructor for LayoutInflater
        public LinedEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            
            mRect = new Rect();
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(0x800000FF);
        }
        
        @Override
        protected void onDraw(Canvas canvas) {
            int count = getLineCount();
            Rect r = mRect;
            Paint paint = mPaint;

            for (int i = 0; i < count; i++) {
                int baseline = getLineBounds(i, r);

                canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            }

            super.onDraw(canvas);
        }
    }