标签:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.pingyijinren.helloworld.MainActivity"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/group"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="音乐" android:id="@+id/music"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电视剧" android:id="@+id/movie"/> </RadioGroup> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" android:id="@+id/button"/> </LinearLayout>
package com.pingyijinren.helloworld; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private RadioGroup group; private RadioButton music; private RadioButton movie; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); group=(RadioGroup)findViewById(R.id.group); music=(RadioButton)findViewById(R.id.music); movie=(RadioButton)findViewById(R.id.movie); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(music.isChecked()){ Toast.makeText(MainActivity.this,"音乐",Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(MainActivity.this,"电影",Toast.LENGTH_SHORT).show(); } } }); button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { group.clearCheck(); } }); } }
标签:
原文地址:http://www.cnblogs.com/zqxLonely/p/5550664.html