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的更多相关文章
随机推荐
- 【转】was mutated while being enumerated 你是不是以为你真的懂For...in... ??
原文网址:http://www.jianshu.com/p/ad80d9443a92 支持原创,如需转载, 请注明出处你是不是以为你真的懂For...in... ??哈哈哈哈, 我也碰到了这个报错 . ...
- 致改变——总结&规划(2016·一)
今天是立夏,过完这一天意味着农历2016年的第一季度已经过去了,也意味着真正的夏天已经来了.如果说春天是作物的播种期的话,那夏天可以看做是作物的成长期,也是农民伯伯们最繁忙的时期.本文主要对自己过去的 ...
- 由点击页面其它地方隐藏div所想到的jQuery的delegate
对于这个问题一般有两种思路,这两种思路都会利用事件冒泡这一原理,想要详细了解Javascript事件机制可以看看JavaScript与HTML交互——事件,这不是本文重点,所以这里只是简单介绍一下事件 ...
- HDU-1896 Stones
http://acm.hdu.edu.cn/showproblem.php?pid=1896 题意:一个人从0开始走起,遇到偶数个石头就踢.要是同一位置有多个石头,则先扔最重的石头(也就是扔的最近的那 ...
- [CODEVS1037]取数游戏
N(2 <=N<=200,且为偶数)个正整数的序列放在一个游戏平台上,A.B两人轮流从序列的两端取数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结束.以最终得分多者为胜(A ...
- 【JS】Beginner3 & 4 & 5 & 6:Maths & Logic & Conditonal & Looping
1.number operator () * / + - 2.logic make decisions in code compare values to produce a boolean valu ...
- Floyd-Warshall算法的理解
Floyd算法可以求图内任意两点之间的最短路径,三重循环搞定,虽然暴力,但是属于算法当中最难的动态规划的一种,很有必要理解. 花了一晚上和半个下午专门看这个,才看个一知半解,智商被碾压没办法. 我一直 ...
- win7下wubi安装Ubuntu,重装win7后找回Ubuntu启动项
怀念一下我的win7,使用了将近5年,最近终于慢慢处于崩溃且无法修复的状态. 还是重新安装了. 原本是win7下使用wubi安装Ubuntu.重装win7后,肯定没有了Ubuntu的启动项. 具体恢复 ...
- 经典 Linux & VIM 教程
简明 Vim 练级攻略: http://coolshell.cn/articles/5426.html VIM快捷键: http://coolshell.cn/wp-content/uploads/2 ...
- php&mysql
更新语句 $query='update num set num='.$counter.'where id=1'; ....................这个查询失败 $query='upd ...