继续上一篇
问题十一:在Fragment实现类中中加载资源文件的方式:
在onCreate方法中:
LayoutInflater inflater = getActivity().getLayoutInflater(); mMainView = inflater.inflate(R.layout.task_layout_main, (ViewGroup)getActivity().findViewById(R.id.viewpager), false);
重写onCreateView方法
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO 自动生成的方法存根 Log.i("MAIN", "^^^^^^^^TaskFragment^^^^^^^^onCreateView^^^^^^^^"); ViewGroup p = (ViewGroup) mMainView.getParent(); if (p != null) { p.removeAllViewsInLayout(); Log.i("MAIN", "^^^^^^^^TaskFragment^^^^^^^^removeAllViewsInLayout!!!!!!!!"); } return mMainView; }
问题十二:SQLiteHelper实现类:
package com.android.tomatotask.Task; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SqliteHelper extends SQLiteOpenHelper { private static String INFONAME; private static String NAME; private static int VERSION = 1; static { NAME = " table_notepad"; INFONAME = "notepad.db"; } public SqliteHelper(Context paramContext, String paramString, SQLiteDatabase.CursorFactory paramCursorFactory, int paramInt) { super(paramContext, INFONAME, null, VERSION); } public void onCreate(SQLiteDatabase paramSQLiteDatabase) { paramSQLiteDatabase.execSQL("create table " + NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT)"); } public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2) { } }
问题十三:SQLite改变的类
package com.android.tomatotask.Task; import java.util.ArrayList; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class ChangeSqlite { public static String table = "table_notepad"; public long add(SQLiteDatabase paramSQLiteDatabase, Notepad paramNotepad) { ContentValues localContentValues = new ContentValues(); localContentValues.put("title", paramNotepad.getTitle()); long l = paramSQLiteDatabase.insert(table, null, localContentValues); paramSQLiteDatabase.close(); return l; } public void delete(SQLiteDatabase paramSQLiteDatabase, Notepad paramNotepad) { paramSQLiteDatabase.delete(table, "id=" + paramNotepad.getid(), null); paramSQLiteDatabase.close(); } public ArrayList<Notepad> query(SQLiteDatabase paramSQLiteDatabase) { ArrayList<Notepad> localArrayList = new ArrayList<Notepad>(); Cursor localCursor = paramSQLiteDatabase.query(table, new String[] { "id", "title" }, null, null, null, null, null); while (true) { if (!localCursor.moveToNext()) { paramSQLiteDatabase.close(); return localArrayList; } Notepad localNotepad = new Notepad(); localNotepad.setid(localCursor.getString(localCursor .getColumnIndex("id"))); localNotepad.setTitle(localCursor.getString(localCursor .getColumnIndex("title"))); localArrayList.add(localNotepad); } } public void update(SQLiteDatabase paramSQLiteDatabase, Notepad paramNotepad) { ContentValues localContentValues = new ContentValues(); localContentValues.put("title", paramNotepad.getTitle()); paramSQLiteDatabase.update(table, localContentValues, "id=" + paramNotepad.getid(), null); paramSQLiteDatabase.close(); } }
问题十二和问题十三的实现:
SQLiteDatabase localSqLiteDatabase = new SqliteHelper(getActivity(), null, null, 0).getWritableDatabase(); Notepad localNotepad = new Notepad();//自己定义的类 ChangeSqlite localChangeSqlite = new ChangeSqlite(); String strContent = editText.getText().toString(); if (strContent.equals("")) { Toast.makeText(getActivity(), "内容为空", Toast.LENGTH_SHORT).show(); } String strTitle=strContent.length()>11?" "+strContent.substring(0, 11):strContent; localNotepad.setContent(strContent); localNotepad.setTitle(strTitle); localNotepad.setdata(date); localChangeSqlite.add(localSqLiteDatabase, localNotepad);
问题十四:重写TextView,使之每行带横线的效果:
package com.android.tomatotask.Task; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.widget.TextView; public class DateLine extends TextView { private Paint ePaint = new Paint(); public DateLine(Context context) { super(context); // TODO 自动生成的构造函数存根 } public DateLine(Context context, AttributeSet attrs) { super(context, attrs); // TODO 自动生成的构造函数存根 this.ePaint.setColor(-16777216); this.ePaint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 canvas.drawLine(0.0F, 40.0F, getWidth(), 40.0F, this.ePaint); super.onDraw(canvas); } }
或者如下代码:
package com.android.tomatotask.Task; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.widget.TextView; public class TextViewLine extends TextView { private Paint ePaint = new Paint(); public TextViewLine(Context context, AttributeSet attrs) { super(context, attrs); // TODO 自动生成的构造函数存根 this.ePaint.setColor(-16777216); this.ePaint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int i=getLineCount(); for (int j = 0; ; ++j) { if (j >= i) { super.onDraw(canvas); return; } float[] arrayOfFloat = new float[4]; arrayOfFloat[0] = 15.0F; arrayOfFloat[1] = ((j + 1) * getLineHeight()); arrayOfFloat[2] = (-20 + getWidth()); arrayOfFloat[3] = ((j + 1) * getLineHeight()); canvas.drawLines(arrayOfFloat, this.ePaint); } } }
问题十五:重写EditText,使之每行带横线的效果:
package com.android.tomatotask.Task; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.widget.EditText; public class DrawLine extends EditText { private Paint ePaint; // private Rect rect; public DrawLine(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub ePaint = new Paint(); // rect=new Rect(); ePaint.setColor(Color.BLACK); ePaint.setStyle(Paint.Style.STROKE); } // 标记3,此处线字交叉是由于行间距引起,注意使用getLineHeight()得到行高。 public void onDraw(Canvas canvas) { int count = getLineCount(); for (int i = 0; i < count + 11; i++) { float[] pts = { 15.0F, (i + 1) * getLineHeight(), this.getWidth() - 20.0F, (i + 1) * getLineHeight() }; // i*50-280,50增加则行间距大285增加则线靠上。 // canvas.drawLine(15, i*42, this.getWidth()-20,i*42, ePaint); canvas.drawLines(pts, ePaint); } super.onDraw(canvas); } }
问题十四和问题十五的XML:
<com.android.tomatotask.Task.DateLine android:textColor="#ffff0000" android:gravity="center" android:id="@id/editdate" android:background="@drawable/shapetop" android:layout_width="300.0dip" android:layout_height="30.0dip" android:layout_centerHorizontal="true" /> <com.android.tomatotask.Task.DrawLine android:textSize="18.0sp" android:textColor="#ff000000" android:gravity="top" android:id="@id/edittexttwo" android:background="@drawable/shape" android:scrollbars="vertical" android:layout_width="300.0dip" android:layout_height="100.0dip" android:layout_below="@id/editdate" android:layout_centerHorizontal="true" android:lineSpacingExtra="3.0dip" />
<com.android.tomatotask.Task.DateLine android:textColor="#ffff0000" android:gravity="center" android:id="@id/changedateview" android:background="@drawable/shapetop" android:layout_width="300.0dip" android:layout_height="27.0dip" android:layout_marginTop="8.0dip" android:layout_centerHorizontal="true" /> <com.android.tomatotask.Task.TextViewLine android:textSize="15.0sp" android:textColor="#ff000000" android:id="@id/changecontentview" android:background="@drawable/shape" android:layout_width="300.0dip" android:layout_height="wrap_content" android:minHeight="68.0dip" android:layout_below="@id/changedateview" android:layout_centerHorizontal="true" android:lineSpacingExtra="6.0dip" />
问题十六:取消标题行显示:
在styles.xml文件中
<style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:windowNoTitle">true</item> </style>
注:为了阅读体验以及排版效果,本文代码与实际番茄工作法项目中代码有出入。仅作参考
应用截图: