android 单选按钮组的使用

RadioGroup是Android组单选按钮控件。更具体地,使用提供的RadioGroup从组中只选择一个RadioButton的能力。当用户选择一个单选按钮前一个被选中,自动成为泛滥

在我们的例子中,我们将向你展示RadioGroupAndroid应用程序的使用

activity_main.xml:



    
    
    
        
        
        
        
        
        
        
    
    
    

的RadioGroup的基本属性是Android:checkedbutton,指定单选按钮应该是默认被选中。其他成分是从类继承。您可以从上面的代码,注意单选按钮的设置是由一个RadioGroup体现所以其组成每个配置影响单选按钮

strings.xml




    RadioGroupExample
    Settings
    Choose one of the radio buttons below
    Sound
    Vibration
    Silent
    Choose


MainActivity.java:

package com.javacodegeeks.android.radiogroupexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private RadioGroup radioGroup;
	private RadioButton sound, vibration, silent; 
	private Button button;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
		
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// find which radio button is selected
				if(checkedId == R.id.silent) {
					Toast.makeText(getApplicationContext(), "choice: Silent", 
							Toast.LENGTH_SHORT).show();
				} else if(checkedId == R.id.sound) {
					Toast.makeText(getApplicationContext(), "choice: Sound", 
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), "choice: Vibration", 
							Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
		sound = (RadioButton) findViewById(R.id.sound);
		vibration = (RadioButton) findViewById(R.id.vibration);
	    silent = (RadioButton) findViewById(R.id.silent);
	    textView = (TextView) findViewById(R.id.text);
	    
	    button = (Button)findViewById(R.id.chooseBtn);
	    button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int selectedId = radioGroup.getCheckedRadioButtonId();
				
				// find which radioButton is checked by id
				if(selectedId == sound.getId()) {
					textView.setText("You chose 'Sound' option");
				} else if(selectedId == vibration.getId()) {
					textView.setText("You chose 'Vibration' option");
				} else {
					textView.setText("You chose 'Silent' option");
				}	
			}
	    });
	}

}

现在,让我们来看看上面的代码。当选中单选按钮在它的组改变时, OnCheckedChangeListener是为了处理这种情况调用。此接口的onCheckedChanged ( )方法,包括选择,导致回调的调用单选按钮的唯一ID 。

在这个例子中,我们会告诉你选择的选择信息(例如,当按下按钮)的另一种方式。这可以通过getCheckedRadioButtonId (),它是RadioGroup中类的公共函数来完成。这个方法返回从小组选择的单选按钮的唯一ID 。你可以看看代码,看看你能如何处理这两种情况。

当然, Android系统给我们提供了改变和处理的应用程序视图的属性更动态的方式。作为先决条件,是每一个映射视图中使用XML的唯一ID的组成部分。这可以通过findViewById ()方法来进行。

最效果图:


相关关键词:  
大神网
android百度网盘教程




你可能感兴趣的:(技术类,android教程)