Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android---18---RadioButton單選按鈕

Android---18---RadioButton單選按鈕

編輯:關於Android編程

MainActivity.java:

 

 

 

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
 * RadioButton
 * @author Caesar
 *
 */
public class MainActivity extends Activity implements OnClickListener{

	private Button button;
	private RadioGroup group;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		group = (RadioGroup) this.findViewById(R.id.sex);
		button = (Button) this.findViewById(R.id.button);
		button.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//獲得單選按鈕組的選項個數
		int len = group.getChildCount();
		String msgsString= ;
		for (int i = 0; i < len;i++){
			RadioButton radioButton = (RadioButton) group.getChildAt(i);
			if (radioButton.isChecked()){
				msgsString = radioButton.getText().toString();
				break;
			}
		}
		Toast.makeText(MainActivity.this, msgsString, 1).show();
	}
}


 


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved