Button的使用(九):RadioGroup的OnCheckedChange事件

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.LinearLayout;
import android.view.Gravity;
import android.util.Log;


public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
	
	RadioGroup orientation;
	RadioGroup gravity;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		orientation = (RadioGroup)findViewById(R.id.orientation);
		orientation.setOnCheckedChangeListener(this);
		gravity = (RadioGroup)findViewById(R.id.gravity);
		gravity.setOnCheckedChangeListener(this);
	}
	
	public void onCheckedChanged(RadioGroup group, int checkedId)
	{
		switch (checkedId) {
		case R.id.horizontal:
			orientation.setOrientation(LinearLayout.HORIZONTAL);
			break;
		case R.id.vertical:
			orientation.setOrientation(LinearLayout.VERTICAL);
			break;
		case R.id.left:
			gravity.setGravity(Gravity.LEFT);
			break;
		case R.id.center:
			gravity.setGravity(Gravity.CENTER);
			break;
		case R.id.right:
			gravity.setGravity(Gravity.RIGHT);
			break;
		}
	}
	
}

xml:



  
    
    
  
  
    
    
    
  


你可能感兴趣的:(android)