Android学习笔记之RadioButton的使用详解


(1)选用radioGroup的图标



    

    

        

        

        
    


(2)控制的类是

package com.lc.radiobutton;

import com.example.radiobutton.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	/*
	 * 设置radio的点击事件,当点击的时候显示文字
	 */
	public void onRadioButtonClicked(View view) {
		RadioButton button = (RadioButton) view;
		boolean isChecked = button.isChecked();
		switch (view.getId()) {
		case R.id.radio0:
			if (isChecked) {
				Toast.makeText(MainActivity.this, button.getText(), 1).show();
			}
			break;
		case R.id.radio1:
			if (isChecked) {
				Toast.makeText(MainActivity.this, button.getText(), 1).show();
			}
			break;
		case R.id.radio2:
			if (isChecked) {
				Toast.makeText(MainActivity.this, button.getText(), 1).show();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

(3)显示结果,当点击的时候显示文字

Android学习笔记之RadioButton的使用详解_第1张图片

你可能感兴趣的:(Android)