2011年5月24日 星期二

ScrollView物件

ScrollView物件當資料訊息過大,導致物件僅顯示部分資料時
可以使用ScrollView物件,將所有的訊息顯示出來

<ScrollView
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    >
    <TextView 
        android:id="@+id/msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""    />
</ScrollView>

RelativeLayout版面配置

RelativeLayout版面配置相對佈局版面配置,可指定一物件位於另一物件的相對位置(上下左右)
Default設定,物件位於左上角

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    >
    <TextView
        android:id="@+id/key_msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15pt"
        android:text="Please press all keys."  />
    <TextView
        android:id="@+id/key_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/key_msg"
        android:text="Menu"  />
    <TextView
        android:id="@+id/key_back"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/key_menu"
        android:text="Back"  />
    <TextView
        android:id="@+id/key_vol_add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/key_back"
        android:text="Volume +"  />
    <TextView
        android:id="@+id/key_vol_dec"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/key_vol_add"
        android:text="Volume -"  />
    <Button
        android:id="@+id/pass"
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"        android:text="PASS"  />
    <Button
        android:id="@+id/fail"
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="FAIL"  />
</RelativeLayout>

Rating Bar物件

Rating Bar物件    <RatingBar
        android:id="@+id/ratingbar"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:numStars="5"
       
android:stepSize="1.0"/>


    RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
    ratingbar
.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
       
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
           
Toast.makeText(HelloFormStuff.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
       
}
    });

Toggle Button物件

Toggle Button物件通常用於開/關選項設定
    <ToggleButton android:id="@+id/togglebutton"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textOn="Vibrate on"
       
android:textOff="Vibrate off"/>


    ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
    togglebutton
.setOnClickListener(new OnClickListener() {
       
public void onClick(View v) {
           
// Perform action on clicks
           
if (togglebutton.isChecked()) {
               
Toast.makeText(HelloFormStuff.this, "Checked", Toast.LENGTH_SHORT).show();
           
} else {
               
Toast.makeText(HelloFormStuff.this, "Not checked", Toast.LENGTH_SHORT).show();
           
}
       
}
    });

Radio Button物件

Radio Button物件通常用於"單選"項目
mian.xml
    <RadioGroup
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:orientation="vertical">
       
<RadioButton

            android:id="@+id/radio_red"
           
android:layout_width="wrap_content"
            
android:layout_height="wrap_content"
           
android:text="Red" />
       
<RadioButton

            android:id="@+id/radio_blue"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="Blue" />
   
</RadioGroup>


Xxx.java
    public void onCreate(Bundle savedInstanceState) {
        .......
        RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
        
RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
        radio_red
.setOnClickListener(radio_listener);
        radio_blue
.setOnClickListener(radio_listener);

    }

    private OnClickListener radio_listener = new OnClickListener() {
       
public void onClick(View v) {
           
// Perform action on clicks
           
RadioButton rb = (RadioButton) v;
            // 顯示選擇項目

            Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
       
}
    };

-------------------------------------------------------------------------------------
    private RadioGroup.OnCheckedChangeListener change =
        new RadioGroup.OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == mRadio1.getId()) {
                Toast.makeText(radio.this, "1", Toast.LENGTH_SHORT).show();
            }
            if (checkedId == mRadio2.getId()) {
                Toast.makeText(radio.this, "2", Toast.LENGTH_SHORT).show();
            }
        }
    };

CheckBox物件

CheckBox物件
通常用於設定開/關選項
    checkbox.setOnClickListener(new OnClickListener() {
       
public void onClick(View v) {
           
// Perform action on clicks, depending on whether it's now checked
           
if (((CheckBox) v).isChecked()) {
               
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
           
} else {
               
Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
           
}
       
}
    });

2011年5月23日 星期一

Gallery物件

Gallery物件畫廊,大部分用於顯示圖片
Gallery.java
    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // function
        }
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;
        private Integer[] mImageIds = {
            R.drawable.sample01,        R.drawable.sample02,        R.drawable.sample03,
            R.drawable.sample04,        R.drawable.sample05,        R.drawable.sample06,
            R.drawable.sample07,        R.drawable.sample08,        R.drawable.sample09     };
   
        // 建構子,當new新物件時會進到這裡做初始化的動作
        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
            a.recycle();
        }
   
        public int getCount() {    return mImageIds.length;    }
        public Object getItem(int position) {    return position;    }
        public long getItemId(int position) {    return position;    }
   
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);
            i.setImageResource(mImageIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(320, 480));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);
            return i;
        }
    }

main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
values的資料夾下新增HelloGallery的style
attrs.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="HelloGallery">
            <attr name="android:galleryItemBackground" />
        </declare-styleable>
    </resources>