xml布局:

<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"
tools:context="com.example.homework03.MainActivity" > <!-- 第一行:姓名 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="姓名"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入姓名"
android:textSize="20sp" />
</LinearLayout>
<!-- 第二行:密码 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="密码"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码"
android:inputType="textPassword"
android:textSize="20sp" />
</LinearLayout>
<!-- 第三行:性别 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="性别"
android:textSize="20sp" /> <RadioGroup
android:id="@+id/group_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" /> <RadioButton
android:id="@+id/rb_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
<!-- 第四行:年龄 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="年龄"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入年龄"
android:inputType="number"
android:textSize="20sp" />
</LinearLayout>
<!-- 第五行:email --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Email"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入Email"
android:inputType="textEmailAddress"
android:textSize="20sp" />
</LinearLayout>
<!-- 第六行:爱好 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="爱好"
android:textSize="20sp" /> <GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="3" > <CheckBox
android:id="@+id/checkbox0"
android:text="动漫" /> <CheckBox
android:id="@+id/checkbox1"
android:text="美食" /> <CheckBox
android:id="@+id/checkbox2"
android:text="约会" /> <CheckBox
android:id="@+id/checkbox3"
android:text="Dota" /> <CheckBox
android:id="@+id/checkbox4"
android:text="篮球" /> <CheckBox
android:id="@+id/checkbox5"
android:text="野炊" /> <CheckBox
android:id="@+id/checkbox6"
android:text="电影" /> <CheckBox
android:id="@+id/checkbox7"
android:text="桌游" /> <CheckBox
android:id="@+id/check_all"
android:text="全选" /> <CheckBox
android:id="@+id/check_none"
android:text="全不选" />
</GridLayout>
</LinearLayout>
<!-- 第七行:评分 --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="评分"
android:textSize="20sp" /> <RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="btn_click"
/>
</LinearLayout>

源代码:

package com.example.homework03;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText edit_name, edit_passwd, edit_age, edit_email;
private RadioGroup group_sex;
private CheckBox[] checks;
private CheckBox checkAll, checkNone;
private RatingBar ratingBar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找出对应的控件
edit_name = (EditText) findViewById(R.id.edit_name);
edit_passwd = (EditText) findViewById(R.id.edit_password);
edit_age = (EditText) findViewById(R.id.edit_age);
edit_email = (EditText) findViewById(R.id.edit_email);
group_sex = (RadioGroup) findViewById(R.id.group_sex);
checks = new CheckBox[8];
checks[0] = (CheckBox) findViewById(R.id.checkbox0);
checks[1] = (CheckBox) findViewById(R.id.checkbox1);
checks[2] = (CheckBox) findViewById(R.id.checkbox2);
checks[3] = (CheckBox) findViewById(R.id.checkbox3);
checks[4] = (CheckBox) findViewById(R.id.checkbox4);
checks[5] = (CheckBox) findViewById(R.id.checkbox5);
checks[6] = (CheckBox) findViewById(R.id.checkbox6);
checks[7] = (CheckBox) findViewById(R.id.checkbox7);
checkAll = (CheckBox) findViewById(R.id.check_all);
checkNone = (CheckBox) findViewById(R.id.check_none);
ratingBar = (RatingBar) findViewById(R.id.ratingbar);
ratingBar.setMax(5);
// 给checkbox添加onCheck事件
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) { // 当勾上了全选时,才选择全部的CheckBox
for(CheckBox check : checks) {
check.setChecked(true);
}
checkNone.setChecked(false);
}
}
});
checkNone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) { // 当勾上了全不选时,才取消选择全部的CheckBox
for(CheckBox check : checks) {
check.setChecked(false);
}
checkAll.setChecked(false);
}
}
});
} public void btn_click(View view) {
String result = "";
String name = edit_name.getText().toString();
result += "姓名:" + name +"\n";
String passwd = edit_passwd.getText().toString();
result += "密码:" + passwd +"\n";
int id = group_sex.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(id);
String sex = rb.getText().toString();
result += "性别:" + sex + "\n";
int age = Integer.parseInt(edit_age.getText().toString());
result += "年龄:" + age +"\n";
String email = edit_email.getText().toString();
result += "Email:" + email +"\n"; String favor = "";
for(CheckBox check : checks) {
if(check.isChecked()) {
favor += check.getText().toString() + " ";
}
}
result += "爱好:" + favor +"\n"; int rating = ratingBar.getProgress();
result += "评分:" + rating +"\n"; Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}

Android_Component_example的更多相关文章

随机推荐

  1. C#中的ODBC、OLEDB连接

      using System;using System.Collections.Generic;using System.Text;using System.Data.Odbc;using Syste ...

  2. hadoop2.2编程:各种API

    hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...

  3. UpdatePanel无法直接弹出窗口的解决

    UpdatePanel无法直接弹出窗口的解决 2010-06-20  来自:博客园  字体大小:[大 中 小] 摘要:本文介绍一些UpdatePanel无法直接弹出窗口的解决方法 ///<sum ...

  4. Android 应用启动渐变效果

    /** * 应用程序启动类:显示欢迎界面并跳转到主界面 * @author liux (http://my.oschina.net/liux) * @version 1.0 * @created 20 ...

  5. vs2010的一个opencv插件

    调试时,可视化mat等图像 下载及使用地址: http://visualstudiogallery.msdn.microsoft.com/657956e4-8e02-4764-8022-72a0c9c ...

  6. oracle创建临时表

    Oracle临时表可以说是提高数据库处理性能的好方法,在没有必要存储时,只存储在Oracle临时表空间中.希望本文能对大家有所帮助. 1 .前言 目前所有使用 Oracle 作为数据库支撑平台的应用, ...

  7. Esper系列(二)时间窗口、长度窗口、cast、注解、自定义函数、静态方法

    长度窗口实现原理图 说明: 上图长度窗口为5,事件W1至W5进入引擎后属于NewEvents队列,事件W6进入引擎后,W2至W6就属于NewEvents队列,而事件W1就属于OldEvents队列了. ...

  8. POJ1423 - Big Number(Stirling公式)

    题目大意 求N!有多少位 题解 用公式直接秒杀... 代码: #include<iostream> #include<cmath> using namespace std; # ...

  9. CodeForces 540D--Bad Luck Island(概率DP)

    貌似竟然是我的第一道概率DP.. 手机码代码真不舒服.... /************************************************ Memory: 67248 KB Ti ...

  10. 教程-FastReport 的安装 心得

    由于要使用报表,所以下载了FastReport 4.7.91,由于是第一次安装和使用FastReport报表,所以在安装的时候走了点弯路.把心得写一下吧. 我是第安装第二遍才完全理解安装过程,也可以定 ...