You are on page 1of 4

RadioButton

Project : RadioButtonDemo

สําหรับโปรเจ็กนี้ จะแนะนําให้รู้จักกับ RadioButton โดยที่เราจะรวม RadioButton


เอาไว้ใน RadioGroup

<?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"
android:gravity = "center_horizontal" >
<RadioGroup
android:id = "@+id/orientation"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginTop = "50dip"
android:orientation = "horizontal" >

<RadioButton
android:id = "@+id/horizontal"
android:text = "Horizontal" >
</RadioButton>

<RadioButton
android:id = "@+id/vertical"
android:text = "Vertical" >
</RadioButton>

</RadioGroup>
</LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity = "center_horizontal" >

Code XML ในการจัด Layout ของ LinearLayout


orientation : อะไรก็ตามที่ใส่เข้าไปใน linear จะเรียงต่อกันตามแนวดิ่ง
width : กว้าง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
height : สูง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
gravity : กําหนดให้อะไรก็ตามที่ใส่เข้าไปในนี้ จะอยู่ตรงกลางในแนวนอน
<RadioGroup
android:id = "@+id/orientation"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginTop = "50dip"
android:orientation = "horizontal" >

Code XML ในการจัด Layout ของ RadioGroup


id : มีชื่อตัวแปรว่า orientation
width : กว้าง เท่ากับขนาดของข้อมูลที่อยู่ข้างใน ซึ่ง ในที่นี้คือ RadioButton จํานวน 2 อัน
height : สูง เท่ากับขนาดของข้อมูลที่อยู่ข้างใน ซึ่ง ในที่นี้คือ RadioButton จํานวน 2 อัน
marginTop : เป็นดารดัน RadioGroup ลงมาจากทางด้านบน 50 dip
orientation : เป็นการกําหนดการต่อกันของสิ่งที่อยู่ภายใน ซึ้งในที่นี้คือ แนวนอน

<RadioButton
android:id = "@+id/horizontal"
android:text = "Horizontal" >
</RadioButton>

Code XML ในการจัด Layout ของ RadioButton


id : มีชื่อตัวแปรว่า horizontal
text : เป็นการกําหนดตัวอักษรให้กับ RadioButton ในที่นี้คือ Horizontal

<RadioButton
android:id = "@+id/vertical"
android:text = "Vertical" >
</RadioButton>

Code XML ในการจัด Layout ของ RadioButton


id : มีชื่อตัวแปรว่า vertical
text : เป็นการกําหนดตัวอักษรให้กับ RadioButton ในที่นี้คือ Vertical
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

public class RadioButtonDemo extends Activity {



RadioGroup orientation;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

orientation = (RadioGroup)findViewById(R.id.orientation);
orientation.setOnCheckedChangeListener( new
RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if( checkedId == R.id.horizontal ){
orientation.setOrientation(LinearLayout.HORIZONTAL);
}
else if( checkedId == R.id.vertical ){
orientation.setOrientation(LinearLayout.VERTICAL);
}
}
});
}
}

RadioGroup orientation;

Code Java ในการประกาศตัวแปร RadioGroup

orientation = (RadioGroup)findViewById(R.id.orientation);

Code Java ในการการเชื่อม ตัวแปร RadioGroup กับ Layout ( XML )


จากนั้นเราก็มากําหนดเหตุการณ์เมื่อมีการ คลิก ที่ RadioButton
ด้วยฟังก์ชัน setOnCheckChangeListener
โดยที่ เมื่อ คลิก RadioButton ที่ชื่อว่า Horizontal แล้ว LayoutOrientation จะจัดในแนว นอน
RadioButton ที่ชื่อว่า Vertical แล้ว LayoutOrientation จะจัดในแนว ดิ่ง จะได้ว่า

orientation.setOnCheckedChangeListener( new
RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if( checkedId == R.id.horizontal ){
orientation.setOrientation(LinearLayout.HORIZONTAL);
}
else if( checkedId == R.id.vertical ){
orientation.setOrientation(LinearLayout.VERTICAL);
}
}
});

drbomkung@gmail.com

You might also like