Android学习笔记04

来源:互联网 发布:chocobank网络剧 编辑:程序博客网 时间:2024/06/06 08:35

[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;@Overridepublic 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() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif (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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {Toast.makeText(RadioMain.this, "上", Toast.LENGTH_SHORT).show();}}});down.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif (isChecked){Toast.makeText(RadioMain.this, "下", Toast.LENGTH_SHORT).show();}}});left.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(isChecked){Toast.makeText(RadioMain.this, "左", Toast.LENGTH_SHORT).show();}}});right.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif (isChecked){Toast.makeText(RadioMain.this, "右", Toast.LENGTH_SHORT).show();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_radio_main, menu);return true;}}


最终结果


原创粉丝点击