当前位置:   article > 正文

Android studio 学生信息录入表(课堂作业)_安卓手动录入信息代码

安卓手动录入信息代码

任务:

制作学生信息录入表:

开发步骤:

  • 运用线性布局嵌套,按照模型制作activity_main.xml
  • 添加组件TextView、EditText、单选框、复选框、按钮等
  • 编辑MainActivity.java,制作各组件监听器

源代码:

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="学生信息录入:"
  11. android:textSize="40sp" />
  12. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:orientation="horizontal">
  16. <TextView
  17. android:id="@+id/textname"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="姓名:"
  21. android:textSize="30sp" />
  22. <EditText
  23. android:id="@+id/name"
  24. android:layout_width="237dp"
  25. android:layout_height="wrap_content"
  26. android:textSize="30sp" />
  27. </LinearLayout>
  28. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:orientation="horizontal">
  32. <TextView
  33. android:id="@+id/textsex"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="性别:"
  37. android:textSize="30sp" />
  38. <RadioGroup
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:orientation="horizontal">
  42. <RadioButton
  43. android:id="@+id/man"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:text="男" />
  47. <RadioButton
  48. android:id="@+id/woman"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:text="女" />
  52. </RadioGroup>
  53. </LinearLayout>
  54. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:orientation="horizontal">
  58. <TextView
  59. android:id="@+id/textage"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:text="年龄:"
  63. android:textSize="30sp" />
  64. <EditText
  65. android:id="@+id/age"
  66. android:layout_width="237dp"
  67. android:layout_height="wrap_content"
  68. android:textSize="30sp" />
  69. </LinearLayout>
  70. <LinearLayout
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:orientation="vertical">
  74. <TextView
  75. android:id="@+id/hobby"
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:text="兴趣爱好:"
  79. android:textSize="30sp" />
  80. <CheckBox
  81. android:id="@+id/basketball"
  82. android:layout_width="match_parent"
  83. android:layout_height="wrap_content"
  84. android:text="篮球" />
  85. <CheckBox
  86. android:id="@+id/football"
  87. android:layout_width="match_parent"
  88. android:layout_height="wrap_content"
  89. android:text="足球" />
  90. <CheckBox
  91. android:id="@+id/music"
  92. android:layout_width="match_parent"
  93. android:layout_height="wrap_content"
  94. android:text="音乐" />
  95. </LinearLayout>
  96. <Button
  97. android:id="@+id/confirm"
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:text="确定" />
  101. <TextView
  102. android:id="@+id/confirmed"
  103. android:layout_width="match_parent"
  104. android:layout_height="wrap_content"
  105. android:textSize="30dp"
  106. android:text="您的个人信息:" />
  107. <TextView
  108. android:id="@+id/message"
  109. android:layout_width="match_parent"
  110. android:layout_height="match_parent"
  111. android:textSize="27dp"/>
  112. </LinearLayout>

 

 

MainActivity.java

  1. package com.example.excel;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.CheckBox;
  8. import android.widget.TextView;
  9. import android.widget.EditText;
  10. import android.widget.RadioButton;
  11. public class MainActivity extends AppCompatActivity {
  12. Button okBtn;
  13. EditText editname,editage;
  14. TextView showmesage;
  15. RadioButton r1,r2;
  16. CheckBox c1,c2,c3;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. editname = (EditText)findViewById(R.id.name);
  22. editage = (EditText)findViewById(R.id.age);
  23. okBtn = (Button)findViewById(R.id.confirm);
  24. showmesage = (TextView)findViewById(R.id.message);
  25. r1 = (RadioButton)findViewById(R.id.man);
  26. r2 = (RadioButton)findViewById(R.id.woman);
  27. c1 = (CheckBox)findViewById(R.id.basketball);
  28. c2 = (CheckBox)findViewById(R.id.football);
  29. c3 = (CheckBox)findViewById(R.id.music);
  30. okBtn.setOnClickListener(new mClick());
  31. }
  32. class mClick implements OnClickListener{
  33. public void onClick(View v){
  34. CharSequence xingming = "",xingbie = "",nianling = "",aihao = "";
  35. xingming = editname.getText();
  36. if(r1.isChecked())
  37. xingbie = r1.getText();
  38. if(r2.isChecked())
  39. xingbie = r2.getText();
  40. nianling = editage.getText();
  41. if(c1.isChecked())
  42. aihao = aihao+" "+c1.getText();
  43. if(c2.isChecked())
  44. aihao = aihao+" "+c2.getText();
  45. if(c3.isChecked())
  46. aihao = aihao+" "+c3.getText();
  47. showmesage.setText("姓名:"+xingming+"\n性别:"+xingbie+"\n年龄:"+nianling+"\n业余爱好:"+aihao);
  48. }
  49. }
  50. }

运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/598659
推荐阅读
相关标签
  

闽ICP备14008679号