Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总...

DigitalClock和AnalogClock两个时钟类

可以为DigitalClock设置背景图片,自定义时针,秒针,分针的样式

例子:

 









运行结果:

 

Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总..._第1张图片

 

 

RadioGroup单选组RadioGroup.

OnCheckedChangeListener()监听单选按钮点击事件

注意:各个RadioButton之间的id不能重复,RadioButton的id与RadioGroup的id也不能重复.


RadioButton

 

android:button="@null" 

去除RadioButton前面的圆点


 

ToggleButton

常用属性:

android:textOff未选中时按钮的文本

android:textOn选中时按钮的文本

android:checked="true"  该状态用于设置初始状态,true表示开,false表示关,若没有设置该状态则默认为关android:background="@drawable/toggleselector"  如果要想改变ToggleButton开和关的图片,则需要设置这个属性,toggleselector将放到下面,是一个selector配置文件监听事件:CompoundButton.OnCheckedChangeListener()

重要补充:在xml中设置文字有时候间距什么的不好控制,这个时候可以把文字写在图片里面,在xml中把texton和textoff都设置为“”

 

实例:

 

package com.light.study.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
	private RadioGroup gender;
	private CheckBox swimming, reading, basketball;

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

	private void init() {
		gender = (RadioGroup) findViewById(R.id.myRadioGroup);
		swimming = (CheckBox) findViewById(R.id.swimming);
		reading = (CheckBox) findViewById(R.id.reading);
		basketball = (CheckBox) findViewById(R.id.basketball);
	}

	private void initListener() {
		gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.female) {
					Toast.makeText(MainActivity.this, "性别:女", Toast.LENGTH_LONG)
							.show();
				} else if (checkedId == R.id.male) {
					Toast.makeText(MainActivity.this, "性别:男", Toast.LENGTH_LONG)
							.show();
				}
			}
		});
		swimming.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "爱好:游泳", Toast.LENGTH_LONG)
						.show();
			}
		});
		reading.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "爱好:阅读", Toast.LENGTH_LONG)
						.show();
			}
		});
		basketball.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "爱好:篮球", Toast.LENGTH_LONG)
						.show();
			}
		});
	}

}

 




    

        
        

        

            

            
            

            
        
    

    

        

        

        

        
    


效果图:

 

 

Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总..._第2张图片

 
   

 

 

转载于:https://www.cnblogs.com/krislight1105/p/3748411.html

你可能感兴趣的:(Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总...)