android常用組件用法之RadioGroup
RadioGroup組件內部含有若干個RadioButton組件,每一個RadioButton對應一個選項,利用RadioGroup類似于做單選題。
RadioGroup可以理解為存放RadioButton的容器,他將多個RadioButton組織起來,形成一個組,而用戶在選擇時只能是組內的某一個RadioButton,所以用戶直接操作的對象是RadioButton組件。
以下實例是首先選擇一個選項,按提交按鈕,會彈出一個消息框提示選擇信息。
首先是布局文件:
<LinearLayout xmlns:android="<TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv1" />
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" >
<RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/rb1" />
<RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb2" />
<RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb3" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb4"/> <RadioButton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb5"/> </RadioGroup>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn" />
</LinearLayout></pre>
其次是strings.xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources><string name="app_name">Test_RadioGroup</string> <string name="action_settings">Settings</string> <string name="tv1">請選出您最喜歡的一道菜:</string> <string name="rb1">鐵鍋蛋</string> <string name="rb2">蜜汁兩樣</string> <string name="rb3">瓷壇羊肉</string> <string name="rb4">肉絲帶底</string> <string name="rb5">活魚活吃</string> <string name="btn">提交</string>
</resources></pre>
再次是android源文件:
package main.test_radiogroup;import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.Toast; import android.os.Bundle;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private RadioButton rbtn1=null,rbtn2=null,rbtn3=null,rbtn4=null,rbtn5=null; private Button btn1=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rbtn1=(RadioButton)findViewById(R.id.radio0); rbtn2=(RadioButton)findViewById(R.id.radio1); rbtn3=(RadioButton)findViewById(R.id.radio2); rbtn4=(RadioButton)findViewById(R.id.radio3); rbtn5=(RadioButton)findViewById(R.id.radio4); btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(MainActivity.this); }
@Override public void onClick(View arg0) { String str=""; if(rbtn1.isChecked()) str=rbtn1.getText().toString(); else if(rbtn2.isChecked()) str=rbtn2.getText().toString(); else if(rbtn3.isChecked()) str=rbtn3.getText().toString(); else if(rbtn4.isChecked()) str=rbtn4.getText().toString(); else if(rbtn5.isChecked()) str=rbtn5.getText().toString(); else { } Toast.makeText(MainActivity.this,"您選擇的是"+str,Toast.LENGTH_LONG).show();//彈出選擇消息框 } }</pre>
最后是測試結果:
來自:http://my.oschina.net/u/2243176/blog/358369
