Android学习笔记04

举报
wh_bn 发表于 2021/12/15 23:11:58 2021/12/15
【摘要】 [Android开发视频教学].01_12_Android常见控件(二) 主要学习 RadioGroup RadioButton CheckBox Toast的用法 布局文件  <LinearLayout xmlns:android="http://schemas.android.com/apk/r...

[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;
	}
}


最终结果


文章来源: blog.csdn.net,作者:fengda2870,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/fengda2870/article/details/8155412

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。