วันเสาร์ที่ 10 มีนาคม พ.ศ. 2555

Android Radio Button

ลองมาทำ RadioButton กันครับ
ดูว่ามันใช้งานอย่างไร รู้ได้อย่างไรว่าเลือกปุ่มไหน
OK......มาสร้าง Form กันก่อนครับ
ผมใช้ RadioGroup นการใช้งาน ซึ่ง มันก็เหมือน radioทั่วไป เพียงแต่เพิ่ม
ให้มันอยู่กันเป็นกลุ่มๆ  และเลือกได้เพียง ปุ่มเดียว ซึ่ง
Radio Button จะอยู่ภายใต้ linearLayout1 ตามภาพด้านล่างครับ







main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/radio_button_group" android:layout_gravity="center" android:textSize="20dp" android:gravity="center" android:textStyle="bold" android:background="#007D47"/>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_margin="6dp">

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_weight="1">

            <RadioButton
                android:id="@+id/radioRed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="false"
                android:text="@string/txtRed" android:textColor="@color/red"/>

            <RadioButton
                android:id="@+id/radioGreen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/txtGreen" android:textColor="@color/green"/>

            <RadioButton
                android:id="@+id/radioBlue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/txtBlue" android:textColor="@color/blue"/>

        </RadioGroup>

        <TextView
            android:id="@+id/textViewColorSelected"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="center" android:textSize="30dp" android:layout_weight="1"/>

    </LinearLayout>

    <Button
        android:id="@+id/buttonSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonSelect" android:layout_gravity="center"/>

</LinearLayout>



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

    <string name="hello">Hello World, RadioButtonActivity!</string>
    <string name="app_name">RadioButton</string>
    <color name="red">#FF0000</color>
    <color name="green">#00FF00</color>
    <color name="blue">#0000FF</color>
    <string name="txtRed">RED</string>
    <string name="txtGreen">Green</string>
    <string name="txtBlue">Blue</string>
    <color name="yellow">#FFFF00</color>
    <string name="buttonSelect">Reset</string>
    <string name="radio_button_group">Radio Button Group</string>
    <color name="black">#FFFFFF</color>
    <color name="white">#000000</color>

</resources>



RadioButtonActivity.java

package com.exsample.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class RadioButtonActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    private Button buttonSelected;
    private RadioButton radioRed, radioBlue, radioGreen;
    private TextView textviewSelected;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // ทำการ link id ของ ปุ่มต่างๆ ใน form เข้ากับ ตัวแปร
        // ระหว่าง main.xls กับ ตัวแปรเรา ซึ่งจะไปlinkกันไว้ที่ R.java
        buttonSelected = (Button)findViewById(R.id.buttonSelect);
        radioRed = (RadioButton)findViewById(R.id.radioRed);
        radioGreen = (RadioButton)findViewById(R.id.radioGreen);
        radioBlue = (RadioButton)findViewById(R.id.radioBlue);
        textviewSelected = (TextView)findViewById(R.id.textViewColorSelected);
       
        //ตั้งค่า text เริ่มต้น
            textviewSelected.setText(" ลองกด ปุ่ม  \n radio button \n ดูครับ");
       
       
       
        //กำหนดให้ปุ่ม ทำงานเมื่อมีเหตการณ์ การกด เกิดขึ้น
        buttonSelected.setOnClickListener(this);
        radioRed.setOnClickListener(this);
        radioGreen.setOnClickListener(this);
        radioBlue.setOnClickListener(this);
       
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
       
        switch(arg0.getId()){ // หาค่าปุ่มที่กดมา เป็น ID อะไร
        case R.id.radioRed:// เมื่อกดปุ่ม RED ให้ทำคำสั่งด้านล่าง
            textviewSelected.setBackgroundResource(R.color.red); //เปลี่ยนสีพื้นหลังของ textviewSelectedColor เป็นสี แดง
            textviewSelected.setText(R.string.txtRed); // เปลี่ยนข้อความ

            break;
        case R.id.radioGreen: // เมื่อกดปุ่ม Green ให้ทำคำสั่งด้านล่าง
            textviewSelected.setBackgroundResource(R.color.green);//เปลี่ยนสีพื้นหลังของ textviewSelectedColor เป็นสีเขียว
            textviewSelected.setText(R.string.txtGreen); // เปลี่ยนข้อความ

            break;
        case R.id.radioBlue: // เมื่อกดปุ่ม Blue ให้ทำคำสั่งด้านล่าง
            textviewSelected.setBackgroundResource(R.color.blue);//เปลี่ยนสีพื้นหลังของ textviewSelectedColor เป็นสีน้ำเงิน
            textviewSelected.setText(R.string.txtBlue);// เปลี่ยนข้อความ

            break;
        case R.id.buttonSelect:
            //textviewSelected.getBackground(); // get background color value
            //textviewSelected.setText(String.valueOf(textviewSelected.getBackground())); // value is null the null is 0
            textviewSelected.setBackgroundColor(0); //Reset background color
            //radioRed.isChecked();
            radioRed.setChecked(false);
            radioGreen.setChecked(false);
            radioBlue.setChecked(false);
            textviewSelected.setText("Reset");
           
            break;
       
        }//end select case
    }//end onClick
}//end RadioButtonActivity


ทดลองเล่น เปลี่ยนค่าต่างๆดูครับ ว่าเกิดอะไรขึ้นบ้าง

ไม่มีความคิดเห็น:

แสดงความคิดเห็น