day04:RadioButton单选按钮
string.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">RadioButton</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="amount">充值金额:</string> </resources>
radio_button_demo.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="44dp" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="50" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="200" /> </RadioGroup> <TextView android:id="@+id/tvAmount" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/radioGroup1" android:text="@string/amount" android:textSize="20sp" /> </RelativeLayout>
RadioButtonDemo.java:
package com.lvshitech.radiobutton; import android.app.Activity; import android.os.Bundle; import android.widget.RadioGroup; import android.widget.TextView; public class RadioButtonDemo extends Activity { TextView tvAmount; RadioGroup rg; String temp = "充值金额:"; String amount50 = "50 元"; String amount100 = "100 元"; String amount200 = "200 元"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio_button_demo); tvAmount = (TextView) findViewById(R.id.tvAmount); tvAmount.setText(temp + amount50); rg = (RadioGroup) findViewById(R.id.radioGroup1); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { String result = ""; switch(checkedId) { case R.id.radio0: result = amount50; break; case R.id.radio1: result = amount100; break; case R.id.radio2: result = amount200; break; } tvAmount.setText(temp + result); } }); } }
==================================== 下面是通过按钮来触发 ====================================
string.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">RadioButton</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="amount">充值金额:</string> <string name="btnSubmit">提交</string> </resources>
RadioButtonDemo.java:
package com.lvshitech.radiobutton; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.TextView; public class RadioButtonDemo extends Activity { TextView tvAmount; RadioGroup rg; Button btnSub; String temp = "充值金额:"; String amount50 = "50 元"; String amount100 = "100 元"; String amount200 = "200 元"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio_button_demo); tvAmount = (TextView) findViewById(R.id.tvAmount); tvAmount.setText(temp + amount50); btnSub = (Button) findViewById(R.id.btnSub); rg = (RadioGroup) findViewById(R.id.radioGroup1); btnSub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String result = ""; int isCheckedId = rg.getCheckedRadioButtonId(); // 获取被选中的单选按钮的Id switch(isCheckedId) { case R.id.radio0: result = amount50; break; case R.id.radio1: result = amount100; break; case R.id.radio2: result = amount200; break; } tvAmount.setText(temp + result); } }); /*rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { String result = ""; switch(checkedId) { case R.id.radio0: result = amount50; break; case R.id.radio1: result = amount100; break; case R.id.radio2: result = amount200; break; } tvAmount.setText(temp + result); } });*/ } }