總網頁瀏覽量

2012年1月29日 星期日

Android筆記

Google! Android 3(gasolin著)中的一些筆記

android:orientation 版面走向
fill_parent 填滿整個上層元件
wrap_content 包住內容, 隨文字欄位行數的不同而改變介面元件的高度

<EditText android:id="@+id/height" />
@ 提示XML解析器應該把後面的字串解析成識別符號
+  代表新建一個識別符號
id/  識別符號會被歸類在id類別下

string.xml
<string name="識別符號">文字敘述</string>
<string name="bmi_height">身高(cm)</string>

main.xml
<TextView android:text ="@string/bmi_height" />

重構模式:
好處為方便修改程式與整理,
 findViews();
 setListensers();

在android上設計對話框:
 private void openOptionDialog(){
    AlertDialog.Builder dialog = new AlertDialog.Builder(Main.this);
    dialog.setTitle("");
    dialog.setMessage("");
    dialog.show();

解決產生實體時所造成的記憶體消耗, 使用匿名方法, 並加入確認按鍵

    private void openOptionDialog(){
    new AlertDialog.Builder(Main.this)
    .setTitle(R.string.about_title)
    .setMessage(R.string.about_msg)
    .setPositiveButton("確認", new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialoginterface, int i){
    }
    })
    .show();
    }

使用Toast函式對話框:
    private void openOptionDialog(){
    /*Toast popup = Toast.makeText(Main.this, "BMI 計算器", Toast.LENGTH_SHORT);
    popup.show();*/
    //匿名呼叫
    Toast.makeText(Main.this, "BMI 計算器", Toast.LENGTH_SHORT).show();
    }










2012年1月27日 星期五

C指標

指標 int *ptr;   陣列 int array[10];

陣列指定給指標:
ptr = array;  或者是  ptr = &array[0];
寫array跟&array[0]是相同的
===> a[i]就可寫為 *(a + i)

若指標ptr為指向陣列的第一個元素array[0]
則 *(ptr + 1)表示指向array[1], 第二個元素 ===> *(ptr+i) 指向 array[i]

char *array[] 指標陣列 ===> *array[i]指向文字列的第一個字元
char (*array)[] 指標指向陣列

int *f(); f為函數, 傳回值為指向整數的指標   ----------    函式返回指標
int (*f)(); f是個指標, 指向一個會傳回整數的函數   ---------   函式指標

const char i=5; 不可改值寫法
int* const: 指向const的指標 可以改值, 不可以改位址
const int* const:指向constconst指標


typedef int (*fp)(char a);
fp = fun;   //fun為一個指向函數的函數指標(參數為char值, 返回值為int)
int x = fun('x');

char (*(*x( )) [] )( ):函數, 傳回一指標,該指標指向指標陣列, 陣列中各元素指向一個會傳回char值的函數

char (*(*x[3]) ( )) [5]: 指標陣列, 有3個元素, 各指向一個會傳回另一指標的函數, 該指標是指向含有5個元素的字元陣列


錯誤: int *ptr;
        *ptr = 35;

正確: int *ptr, number = 10;
        ptr = &number;

ptri 位址為 0012ff84
ptri +1 位址改為  0012ff88
取得鄰近位址內的值 *(ptr+1)



記憶體配置:
配置一個浮點數指標
float *fp;
fp = (float *)malloc(sizeof(float));
free(fp);


int *num;
num = (int *)malloc(sizeof(int) * 10)
free(num)

int array[10][20]

int **array= (int **)malloc(sizeof(int*)*10);
for(i=0;i<10;i++)
   *(array+i) = (int *)malloc(sizeof(int) * 20);


------------------------------------------------------------------------------------

struct grade
{
    int math;
    int english;
    int computer;
};
struct grade *student;
student = (struct grade *) malloc(num * sizeof(struct grade));

------------------------------------------------------------------------------------

鏈結串列:
struct list                    
{
   int num;                    
   char name[10];            
   char address[50];    
   struct list *next;
};
typedef struct list node;
typedef node *link;

ptr = ( link ) malloc(sizeof(node));
ptr->next = NULL;

------------------------------------------------------------------------------------