Pichayas, Android, C, C++, Java, J2ME, J2EE, Spring

Categories

วันอาทิตย์ที่ 13 ธันวาคม พ.ศ. 2552

Code highlighting on blogger

http://cubicware.blogspot.com/2009/01/code-highlighting-on-blogger.html

[+/-] Show Full Post...

วันพุธที่ 22 กรกฎาคม พ.ศ. 2552

Android Techniques for Saving Data

❑ Shared Preferences เป็นข้อมูลแบบ key/value เอาไว้เก็บ UI state, user preferences หรือ application settings
❑ Files เก็บลองไฟล์โดยตรง Android อนุญาติให้คุณสร้าง และโหลดไฟล์ทั้งจากในเครื่อง และการ์ด
❑ SQLite Databases ถ้าต้องการการจัดการที่สูงขึ้น structured data น่าจะดีที่สุด, Android มี library ของ SQLite relational database. ทุก ๆ application สามารถสร้าง databases ของตัวเองได้ และจัดการได้ทั้งหมด
❑ Content Providers
นอกจากข้อมูลด้วยสิทธิ์ของตัวเองแล้ว, Content Providers อนุญาติให้คุณดึงข้อมูลที่ถูกสร้างไว้แล้วจาก interface ของโปรแกรมคนอื่น คุณสามารถจัดการการเข้าถึง Content Providers โดยใช้ระบบ permission

[+/-] Show Full Post...

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

TIP on Android Development

เทคนิคที่สำคัญที่สุดอันหนึ่งของการเขียนโปรแกรมบน Android คือการหลีกเลี่ยงการสร้าง และ ทำลาย object บ่อย ๆ ซึ่งทุก ๆ object ที่ถูกสร้างใน method onDraw จะถูกสร้างและทำลายทิ้ง ทุก ๆ ครั้งที่คุณ refresh หน้าจอ เราจะเพิ่มประสิทธิภาำพให้โปรแกรมได้โดยการย้ายการสร้าง object พวกนี้ (โดยเฉพาะ instance ของ Paint และ Drawable) ไปใว้ใน constructor
public class MyView extends View {
    // Constructor required for in-code creation
    public MyView(Context context) {
         super(context);
    }
    @Override
    protected void onDraw(Canvas canvas) {
         int px = 100;
         int py = 100;
         // Create the new paint brushes
         Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
         mTextPaint.setColor(Color.WHITE);

         // Define the string.
         String displayText = “Hello World!”;

         // Measure the width of the text string.
         float textWidth = mTextPaint.measureText(displayText);

         // Draw the text string in the center of the control.
         canvas.drawText(displayText, px-textWidth/2, py, mTextPaint);
    }
}
ไปเป็น...
public class MyView extends View {
    // Constructor required for in-code creation
    public MyView(Context context) {
         super(context);
         // Create the new paint brushes
         Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }
    @Override
    protected void onDraw(Canvas canvas) {
         int px = 100;
         int py = 100;
         mTextPaint.setColor(Color.WHITE);

         // Define the string.
         String displayText = “Hello World!”;

         // Measure the width of the text string.
         float textWidth = mTextPaint.measureText(displayText);

         // Draw the text string in the center of the control.
         canvas.drawText(displayText, px-textWidth/2, py, mTextPaint);
    }
}

[+/-] Show Full Post...

วันพฤหัสบดีที่ 16 กรกฎาคม พ.ศ. 2552

Introducing the Android Menu System

เมนูมีอยู่ 3 ประเภท
  • Icon Menu เป็นเมนูที่แสดงเมื่อกดปุ่มเมนู แสดงได้สูงสุด 6 อัน

    ไม่สามารถแสดง checkbox, radio button, หรือ shortcut key ได้ จึงไม่ควรใส่พวกนั้นไว้ใน icon menu แล้วก็ถ้าใส่ item ไปมากกว่า 6 item มันจะมีปุ่ม More ขึ้นมาให้กดเพื่อให้แสดง expanded menu ถ้าจะออกจากเมนูก็กดปุ่ม back
  • Expanded Menu ขึ้นมาเมื่อกดปุ่ม More ที่ icon menu มันจะแสดงแสดง list ที่สามารถ scroll ได้ และแสดงเฉพาะ item ที่ไม่แสดงใน icon menu ซึ่งเมนูนี้จะแสดงทั้ง text เต็ม, short cut key, check box, radio button ได้ปกติ

    ไม่สามารถแสดง icon ได้ดังนั้น เวลาเขียนโปรแกรมก็อย่าใส่ icon ให้เมนูที่คิดว่ายังไงก็ต้องมาตกใน expended menu กด back เพื่อกลับไป icon menu
  • Submenu เป็นเมนูพื้นฐาน ที่เปิดด้วย mouse จะแสดงเป็น floating window

    กด back จะปิด floating window

[+/-] Show Full Post...

วันจันทร์ที่ 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);
}

[+/-] Show Full Post...

Android Layouts

Layout Manager หรือ layouts เป็นส่วนขยายของ ViewGroup class เอาไว้ควบคุมตำแหน่งของ child control บนจอภาพ สามารถ nested ได้
  • FrameLayout ง่ายที่สุด มันจะเปิด child view ขึ้นมาอ้างอิงด้วยมุมบนซ้าย และจะทับอันอื่น ๆ ไปเรื่อย ๆ
  • LinearLayout เพิ่ม child ในแนวเส้นตรง อาจจะแนวตั้งหรือแนวนอนก็ได้ สามารถกำหนด weight ให้ child ทุกตัวเพื่อกำหนดขนาดให้สัมพันธ์กันได้
  • RelativeLayout จัดวางให้สัมพันธ์กับ child อื่นหรือขอบจอ
  • TableLayout ใช้ตารางของ row และ columns
  • AbsoluteLayout วางตรงไหนก็ได้ แต่มีข้อเสียคือเวลาจอภาพเปลี่ยนแปลงไป จะไม่สามารถ dynamic ได้เท่าที่ควร
อ่านต่อได้ที่ http://code.google.com/android/devel/ui/layout.html

[+/-] Show Full Post...

The Android Widget Toolbox

Android 0ะมี toolbox ที่เป็น View มาตรฐานที่จะช่วยให้คุณสร้าง User Interface ง่าย ๆ ได้โดยใช้ control พวกนี้ และยังทำให้โปรแกรม ดูเป็นมาตรฐานเดียวกันด้วย toolbox control ที่เด่น ๆ ได้แก่
  • TextView เป็น read only text label สามารถแสดงหลายบรรทัด (Multipleline), การจัดรูปแบบ (String formatting), และ ตัดคับอัตโนมัติได้ (Word Wrapping)
  • EditText ช่องที่เอาไว้กรอกข้อความ สามารถ multipleline และ word wrapping
  • ListView เป็น View Group ของ View ที่แสดง Item ใน List ธรรมดาแล้วจะแสดง String ของ Array ค่าหนึ่งด้วย TextView สำหรับทุก ๆ ค่า
  • Spinner แสดง TextView และทำงานร่วมกันกับ ListView ซึ่งมันจะแสดง Item ที่เราเลือกขึ้นไปบน Textbox มันมาจากการรวม TextView ที่แสดง Item ปัจจุบัน และ ปุ่ม (button) ที่แสดง ListView พอกด Item ใน ListView ข้อความใน ListView ก็จะไปอยู่ที่ TextView แทน
  • Button ปุ่มไว้กด
  • CheckBox ปุ่มที่มี 2 สถานะคือ checked และ unchecked
  • RadioButton กลุ่มของปุ่ม 2 สถานะที่เลือกได้ตัวเลือกเดียว
นอกจากนี้ยังมี View ขั้นสูงอีกหลายตัว เช่น date-time pickers, auto-complete input box, maps, galleries, และ tab sheets รายละเอียกเพิ่มเติมไปดูได้ที่ http://code.google.com/android/reference/view-gallery.html

[+/-] Show Full Post...

C Convert cases of strings (tolower/toupper)

Two functions, used to convert c++ strings from upper case to lowercase and vice versa. Each takes a string argument, and returns the converted string. Please note that parameters should be validated before passing them to the functions.
string StringToUpper(string strToConvert)
{
   //change each element of the string to upper case
   for(unsigned int i=0;i<strToConvert.length();i++)
   {
      strToConvert[i] = toupper(strToConvert[i]);
   }
   return strToConvert;//return the converted string
}

string StringToLower(string strToConvert)
{
   //change each element of the string to lower case
   for(unsigned int i=0;i<strToConvert.length();i++)
   {
      strToConvert[i] = tolower(strToConvert[i]);
   }
   return strToConvert;//return the converted string
}

[+/-] Show Full Post...

Check input is number or not

#include 
// Need ctype.h for isdigit and isalnum functions.
#include 

int main(void)
{
 char var[10];    // This is the variable to store input.
 int i = 0;
 int varisnum = 0;    // Is var all numbers?  1 for yes, 0 for no.
 
 scanf("%s", var);

 while (isalnum(var[i]) != 0) {    // Loop until it a character is not alpha-numeric.
  if (isdigit(var[i]) != 0) {    // Is var[i] a numeric digit?
   varisnum = 1;
  } else {
   varisnum = 0;
   break;    // If we encounter a non-numeric character, there is no need to keep looping, so just break out.
  }
  i++;    // Move counter to the next element.
 }
 
 if (varisnum == 0)
  printf("Input was not all numbers.\n");
 else
  printf("Input was all numbers.\n");
 
 return 0;
}

[+/-] Show Full Post...

C scanf(), scanf_s()

scanf() and scanf_s() - อ่านข้อมูลจาก standard input char a, b[20]; int i; scanf("%c %s %d", &a, b, &i); scanf_s("%c %s %d", &a, b, 20, i); /* scanf_s ทำมากัน buffer overflow เวลารับ string ต้องใสขนาดด้วย*/

c

Character. When used with scanf() functions, specifies single-byte character; when used with wscanf() functions, specifies wide character. White-space characters that are ordinarily skipped are read when c is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws.

Pointer to char when used with scanf() functions, pointer to wchar_t when used with wscanf() functions.

Required. Size does not include space for a null terminator.

C

Opposite size character. When used with scanf() functions, specifies wide character; when used with wscanf functions, specifies single-byte character. White-space characters that are ordinarily skipped are read when C is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws.

Pointer to wchar_t when used with scanf functions, pointer to char when used with wscanf() functions.

Required. Size argument does not include space for a null terminator.

d

Decimal integer.

Pointer to int.

No.

i

Decimal, hexadecimal, or octal integer.

Pointer to int.

No.

o

Octal integer.

Pointer to int.

No.

u

Unsigned decimal integer.

Pointer to unsigned int.

No.

x

Hexadecimal integer.

Pointer to int.

No.

e, E, f, g, G

Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent ("e" or "E") followed by an optionally signed integer value.

Pointer to float.

No.

n

No input read from stream or buffer.

Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf() functions.

No.

s

String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf() Width Specification.

When used with scanf() functions, signifies single-byte character array; when used with wscanf() functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.

Required. Size includes space for a null terminator.

S

Opposite-size character string, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf() Width Specification.

When used with scanf() functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.

Required. Size includes space for a null terminator.

[+/-] Show Full Post...

Search