编辑框窗口跳转

矫情吗;* 2024-03-30 10:52 174阅读 0赞

目录

编辑框常用属性

具体例子

最后效果

基于Empty Activity模板创建安卓应用 - UserRegistration

准备图片素材

主界面与主布局资源文件更名并新建信息界面

打开字符串资源文件strings.xml

注册界面布局资源文件activity_registration.xml输入代码

查看预览效果

打开 信息界面布局资源文件 - activity_information.xml输入代码

查看预览效果

打开用户注册界面类 - RegistrationActivity输入代码

打开注册信息显示界面 - InformationActivity输入代码


编辑框常用属性
















































属性 含义
text 文本内容
textSize 文本字号,单位:sp
textColor 文本颜色,#ff0000 - 红色
hint 提示信息
singleLine 单行(true or false)
layout_height 高度,单位:dp (wrap_content, match_parent)
layout_weight 宽度,单位:dp (wrap_content, match_parent)
inputType 输入类型(普通文本、密码、邮件……)
maxLines 最大行数
lines 行数

具体例子

最后效果

695a777a1c394fa8ab593b7f7eb25a0f.gif

基于Empty Activity模板创建安卓应用 - UserRegistration

c535bc5ce2ce46a6be8ac81d36b0b9d2.png

准备图片素材

将两张背景图片拷贝到drawable目录

e5fa7d81e3964172814d0dbfe010bbbf.png

主界面与主布局资源文件更名并新建信息界面

选中MainActivity>Refactor>Rename

82d132272a2343b7bc3668adcc7cb2b3.png

右击net.zvt.userregistration>New>Java Classb08229e2123844298f74f44210dbe7f2.png056d9e9cbf7545569c9782f14ba925c1.png

f87dc34cf293498d93ad021924b5420d.png

打开字符串资源文件strings.xml

49b7f2075a924870a6d75228014c53bc.png

具体代码:

  1. <resources>
  2. <string name="app_name">用户注册</string>
  3. <string name="name">姓名:</string>
  4. <string name="gender">性别:</string>
  5. <string name="age">年龄:</string>
  6. <string name="phone">电话:</string>
  7. <string name="email">邮箱:</string>
  8. <string name="home_page">主页:</string>
  9. <string name="memo">备注:</string>
  10. <string name="register">注册</string>
  11. <string name="cancel">取消</string>
  12. </resources>

注册界面布局资源文件activity_registration.xml输入代码

9d77a882571b451bb1733af95d3f5166.png

43756cd67d744aae9011b31c34daedec.png

66128e030446466caff6d6edbbc5cc66.png

具体代码:

  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:background="@drawable/reg_bg"
  6. android:gravity="center"
  7. android:orientation="vertical"
  8. >
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:gravity="center">
  13. <TextView
  14. android:id="@+id/tv_name"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="@string/name"
  18. android:textColor="#000000"
  19. android:textSize="16sp" />
  20. <EditText
  21. android:id="@+id/et_name"
  22. android:layout_width="200dp"
  23. android:layout_height="wrap_content"
  24. android:ems="10"
  25. android:singleLine="true" />
  26. </LinearLayout>
  27. <LinearLayout
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:gravity="center">
  31. <TextView
  32. android:id="@+id/tv_gender"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="@string/gender"
  36. android:textColor="#000000"
  37. android:textSize="16sp" />
  38. <EditText
  39. android:id="@+id/et_gender"
  40. android:layout_width="200dp"
  41. android:layout_height="wrap_content"
  42. android:ems="10"
  43. android:singleLine="true" />
  44. </LinearLayout>
  45. <LinearLayout
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:gravity="center">
  49. <TextView
  50. android:id="@+id/tv_age"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:text="@string/age"
  54. android:textColor="#000000"
  55. android:textSize="16sp" />
  56. <EditText
  57. android:id="@+id/et_age"
  58. android:layout_width="200dp"
  59. android:layout_height="wrap_content"
  60. android:ems="10"
  61. android:inputType="number"
  62. android:singleLine="true" />
  63. </LinearLayout>
  64. <LinearLayout
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. android:gravity="center">
  68. <TextView
  69. android:id="@+id/tv_phone"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:text="@string/phone"
  73. android:textColor="#000000"
  74. android:textSize="16sp" />
  75. <EditText
  76. android:id="@+id/et_phone"
  77. android:layout_width="200dp"
  78. android:layout_height="wrap_content"
  79. android:ems="10"
  80. android:inputType="phone"
  81. android:singleLine="true" />
  82. <!-- ems的意思是一行占据的空间=10个M占据的空间。也因此,width就要设置为wrap-content-->
  83. </LinearLayout>
  84. <LinearLayout
  85. android:layout_width="match_parent"
  86. android:layout_height="wrap_content"
  87. android:gravity="center">
  88. <TextView
  89. android:id="@+id/tv_email"
  90. android:layout_width="wrap_content"
  91. android:layout_height="wrap_content"
  92. android:text="@string/email"
  93. android:textColor="#000000"
  94. android:textSize="16sp" />
  95. <EditText
  96. android:id="@+id/et_email"
  97. android:layout_width="200dp"
  98. android:layout_height="wrap_content"
  99. android:ems="10"
  100. android:inputType="textEmailAddress"
  101. android:singleLine="true" />
  102. </LinearLayout>
  103. <LinearLayout
  104. android:layout_width="match_parent"
  105. android:layout_height="wrap_content"
  106. android:gravity="center">
  107. <TextView
  108. android:id="@+id/tv_homepage"
  109. android:layout_width="wrap_content"
  110. android:layout_height="wrap_content"
  111. android:text="@string/home_page"
  112. android:textColor="#000000"
  113. android:textSize="16sp" />
  114. <EditText
  115. android:id="@+id/et_homepage"
  116. android:layout_width="200dp"
  117. android:layout_height="wrap_content"
  118. android:ems="10"
  119. android:inputType="textUri"
  120. android:singleLine="true" />
  121. </LinearLayout>
  122. <LinearLayout
  123. android:layout_width="match_parent"
  124. android:layout_height="wrap_content"
  125. android:gravity="center">
  126. <TextView
  127. android:id="@+id/tv_memo"
  128. android:layout_width="wrap_content"
  129. android:layout_height="wrap_content"
  130. android:text="@string/memo"
  131. android:textColor="#000000"
  132. android:textSize="16sp" />
  133. <EditText
  134. android:id="@+id/et_memo"
  135. android:layout_width="200dp"
  136. android:layout_height="wrap_content"
  137. android:ems="10"
  138. android:lines="4" />
  139. </LinearLayout>
  140. <LinearLayout
  141. android:layout_width="match_parent"
  142. android:layout_height="wrap_content"
  143. android:gravity="center">
  144. <Button
  145. android:id="@+id/btn_register"
  146. android:layout_width="80dp"
  147. android:layout_height="wrap_content"
  148. android:onClick="doRegister"
  149. android:layout_marginRight="15dp"
  150. android:text="@string/register" />
  151. <Button
  152. android:id="@+id/btn_cancel"
  153. android:layout_width="80dp"
  154. android:layout_height="wrap_content"
  155. android:onClick="doCancel"
  156. android:text="@string/cancel" />
  157. </LinearLayout>
  158. </LinearLayout>

查看预览效果

d5f8a4660609434d89924ac969683516.png

打开 信息界面布局资源文件 - activity_information.xml输入代码

0b994511d3094b10a3e92db514ecae8c.png

ef3e9bf8227a498aa678729a480f73b7.png

具体代码:

  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:background="@drawable/info_bg"
  6. android:orientation="vertical"
  7. android:padding="20dp">
  8. <TextView
  9. android:id="@+id/tv_name"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_marginTop="10dp"
  13. android:textColor="#0000ff"
  14. android:textSize="18sp" />
  15. <TextView
  16. android:id="@+id/tv_gender"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginTop="10dp"
  20. android:textColor="#0000ff"
  21. android:textSize="18sp" />
  22. <TextView
  23. android:id="@+id/tv_age"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_marginTop="10dp"
  27. android:textColor="#0000ff"
  28. android:textSize="18sp" />
  29. <TextView
  30. android:id="@+id/tv_phone"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_marginTop="10dp"
  34. android:autoLink="phone"
  35. android:textColor="#0000ff"
  36. android:textSize="18sp" />
  37. <TextView
  38. android:id="@+id/tv_email"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_marginTop="10dp"
  42. android:autoLink="email"
  43. android:textColor="#0000ff"
  44. android:textSize="18sp" />
  45. <TextView
  46. android:id="@+id/tv_homepage"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_marginTop="10dp"
  50. android:autoLink="web"
  51. android:textColor="#0000ff"
  52. android:textSize="18sp" />
  53. <TextView
  54. android:id="@+id/tv_memo"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_marginTop="10dp"
  58. android:textColor="#0000ff"
  59. android:textSize="18sp" />
  60. </LinearLayout>

" class="reference-link">查看预览效果 2a7c1d9097e64c758c48b99f1c652526.png

打开用户注册界面类 - RegistrationActivity输入代码

a7de7cb02fdf49f79778a90ac614865b.png

5b283c6db288436c8eafb86e1060d5a0.png

具体代码

  1. package net.zyt.userregistration;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. public class RegistrationActivity extends AppCompatActivity {
  8. //声明变量
  9. private EditText etName;
  10. private EditText etGender;
  11. private EditText etAge;
  12. private EditText etPhone;
  13. private EditText etEmail;
  14. private EditText etHomepage;
  15. private EditText etMemo;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. //利用布局资源文件设置用户界面
  20. setContentView(R.layout.activity_registration);
  21. //通过资源标识符获取控件
  22. etName=findViewById(R.id.et_name);
  23. etGender=findViewById(R.id.et_gender);
  24. etAge=findViewById(R.id.et_age);
  25. etPhone=findViewById(R.id.et_phone);
  26. etEmail=findViewById(R.id.et_email);
  27. etHomepage=findViewById(R.id.et_homepage);
  28. etMemo=findViewById(R.id.et_memo);
  29. }
  30. /**
  31. * 注册按钮单击事件处理方法
  32. *
  33. // @param view
  34. */
  35. // 注册按钮单击事件处理方法
  36. public void doRegister(View view){
  37. //获取用户输入数据
  38. String name = etName.getText().toString();
  39. String gender = etGender.getText().toString();
  40. String age = etAge.getText().toString();
  41. String phone = etPhone.getText().toString();
  42. String email = etEmail.getText().toString();
  43. String homePage = etHomepage.getText().toString();
  44. String memo = etMemo.getText().toString();
  45. //将各项数据打包
  46. Bundle data = new Bundle();
  47. data.putString("name", name);
  48. data.putString("gender", gender);
  49. data.putString("age", age);
  50. data.putString("phone", phone);
  51. data.putString("email", email);
  52. data.putString("home_page", homePage);
  53. data.putString("memo", memo);
  54. //创建意图,指定起始组件与目标组件
  55. Intent intent=new Intent(this,informationActivity.class);
  56. //利用意图携带数据包
  57. intent.putExtras(data);
  58. //按意图启动目标组件
  59. startActivity(intent);
  60. }
  61. public void doCancel(View view){
  62. finish();
  63. }
  64. }

打开注册信息显示界面 - InformationActivity输入代码

e7708e8255f14a259b8d4c1e4df2cc93.png

03f884a1d21f48d4ae547c9aa283b481.png

具体代码:

  1. package net.zyt.userregistration;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class informationActivity extends AppCompatActivity {
  7. private TextView tvName;
  8. private TextView tvGender;
  9. private TextView tvAge;
  10. private TextView tvPhone;
  11. private TextView tvEmail;
  12. private TextView tvHomePage;
  13. private TextView tvMemo;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. //利用布局资源文件设置用户界面
  18. setContentView(R.layout.activity_information);
  19. // 通过资源标识获得控件示例
  20. tvName = findViewById(R.id.tv_name);
  21. tvGender = findViewById(R.id.tv_gender);
  22. tvAge = findViewById(R.id.tv_age);
  23. tvPhone = findViewById(R.id.tv_phone);
  24. tvEmail = findViewById(R.id.tv_email);
  25. tvHomePage = findViewById(R.id.tv_homepage);
  26. tvMemo = findViewById(R.id.tv_memo);
  27. // 获得意图
  28. Intent intent = getIntent();
  29. if (intent != null) {
  30. // 获得意图携带的数据包
  31. Bundle data = intent.getExtras();
  32. // 从数据包里按键取值
  33. String name = data.getString("name");
  34. String gender = data.getString("gender");
  35. String age = data.getString("age");
  36. String phone = data.getString("phone");
  37. String email = data.getString("email");
  38. String homepage = data.getString("home_page");
  39. String memo = data.getString("memo");
  40. // 设置各个标签的内容
  41. tvName.setText("姓名:" +name );
  42. tvGender.setText("性别:" + gender);
  43. tvAge.setText("年龄:" + age);
  44. tvPhone.setText("电话:" + phone);
  45. tvEmail.setText("邮箱:" + email);
  46. tvHomePage.setText("主页:" + homepage);
  47. tvMemo.setText("备注:" + memo);
  48. }
  49. }
  50. }

发表评论

表情:
评论列表 (有 0 条评论,174人围观)

还没有评论,来说两句吧...

相关阅读