Lập trình di động - Lab06: GridView abd Other Control

a. Tạo new project HelloAutoComplete

b. Tạo xml file với tên list_item.xml được lưu trong /res/layout/ với nội dung:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

android:textSize="16sp"

android:textColor="#000">

pdf25 trang | Chia sẻ: tieuaka001 | Lượt xem: 487 | Lượt tải: 0download
Bạn đang xem trước 20 trang nội dung tài liệu Lập trình di động - Lab06: GridView abd Other Control, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
} d. Override onPrepareDialog(int) trong Activity và thêm đoạn code với nội dung sau switch(id) { case PROGRESS_DIALOG: progressDialog.setProgress(0); progressThread = new ProgressThread(handler); progressThread.start(); } e. Tạo Thread trong Activity với nội dung sau private class ProgressThread extends Thread { Handler mHandler; final static int STATE_DONE = 0; final static int STATE_RUNNING = 1; int mState; int total; ProgressThread(Handler h) { mHandler = h; } public void run() { mState = STATE_RUNNING; total = 0; while (mState == STATE_RUNNING) { try { Thread.sleep(100); } catch (InterruptedException e) { Log.e("ERROR", "Thread Interrupted"); } Ver 1.0 – 2016, FIT - HCMUP Lab 06: GridView abd Other Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 19 Message msg = mHandler.obtainMessage(); msg.arg1 = total; mHandler.sendMessage(msg); total++; } } /* sets the current state for the thread, * used to stop the thread */ public void setState(int state) { mState = state; } } f. Tạo Handle trong Activity final Handler handler = new Handler() { public void handleMessage(Message msg) { int total = msg.arg1; progressDialog.setProgress(total); if (total >= 100){ dismissDialog(PROGRESS_DIALOG); progressThread.setState(ProgressThread.STATE_DONE); } } }; g. Trong layout.xml thêm button <Button android:text="Start" android:id="@+id/progressDialog" android:layout_width="wrap_content" android:layout_height="wrap_content"> h. Trong onCreate() thêm vào đoạn code button = (Button) findViewById(R.id.progressDialog); button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { showDialog(PROGRESS_DIALOG); } }); i. Run ứng dụng Ver 1.0 – 2016, FIT - HCMUP Lab 06: GridView abd Other Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 20 2.9 Làm việc với Custom View sử dụng touch sự kiện a. Tạo new project b. Thay đổi main.layout xml với nội dung <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Drag the droid around" /> <FrameLayout android:id="@+id/graphics_holder" android:layout_height="match_parent" android:layout_width="match_parent"> c. Tạo biến string trong Activity với tên private static final String DEBUG_TAG = "GestureFunActivity"; d. Tạo mới class với tên PlayAreaView extends View e. Định nghĩa biến trong lớp này private GestureDetector gestures; private Matrix translate; private Bitmap droid; f. Trong contructor() bổ sung thêm đoạn code translate = new Matrix(); gestures = new GestureDetector(GestureFunActivity.this, new GestureListener(this)); droid = BitmapFactory.decodeResource(getResources(), R.drawable.droid_g); g. Override phương thức onDraw() , thêm đoạn code canvas.drawBitmap(droid, translate, null); Matrix m = canvas.getMatrix(); Log.d(DEBUG_TAG, "Matrix: " + translate.toShortString()); Log.d(DEBUG_TAG, "Canvas: " + m.toShortString()); h. Override phương thức onTouchEvent() , thêm đoạn code return gestures.onTouchEvent(event); Ver 1.0 – 2016, FIT - HCMUP Lab 06: GridView abd Other Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 21 i. Định nghĩa 2 phương thức trong class với nội dung public void onMove(float dx, float dy) { translate.postTranslate(dx, dy); invalidate(); } public void onResetLocation() { translate.reset(); invalidate(); } j. Tạo mới class với tên GestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTap Listener k. Định nghĩa đối tượng trong class PlayAreaView view; l. Trong Contructor() của class thêm vào dòng code this.view = view; m. Thêm đoạn code @Override public boolean onDoubleTap(MotionEvent e) { Log.v(DEBUG_TAG, "onDoubleTap"); view.onResetLocation(); return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { //Log.v(DEBUG_TAG, "onScroll"); view.onMove(-distanceX, -distanceY); return true; } n. Chạy ứng dụng 2.10 Làm việc với Custom Layout a. Tạo new project Ver 1.0 – 2016, FIT - HCMUP Lab 06: GridView abd Other Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 22 b. Tạo new class extends LinearLayout c. Định nghĩa biến trong class private Paint innerPaint, borderPaint ; d. Trong contructor() gọi init() e. Định nghĩa hàm init() trong class với nội dung private void init() { innerPaint = new Paint(); innerPaint.setARGB(225, 75, 75, 75); //gray innerPaint.setAntiAlias(true); borderPaint = new Paint(); borderPaint.setARGB(255, 255, 255, 255); borderPaint.setAntiAlias(true); borderPaint.setStyle(Style.STROKE); borderPaint.setStrokeWidth(2); } f. Override phương thức dispatchDraw() với nội dung @Override protected void dispatchDraw(Canvas canvas) { RectF drawRect = new RectF(); drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight()); canvas.drawRoundRect(drawRect, 5, 5, innerPaint); canvas.drawRoundRect(drawRect, 5, 5, borderPaint); super.dispatchDraw(canvas); } g. Thay đổi trong layout main.xml với nội dung <FrameLayout xmlns:android="" android:id="@+id/home_container" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView android:id="@+id/city_map_view" Ver 1.0 – 2016, FIT - HCMUP Lab 06: GridView abd Other Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 23 android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="0DUEIIn35xtmfWC2DXprK5kqNF- aEaNgRJ4ONxw"/> <LinearLayout xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="bottom" android:paddingLeft="5px" android:paddingTop="5px" android:paddingRight="5px"> <com.pocketjourney.view.TransparentPanel android:id="@+id/transparent_panel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5px" android:paddingLeft="5px" android:paddingBottom="5px" android:paddingRight="5px"> <Button android:id="@+id/button_click_me" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!"/> h. Thay đổi Activity thành MapActivity, thêm đoạn code trong onCreate() Button button = (Button) findViewById(R.id.button_click_me); button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Toast.makeText(Tutorial1.this, "Button Clicked",Toast.LENGTH_SHORT).show(); }}); Ver 1.0 – 2016, FIT - HCMUP Lab 06: GridView abd Other Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 24 i. Thực hiện quá trình build như trong Lab MapView j. Cài đặt và chạy ứng dụng 2.11 Làm việc với ViewFliper a. Tạo new project b. Thay đổi layout main.xml với nội dung <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="" android:orientation="vertical"> <LinearLayout android:id="@+id/LinearLayout03" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next"> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Previous"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ViewFlipper android:id="@+id/ViewFlipper01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Flipper Content 1"> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Flipper Content 2"> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Flipper Content 3"> Ver 1.0 – 2016, FIT - HCMUP Lab 06: GridView abd Other Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 25 c. Trong Activity implement sự kiện onClickListener và định nghĩa biến Button next; Button previous; ViewFlipper vf; d. Thêm đoạn code trong onCreate() của Activity vf = (ViewFlipper) findViewById(R.id.ViewFlipper01); next = (Button) findViewById(R.id.Button01); previous = (Button) findViewById(R.id.Button02); next.setOnClickListener(this); previous.setOnClickListener(this); e. Override phương thức onClick() và thêm đoạn code if (v == next) { vf.showNext(); } if (v == previous) { vf.showPrevious(); } f. Chạy ứng dụng

Các file đính kèm theo tài liệu này:

  • pdfmad_lab06_gridview_5878.pdf
Tài liệu liên quan