Android开发 ---基本UI组件2

1、activity_main.xml 

  描述:

    定义一个用户注册按钮

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/activity_main"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7.  
  8. <Button
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="用户注册"
  12. android:onClick="test_3"/>
  13. </LinearLayout>

2、MainActivity.java

  描述:

    页面跳转

  1. package com.example.android_ui;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6.  
  7. public class MainActivity extends Activity {
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. }public void test_3(View view){
  14. Intent intent=new Intent(this,UserRegisterActivity.class);
  15. startActivity(intent);
  16. }
  17. }

3、activity_button.xml

  资源文件中的资源:

 edittext_border_bg.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <stroke
  4. android:width="2dp"
  5. android:color="@color/colorPrimary"
  6. />
  7. </shape>

  描述:

    1、一张背景图像

    2、android:maxLength="12"  设置文本输入框中能够输入的最大长度

    3、定义了一组单选按钮

    4、定义了一组多选按钮

    5、定义了一个下拉列表  ---Spinner

    6、分别定义了一个提交、重置、取消按钮  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/activity_user_register"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:layout_marginLeft="5dp"
  7. android:layout_marginRight="5dp"
  8. android:orientation="vertical">
  9. <ImageView
  10. android:layout_width="match_parent"
  11. android:src="@drawable/bg1"
  12. android:scaleType="fitXY"
  13. android:layout_height="80dp" />
  14. <TextView
  15. android:layout_width="match_parent"
  16. android:text="用户注册"
  17. android:gravity="center"
  18. android:textSize="30sp"
  19. android:layout_marginTop="3dp"
  20. android:layout_height="wrap_content" />
  21. <LinearLayout
  22. android:layout_width="match_parent"
  23. android:orientation="horizontal"
  24. android:layout_height="40dp">
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="用户名:"
  29. android:textSize="22sp"
  30. android:gravity="bottom"
  31. />
  32. <EditText
  33. android:id="@+id/uname"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:hint="请输入用户名"
  37. android:maxLength="12"
  38. android:textSize="22sp"
  39. android:background="@drawable/edittext_border_bg"
  40. />
  41. </LinearLayout>
  42. <LinearLayout
  43. android:layout_width="match_parent"
  44. android:orientation="horizontal"
  45. android:layout_height="40dp">
  46. <TextView
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:text="密 码:"
  50. android:textSize="22sp"
  51. android:gravity="bottom"
  52. />
  53. <EditText
  54. android:id="@+id/upwd"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:hint="请输入密码"
  58. android:inputType="textPassword"
  59. android:maxLength="12"
  60. android:textSize="22sp"
  61. android:background="@drawable/edittext_border_bg"
  62. />
  63. </LinearLayout>
  64. <LinearLayout
  65. android:layout_width="match_parent"
  66. android:orientation="horizontal"
  67. android:layout_height="40dp">
  68. <TextView
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:text="性 别:"
  72. android:textSize="22sp"
  73. android:gravity="bottom"
  74. />
  75. <RadioGroup
  76. android:id="@+id/usexGroup"
  77. android:orientation="horizontal"
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content">
  80. <RadioButton
  81. android:id="@+id/usex1"
  82. android:text="男"
  83. android:checked="true"
  84. android:layout_width="wrap_content"
  85. android:layout_height="wrap_content" />
  86. <RadioButton
  87. android:id="@+id/usex2"
  88. android:text="女"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content" />
  91. </RadioGroup>
  92. </LinearLayout>
  93. <LinearLayout
  94. android:layout_width="match_parent"
  95. android:orientation="horizontal"
  96. android:layout_height="40dp">
  97. <TextView
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:text="爱 好:"
  101. android:textSize="22sp"
  102. android:gravity="bottom"
  103. />
  104. <LinearLayout
  105. android:layout_width="match_parent"
  106. android:layout_height="wrap_content"
  107. android:orientation="horizontal">
  108. <CheckBox
  109. android:id="@+id/uhobby1"
  110. android:text="体育"
  111. android:layout_width="wrap_content"
  112. android:layout_height="wrap_content" />
  113. <CheckBox
  114. android:id="@+id/uhobby2"
  115. android:text="音乐"
  116. android:layout_width="wrap_content"
  117. android:layout_height="wrap_content" />
  118. <CheckBox
  119. android:id="@+id/uhobby3"
  120. android:text="美术"
  121. android:layout_width="wrap_content"
  122. android:layout_height="wrap_content" />
  123. </LinearLayout>
  124. </LinearLayout>
  125. <LinearLayout
  126. android:layout_width="match_parent"
  127. android:orientation="horizontal"
  128. android:layout_height="wrap_content">
  129. <TextView
  130. android:layout_width="wrap_content"
  131. android:layout_height="wrap_content"
  132. android:text="职 业:"
  133. android:textSize="22sp"
  134. android:gravity="bottom"
  135. />
  136. <Spinner
  137. android:id="@+id/hjob"
  138. android:entries="@array/jobs"
  139. android:spinnerMode="dropdown"
  140. android:layout_width="match_parent"
  141. android:layout_height="wrap_content">
  142. </Spinner>
  143. </LinearLayout>
  144. <LinearLayout
  145. android:layout_width="match_parent"
  146. android:orientation="horizontal"
  147. android:layout_height="wrap_content">
  148. <Button
  149. android:layout_width="match_parent"
  150. android:layout_weight="1"
  151. android:text="提交"
  152. android:onClick="doSubmit"
  153. android:layout_height="wrap_content" />
  154. <Button
  155. android:layout_width="match_parent"
  156. android:layout_weight="1"
  157. android:text="重置"
  158. android:onClick="doReset"
  159. android:layout_height="wrap_content" />
  160. <Button
  161. android:layout_width="match_parent"
  162. android:layout_weight="1"
  163. android:text="取消"
  164. android:onClick="doBack"
  165. android:layout_height="wrap_content" />
  166. </LinearLayout>
  167. </LinearLayout> 

4、ButtonActivity.java

  描述:    

  1. package com.example.android_ui;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.CheckBox;
  7. import android.widget.EditText;
  8. import android.widget.RadioButton;
  9. import android.widget.RadioGroup;
  10. import android.widget.Spinner;
  11. import android.widget.Toast;
  12.  
  13. public class UserRegisterActivity extends Activity {
  14.   //获取文本输入框的用户名和密码
  15. private EditText uname,upwd;
      //获取单选按钮组
  16. private RadioGroup rgsex;
      //获取多选按钮
  17. private CheckBox ck1,ck2,ck3;
      //获取下拉列表
  18. private Spinner spinner;
       //性别默认设置为男
  19. String sex="男";
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_user_register);
         //分别将所有元素获取到
  24. uname=(EditText) findViewById(R.id.uname);
  25. upwd=(EditText) findViewById(R.id.upwd);
  26. rgsex=(RadioGroup) findViewById(R.id.usexGroup);
  27. ck1=(CheckBox)findViewById(R.id.uhobby1);
  28. ck2=(CheckBox)findViewById(R.id.uhobby2);
  29. ck3=(CheckBox)findViewById(R.id.uhobby3);
  30. spinner=(Spinner)findViewById(R.id.hjob);
  31.      //给单选按钮组设置选择改变监听事件 
  32. rgsex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  33. @Override
  34. public void onCheckedChanged(RadioGroup radioGroup, int i) {
  35. if(i==R.id.usex1){
  36. sex="男";
  37. }else{
  38. sex="女";
  39. }
  40. }
  41. });
  42. }
  43.    //定义一个提交按钮事件
  44. public void doSubmit(View view){
         //当点击提交按钮时,获取用户名文本框中的文本内容,并转化为字符串保存在name变量上
  45. String name=uname.getText().toString();
         //同上
  46. String pwd=upwd.getText().toString();
         //多选按钮的操作
  47. String hobby="";
         //判断如果ck1被选中
  48. if(ck1.isChecked()){
           //则 获取到ck1中的文本内容
  49. hobby=hobby+ck1.getText().toString()+",";
  50. }
         //同上
  51. if(ck2.isChecked()){
  52. hobby=hobby+ck2.getText().toString()+",";
  53. }
         //同上
  54. if(ck3.isChecked()){
  55. hobby=hobby+ck3.getText().toString()+",";
  56. }
         //如果有一个或一个以上被选中
  57. if(hobby.length()>0){
           //则hobby调用substring()方法返回一个新字符串,这个字符串从指定的0开始,一直索引到hobby.length()-1为止
  58. hobby=hobby.substring(0,hobby.length()-1);
  59. }
         //下拉列表的操作
         //获取当前选中项的值
  60. String job=spinner.getSelectedItem().toString();
  61. String message="用户名:"+name+"\n\r";
  62. message+="密码:"+pwd+"\n\r";
  63. message+="性别:"+sex+"\n\r";
  64. message+="爱好:"+hobby+"\n\r";
  65. message+="职业:"+job+"\n\r";
         //弹出内容
  66. Toast.makeText(this,message,Toast.LENGTH_LONG).show();
  67.  
  68. }
       //定义一个重置按钮
  69. public void doReset(View view){
         //将用户名文本框设置为空
  70. uname.setText("");
         //将密码文本框设置为空
  71. upwd.setText("");
         //将选中的按钮设置为未选中
  72. ck1.setChecked(false);
  73. ck2.setChecked(false);
  74. ck3.setChecked(false);
  75. }
      //定义一个取消按钮
  76. public void doBack(View view){
  77. this.finish();
  78. }
  79. }

Android开发 ---基本UI组件3:单选按钮、多选按钮、下拉列表、提交按钮、重置按钮、取消按钮的更多相关文章

  1. Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...

  2. Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制

    Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=" ...

  3. Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  4. Android开发 ---基本UI组件7 :分页功能、适配器、滚动条监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  5. Android开发---基本UI组件1:自动拨电话,自动上网,输入框不换行、只输数字、只输文本、只输密码

    1.activity_main.xml 描述:构建一个按钮 <?xml version="1.0" encoding="utf-8"?> <L ...

  6. Android开发 ---基本UI组件8:九宫格布局、setOnItemClickListener()项被选中监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  7. Android开发 ---基本UI组件5:监听下拉选项,动态绑定下拉选项、全选/反选,取多选按钮的值,长按事件,长按删除,适配器的使用,提示查询数据,activity控制多按钮

    效果图: 效果描述: 1.当点击 1 按钮后,进入选择城市的页面,会监听到你选中的城市名称:动态为Spinner绑定数据 2.当点击 2 按钮后,进入自动查询数据页面,只要输入首字母,就会动态查找以该 ...

  8. 安卓开发:UI组件-RadioButton和复选框CheckBox

    2.5RadioButton 为用户提供由两个及以上互斥的选项组成的选项集. 2.5.1精简代码 在按钮变多之后,多次重复书写点击事件有些繁琐,我们在这里创建一个事件OnClick,每次点击时调用该事 ...

  9. android开发之自定义组件

    android开发之自定义组件 一:自定义组件: 我认为,自定义组件就是android给我们提供的的一个空白的可以编辑的图片,它帮助我们实现的我们想要的界面,也就是通过自定义组件我们可以把我们要登入的 ...

随机推荐

  1. 20165327 2017-2018-2 《Java程序设计》第8周学习总结

    20165327 2017-2018-2 <Java程序设计>第8周学习总结 教材内容总结 第十二章 (一)教材学习内容总结 线程是比进程更小的执行单位.一个进程在其执行过程中,可以产生多 ...

  2. Cisco常用配置和命令

    1.ASA常用管理management-access inside      #开启远程连接inside口 show snmp-server oidlist          #查看ASA snmp的 ...

  3. pandas更换index,column名称

    1)仅换掉index名称 df.index = list 2)调整index时,后面的项目也要跟着调整: df.reindex(list) 注意如果list中出现了df中没有的index,后面的项目会 ...

  4. Spring Batch JSR-305 支持

    本发布版本中为 JSR-305 支持添加了一个注解.这个为了与 Spring 框架中的  Null-safety 注解取得平衡,然后为 Spring Batch 添加为 public APIs. 这个 ...

  5. 以CapsNet为例谈深度学习源码阅读

    本文的参考的github工程链接:https://github.com/laubonghaudoi/CapsNet_guide_PyTorch 之前是看过一些深度学习的代码,但是没有养成良好的阅读规范 ...

  6. PHP设计模式注意点

    PHP命名空间 可以更好地组织代码,与Java中的包类似. Test1.php <?php namespace Test1;//命名空间Test1 function test(){ echo _ ...

  7. PHP流程控制笔记

    一.运算符(Operator) 1.运算符 2.运算符分类   (1)按功能分   (2)按操作数个数分 3.按功能分   (1)算术运算符   (2)递增递减   (3)字符运算符   (4)赋值运 ...

  8. windows系统文件和linux系统文件

    windows系统文件和linux系统文件 1.单用户操作系统和多用户操作系统 单用户操作系统:指一台计算机在同一时间 只能由一个用户 使用,一个用户独自享用系统的全部硬件和软件资源 Windows ...

  9. Linux中磁盘mbr分区——实践篇

    Linux中磁盘mbr分区——实践篇 fdisk命令 语法 fdisk(选项)(参数) 选项 -b <分区大小> 指定每个分区的大小 -l 列出分区表信息 -v 显示版本信息 参数 设备文 ...

  10. vue element upload图片 回显问题

      beforeUpload (file) { var _this = this; var reader = new FileReader(); reader.readAsDataURL(file); ...