Thursday, February 23, 2012 : Article User : Register | Login
บทความ
Webboard
วีดีโอ
บทความจากเพื่อนๆ Guru
17
วันนี้มาพร้อมกับสีสันและความตั้งใจที่จะทำความรู้จัก กับ android.graphics.Color  โดยมีตัวอย่างโปรแกรมมาให้ได้ดูกันค่ะ

1.เริ่มจากการสร้างโปรเจค Android โดยใช้ชื่อว่า AndroidBGColour



2.ใน file main.xml ใส่โค้ดดังต่อไป
 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/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="@string/hello"

    />

<LinearLayout

 android:id="@+id/background"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<Spinner

 android:id="@+id/colorspinner"

 android:layout_width="fill_parent"

    android:layout_height="wrap_content"

 />

</LinearLayout>

</LinearLayout>


จะได้หน้าตาแบบนี้


3. เปิด file AndroidBGColour.java และใส่โค้ดต่อไปนี้

package th.co.bighead.exsample.AndroidBGColour;

 

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.LinearLayout;

import android.widget.Spinner;

import android.widget.Toast;

 

public class AndroidBGColour extends Activity {

 

 LinearLayout background;

 Spinner spinnerColor;

 

 private static final String[] color =

  { "BLACK", 

  "BLUE",

  "CYAN",

  "DKGRAY", 

  "GRAY",

  "GREEN", 

  "LTGRAY", 

  "MAGENTA", 

  "RED",

  "TRANSPARENT", 

  "WHITE",

  "YELLOW"};

  private ArrayAdapter<String> adapter;

 

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        background = (LinearLayout)findViewById(R.id.background);

       

        spinnerColor = (Spinner)findViewById(R.id.colorspinner);

        adapter = new ArrayAdapter<String>(this,

          android.R.layout.simple_spinner_item, color);

        adapter.setDropDownViewResource(

          android.R.layout.simple_spinner_dropdown_item);

        spinnerColor.setAdapter(adapter);

        spinnerColor.setSelection(0);

       

        spinnerColor.setOnItemSelectedListener(

          new Spinner.OnItemSelectedListener(){

 

   @Override

   public void onItemSelected(AdapterView<?> arg0, View arg1,

     int arg2, long arg3) {

    // TODO Auto-generated method stub

    setBackgroundColor(spinnerColor.getSelectedItem().toString());

   }

 

   @Override

   public void onNothingSelected(AdapterView<?> arg0) {

    // TODO Auto-generated method stub

   

   }});

    }

   

    private void setBackgroundColor(String strColor){

     Toast.makeText(this, strColor, Toast.LENGTH_LONG).show();

     if (strColor == "BLACK"){

      background.setBackgroundColor(Color.BLACK);

     }else if(strColor=="BLUE"){

      background.setBackgroundColor(Color.BLUE);

     }else if(strColor=="CYAN"){

      background.setBackgroundColor(Color.CYAN);

     }else if(strColor=="DKGRAY"){

      background.setBackgroundColor(Color.DKGRAY);

     }else if(strColor=="GRAY"){

      background.setBackgroundColor(Color.GRAY);

     }else if(strColor=="GREEN"){

      background.setBackgroundColor(Color.GREEN);

     }else if(strColor=="LTGRAY"){

      background.setBackgroundColor(Color.LTGRAY);

     }else if(strColor=="MAGENTA"){

      background.setBackgroundColor(Color.MAGENTA);

     }else if(strColor=="RED"){

      background.setBackgroundColor(Color.RED);

     }else if(strColor=="TRANSPARENT"){

      background.setBackgroundColor(Color.TRANSPARENT);

     }else if(strColor=="WHITE"){

      background.setBackgroundColor(Color.WHITE);

     }else if(strColor=="YELLOW"){

      background.setBackgroundColor(Color.YELLOW);

     }

    }

}


4.รันโปรแกรม




ลิงค์ที่เกี่ยวข้อง : Color class

Post Rating

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.
Home | Article | Webboard | Video | Blog | Showcase | News
Copyright 2010 by devguru.mobi