<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/button_password"
android:id="@+id/button_password"
android:background="@drawable/btn_background"
/>
在上述xml代码中具有点击Button换颜色的语句是android:background=”@drawable/btn_background”
在drawable中创建一个源文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--点击时的图片-->
<item android:drawable="@mipmap/blue29" android:state_pressed="true"></item>
<!--默认显示的图片-->
<item android:drawable="@mipmap/blue9"></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--checkedButton方法是选择默认的RadioButton-->
<!-- RadioButton中android:button="@drawable/radio_button"是重新定义了Button的图案-->
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radio"
android:checkedButton="@+id/student"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="工作"
android:layout_gravity="center_vertical"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生"
android:button="@drawable/radio_button"
android:id="@+id/student"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="教师"
android:button="@drawable/radio_button"
android:id="@+id/teacher"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/radio_button"
android:text="主任"
android:id="@+id/manger"
/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_submit"
android:text="提交数据"
android:textSize="20dp"
/>
</LinearLayout>
android:button="@drawable/radio_button"
<!--这句话中需要在drawable中写一个源文件,里面内容是:-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/a_radbtn_select" android:state_checked="true"></item>
<item android:drawable="@mipmap/a_radbtn"></item>
</selector>
输出选择的结果的程序为
public class RadioGroupTest extends Activity{
private Button mButtonSubmit;
private RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_group_test);
mRadioGroup= (RadioGroup) findViewById(R.id.radio);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb= (RadioButton) findViewById(checkedId);
Log.d("job","选择的工作是:"+rb.getText());
}
});
mButtonSubmit= (Button) findViewById(R.id.button_submit);
mButtonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int checkedId=mRadioGroup.getCheckedRadioButtonId();
RadioButton rb= (RadioButton) findViewById(checkedId);
Log.d("job","选择的工作是:"+rb.getText());
}
});
}
}