Android学习笔记04

[Android开发视频教学].01_12_Android常见控件(二)

主要学习

RadioGroup

RadioButton

CheckBox

Toast的用法


布局文件 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/gender"
        tools:context=".RadioMain" 
        android:layout_margin="10dp"/>

    <RadioGroup
        android:id="@+id/gendergroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/male"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/male" />

        <RadioButton
            android:id="@+id/female"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/female" />
    </RadioGroup>

    <CheckBox
        android:id="@+id/up"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/up" />

    <CheckBox
        android:id="@+id/down"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/down" />

    <CheckBox
        android:id="@+id/left"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/left" />

    <CheckBox
        android:id="@+id/right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/right" />

</LinearLayout>


国际化文件

<resources>

    <string name="app_name">Android07</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_radio_main">RadioMain</string>
	
	<string name="gender">性别</string>
	<string name="male">男</string>
	<string name="female">女</string>
	
	<string name="up">上</string>
	<string name="down">下</string>
	<string name="left">左</string>
	<string name="right">右</string>
	
</resources>



Java文件 

package fegnda.android07;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioMain extends Activity {

	private RadioGroup genderGroup = null;
	private RadioButton male = null;
	private RadioButton female = null;

	private CheckBox up = null;
	private CheckBox down = null;
	private CheckBox left = null;
	private CheckBox right = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_radio_main);

		genderGroup = (RadioGroup) findViewById(R.id.gendergroup);
		male = (RadioButton) findViewById(R.id.male);
		female = (RadioButton) findViewById(R.id.female);

		genderGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if (male.getId() == checkedId) {
							Toast.makeText(RadioMain.this, "男",
									Toast.LENGTH_SHORT).show();
						}
						if (female.getId() == checkedId) {
							Toast.makeText(RadioMain.this, "女",
									Toast.LENGTH_SHORT).show();
						}
					}
				});

		up = (CheckBox) findViewById(R.id.up);
		down = (CheckBox) findViewById(R.id.down);
		left = (CheckBox) findViewById(R.id.left);
		right = (CheckBox) findViewById(R.id.right);

		up.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					Toast.makeText(RadioMain.this, "上", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

		down.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked){
					Toast.makeText(RadioMain.this, "下", Toast.LENGTH_SHORT).show();
				}
			}

		});
		
		left.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					Toast.makeText(RadioMain.this, "左", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
		right.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked){
					Toast.makeText(RadioMain.this, "右", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_radio_main, menu);
		return true;
	}
}


最终结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值