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的更多相关文章
随机推荐
- Form – 保存自動關閉當前窗口
FAQ: 在BUTTON的触发器中,写如下代码, commit_form; go_bloack('你想显示的那个window的block'); --或者写 show_view('你要显示的canvas ...
- Sublime Text主题下载、安装与配置
从下面地址下载主题包,以下载第一个为例,解压缩并重命名为Theme – Flatland 备注:下载好的文件中 .sublime-theme后缀的表示界面主题(theme),.tmTheme表示颜色 ...
- struts2加入自定义的actionValidatorManager实现类
<constant name="struts.actionValidatorManager" value="bap"/>
- bzoj1922
首先机器人是并行的: 很容易想到到某个点的最短用时 =max(到这个点的最短路,max(到保护这个点结界所在点的最短用时)) 所以我们在做dij的时候,d[j]维护最短路,w[j]维护所有保护这个点结 ...
- C#程序中访问配置文件
在C#编程中,有时候会用到配置文件,那么该如何在程序中获取或修改配置文件中的相关数据呢?下面采用一个简单的C#控制台程序来说明. 新建一个C#控制台程序,打开“解决方案资源管理器”,如下图: 可以看到 ...
- Orchard中的多语言功能
在Orchard中支持了两种本地化的方法: 1.对Orchard应用程序和模块中的一些文本字符串进行本地化.这个就相当程序本身的多语言支持,大多数的CMS系统都支持这一功能,如:DotNetNuke. ...
- Microsoft SQL Server,错误:2;SQL Server配置管理器(本地)—远程过程调用失败
本机是先安装sqlserver2008,后安装vs2012 在安装sqlserver2008后,运行sqlserver2008正常,接着安装vs2012,再运行sqlserver2008,问题出现了, ...
- 浅谈Xcode5和Xcode7在系统创建的文件夹和文件中的区别
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- HW5.16
public class Solution { public static void main(String[] args) { for(int i = 2000; i <= 2010; i++ ...
- HW3.5
import java.util.Scanner; public class Solution { public static void main(String[] args) { int n1 = ...