Android上机实验-2 基本控件的使用

简介: Android上机实验-2 基本控件的使用

一、实验目的

掌握TextView、Button、EditText、ImageView、RadioButton、checkbox的基本使用方式,设计实现注册页面。

二、实验内容

练习TextView、Button、EditText、ImageView、RadioButton、checkbox的基本属性及常用方法,设计并实现注册页面。

三、实验指导

1.新建工程创建新的布局文件。

2.在布局文件中根据自己设计放置TextView、Button、EditText、ImageView、RadioButton、checkbox等控件,设计注册页面。

3.编码实现注册页面,点击注册页面注册按钮将所填信息(自己的学号、姓名)及所选择的信息(可以是自己的兴趣爱好等)使用TextView显示出来。

四、模拟器效果截图

五、实验源代码

1. package com.example.myapplication02;
2. 
3. import android.os.Bundle;
4. import android.view.View;
5. import android.widget.Button;
6. import android.widget.CheckBox;
7. import android.widget.CompoundButton;
8. import android.widget.EditText;
9. import android.widget.RadioButton;
10. import android.widget.RadioGroup;
11. import android.widget.TextView;
12. import android.widget.Toast;
13. import androidx.appcompat.app.AppCompatActivity;
14. 
15. public class Register extends AppCompatActivity {
16.     Button bt;
17. private RadioGroup radioGroup;
18. private RadioButton current;
19. private CompoundButton cp_current;
20. private CheckBox box1,box2,box3,box4;
21. private TextView T_numb;
22. private TextView T_name;
23. private TextView T_sex;
24. private TextView T_like;
25. private String cp_currstr="";
26. private String cp_result="";
27. @Override
28. protected void onCreate(Bundle savedInstanceState) {
29. super.onCreate(savedInstanceState);
30.         setContentView(R.layout.register);
31.         bt=findViewById(R.id.bt);
32.         T_numb=findViewById(R.id.numb);
33.         T_name=findViewById(R.id.name);
34.         radioGroup=findViewById(R.id.gp);
35.         T_sex=findViewById(R.id.T_sex);
36.         bt.setOnClickListener(new View.OnClickListener() {
37. @Override
38. public void onClick(View view) {
39. String number = "";
40.                 number = T_numb.getText().toString();
41.                 T_numb=findViewById(R.id.T_numb);
42. String lname = "";
43.                 lname = T_name.getText().toString();
44.                 T_name=findViewById(R.id.T_name);
45.                 Toast.makeText(Register.this, number, Toast.LENGTH_SHORT).show();
46.                 Toast.makeText(Register.this, lname, Toast.LENGTH_SHORT).show();
47.                 T_numb.setText("学号:"+number);
48.                 T_name.setText("姓名:"+lname);
49.                 T_sex.setText("性别:"+current.getText().toString());
50.                 T_like.setText("爱好:"+cp_result);
51.             }
52.         });
53.         radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
54. @Override
55. public void onCheckedChanged(RadioGroup radioGroup, int checkedid) {
56.                 current=findViewById(checkedid);
57. //Toast.makeText(Register.this, current.getText().toString(), Toast.LENGTH_SHORT).show();
58.             }
59.         });
60.         box1=findViewById(R.id.box1);
61.         box2=findViewById(R.id.box2);
62.         box3=findViewById(R.id.box3);
63.         box4=findViewById(R.id.box4);
64.         T_like=findViewById(R.id.T_like);
65. Mylistener mylistener = new Mylistener();
66.         box1.setOnCheckedChangeListener(mylistener);
67.         box2.setOnCheckedChangeListener(mylistener);
68.         box3.setOnCheckedChangeListener(mylistener);
69.         box4.setOnCheckedChangeListener(mylistener);
70.     }
71. class Mylistener implements CompoundButton.OnCheckedChangeListener{
72. @Override
73. public void onCheckedChanged(CompoundButton compoundButton,boolean ischecked){
74.             cp_currstr=compoundButton.getText().toString();
75. if(ischecked){
76. if(!cp_result.contains(cp_currstr)){
77.                     cp_result=cp_result+cp_currstr;
78.                 }
79.             }else {
80. if(!cp_result.contains(cp_currstr)){
81.                     cp_result=cp_result.replace(cp_currstr,"");
82.                 }
83.             }
84.         }
85.     }
86. }

XML

1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:orientation="vertical"
6. android:background="@drawable/green"
7. xmlns:tools="http://schemas.android.com/tools">
8. <!--输入学号-->
9. <LinearLayout
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:orientation="horizontal"
13. tools:layout_editor_absoluteY="0dp"
14. tools:layout_editor_absoluteX="8dp"/>
15. <TextView
16. android:layout_width="wrap_content"
17. android:layout_height="40dp"
18. android:textSize="30dp"
19. android:textColor="@android:color/background_dark"
20. android:text="学号"/>
21. <EditText
22. android:id="@+id/numb"
23. android:layout_width="match_parent"
24. android:layout_height="60dp"
25. android:hint="请输入您的学号:"/>
26. <!--姓名-->
27. <LinearLayout
28. android:layout_width="wrap_content"
29. android:layout_height="wrap_content"
30. android:orientation="horizontal"
31. tools:layout_editor_absoluteY="0dp"
32. tools:layout_editor_absoluteX="8dp"/>
33. <TextView
34. android:layout_width="wrap_content"
35. android:layout_height="40dp"
36. android:textSize="30dp"
37. android:textColor="@android:color/background_dark"
38. android:text="姓名"/>
39. <EditText
40. android:id="@+id/name"
41. android:layout_width="match_parent"
42. android:layout_height="60dp"
43. android:hint="请输入您的姓名:"/>
44. <!--性别-->
45. <RadioGroup
46. android:id="@+id/gp"
47. android:layout_width="match_parent"
48. android:layout_height="40dp"
49. android:orientation="horizontal">
50. <RadioButton
51. android:id="@+id/sex1"
52. android:layout_width="80dp"
53. android:layout_height="wrap_content"
54. android:textSize="30dp"
55. android:text="男"/>
56. <RadioButton
57. android:id="@+id/sex2"
58. android:layout_width="80dp"
59. android:layout_height="wrap_content"
60. android:textSize="30dp"
61. android:text="女"/>
62. </RadioGroup>
63. <!--爱好-->
64. <LinearLayout
65. android:id="@+id/cpbox"
66. android:layout_width="match_parent"
67. android:layout_height="wrap_content"
68. android:orientation="horizontal">
69. <CheckBox
70. android:id="@+id/box1"
71. android:layout_width="wrap_content"
72. android:layout_height="wrap_content"
73. android:textSize="30dp"
74. android:text="读书"/>
75. <CheckBox
76. android:id="@+id/box2"
77. android:layout_width="wrap_content"
78. android:layout_height="wrap_content"
79. android:textSize="30dp"
80. android:text="打球"/>
81. <CheckBox
82. android:id="@+id/box3"
83. android:layout_width="wrap_content"
84. android:layout_height="wrap_content"
85. android:textSize="30dp"
86. android:text="睡觉"/>
87. <CheckBox
88. android:id="@+id/box4"
89. android:layout_width="wrap_content"
90. android:layout_height="wrap_content"
91. android:textSize="30dp"
92. android:text="听歌"/>
93. </LinearLayout>
94. <Button
95. android:id="@+id/bt"
96. android:layout_width="fill_parent"
97. android:layout_height="40dp"
98. android:background="#3F51B5"
99. android:textColor="#FFFFFF"
100. android:textSize="18sp"
101. android:text="注册">
102. </Button>
103. <TextView
104. android:id="@+id/T_numb"
105. android:layout_width="wrap_content"
106. android:layout_height="wrap_content"
107. android:textSize="30dp"
108. android:textColor="@android:color/background_dark" />
109. <TextView
110. android:id="@+id/T_name"
111. android:layout_width="wrap_content"
112. android:layout_height="wrap_content"
113. android:textSize="30dp"
114. android:textColor="@android:color/background_dark" />
115. <TextView
116. android:id="@+id/T_sex"
117. android:layout_width="wrap_content"
118. android:layout_height="wrap_content"
119. android:textSize="30dp"
120. android:textColor="@android:color/background_dark" />
121. <TextView
122. android:id="@+id/T_like"
123. android:layout_width="wrap_content"
124. android:layout_height="wrap_content"
125. android:textSize="30dp"
126. android:textColor="@android:color/background_dark" />
127. </LinearLayout>
目录
相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
88 1
|
4月前
|
Java Android开发
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
527 0
|
2月前
|
XML 数据格式
Android-自定义三角形评分控件
Android-自定义三角形评分控件
28 0
|
3月前
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
46 4
|
3月前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
85 2
|
3月前
|
Java Android开发 开发者
17. 【Android教程】开关控件ToggleButton/Switch
17. 【Android教程】开关控件ToggleButton/Switch
41 2
|
3月前
|
XML Android开发 数据格式
Android基础控件介绍
Android基础控件介绍
|
3月前
|
Android开发
Android 自定义View 测量控件宽高、自定义viewgroup测量
Android 自定义View 测量控件宽高、自定义viewgroup测量
37 0
|
4月前
|
XML Java Android开发
Android控件动态使用 (转)
Android控件动态使用 (转)
27 1
|
4月前
|
JSON Android开发 数据格式
Android框架-Google官方Gson解析,android开发实验报告总结
Android框架-Google官方Gson解析,android开发实验报告总结