Android_Component_example
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的更多相关文章
随机推荐
- 同一客户代码下一个CPN对应多个FG会出现CPN描述一样的问题
Q&A : D2E01客户下已经存在CPN: TP1062/A 对应FG: P5M2812512AFHHF0 直接在客户代码下无法新建,只能在交叉信息新建CPN: TP1062/A, 但无法修 ...
- 如何配置Java环境
下载JDK并安装 搜索JDK,官网立马就出来了,下载之后个人觉得毕竟开发,毕竟这东西不大,C盘稳一点,安装在C盘可以的 配置 右键打开计算机->属性->高级系统设置->高级-> ...
- hadoop2.2编程:各种API
hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...
- c语言_文件操作_FILE结构体解释_涉及对操作系统文件FCB操作的解释_
1. 文件和流的关系 C将每个文件简单地作为顺序字节流(如下图).每个文件用文件结束符结束,或者在特定字节数的地方结束,这个特定的字节数可以存储在系统维护的管理数据结构中.当打开文件时,就建立了和文件 ...
- js制作圆角按钮(兼容谷歌,ie7,ie8)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- diamond专题(三)—— diamond架构
大家好,这次为大家带来的是diamond的架构,架构如下图所示: 对该图进行一些说明: 1.作为一个配置中心,diamond的功能分为发布和订阅两部分.因为diamond存放的是持久数据,这些数据的变 ...
- [BILL WEI]一些经常用到的SQL函数
截取时间 --convert可以截取特点值 convert(varchar(10),getdate(),120) 截取2012-11-11 11:11:11 前10位,得到日期2012-11-11
- [liu yanling]软件测试用例的基本要素包括哪些?
用例编号: 测试用例的编号有一定的规则,比如系统测试用例的编号这样定义规则: PROJECT1-ST-001 ,命名规则是项目名称+测试阶段类型(系统测试阶段)+编号.定义测试用例编号,便于查找测试用 ...
- 【原】Spark中Master源码分析(二)
继续上一篇的内容.上一篇的内容为: Spark中Master源码分析(一) http://www.cnblogs.com/yourarebest/p/5312965.html 4.receive方法, ...
- Solution for When browse http://xxx/ReportServer Show Error (rsAccessDenied)
Issue: Reporting Services Error The permissions granted to user 'IDEAAM\William' are insufficient fo ...