0


Android开发实验-UI控件及事件响应

一、实验目的

  1. 熟悉Android Studio开发工具使用
  2. 理解事件监听方法
  3. 能根据常用控件设计的APP应用程序

二、实验任务

  1. 常用控件应用
  2. 事件监听应用
  3. Spinner控件应用

三、实验环境

  1. PC机、Android Studio

四、实验内容

(一)、常用控件应用

编写程序,实现如下功能:

  1. 设计一个用户登录的App,模拟用户通过账号或邮箱地址进行登录,并判断用户账号及密码是否正确,并给出相应的登录提示。登录界面提供账号,密码及登录类型的输入,单击登录按钮后,后台根据用户选择的类型,进行账号或邮件地址进行登录验证,并给出相关的登录提示。

假设后台的账号和密码是固定的,如:

内置账号:zhangsan 内置密码:123456

内置账号:zhangsan@163.com 内置密码:12345678

要求:(1).使用hint属性;(2).使用Toast提示登录结果

代码部分:

  1. MainActivity
  2. public class MainActivity extends AppCompatActivity {
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. final EditText nametext = findViewById(R.id.name);
  8. final EditText emailtext = findViewById(R.id.email);
  9. final EditText password = findViewById(R.id.password);
  10. Button button = (Button) findViewById(R.id.skip);
  11. button.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View view) {
  14. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  15. if(String.valueOf(nametext.getText()).equals("zhangsan")&&String.valueOf(password.getText()).equals("123456")){
  16. Toast login=Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG);
  17. login.setGravity(Gravity.CENTER,0,0);
  18. login.show();
  19. startActivity(intent);
  20. }
  21. if(String.valueOf(emailtext.getText()).equals("zhangsan@163.com")&&String.valueOf(password.getText()).equals("12345678")){
  22. Toast login=Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG);
  23. login.setGravity(Gravity.CENTER,0,0);
  24. login.show();
  25. startActivity(intent);
  26. }
  27. else{
  28. Toast login=Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_LONG);
  29. login.setGravity(Gravity.CENTER,0,0);
  30. login.show();
  31. }
  32. }
  33. });
  34. final RadioButton name = findViewById(R.id.nameButton);
  35. final RadioButton email = findViewById(R.id.emalButton);
  36. RadioGroup group = findViewById(R.id.radioGroup);
  37. group.setOnCheckedChangeListener(
  38. new RadioGroup.OnCheckedChangeListener() {
  39. @Override
  40. public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
  41. if (checkedId == R.id.nameButton){
  42. nametext.setVisibility(View.VISIBLE);
  43. emailtext.setVisibility(View.INVISIBLE);
  44. }else if (checkedId == R.id.emalButton){
  45. emailtext.setVisibility(View.VISIBLE);
  46. nametext.setVisibility(View.INVISIBLE);
  47. }
  48. }
  49. });
  50. }
  51. }
  52. activity_main.xml
  53. <TextView
  54. android:id="@+id/textView"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_marginTop="16dp"
  58. android:text="用户登录"
  59. android:textSize="20sp"
  60. app:layout_constraintEnd_toEndOf="parent"
  61. app:layout_constraintStart_toStartOf="parent"
  62. app:layout_constraintTop_toTopOf="parent" />
  63. <CheckBox
  64. android:id="@+id/checkBox"
  65. android:layout_width="137dp"
  66. android:layout_height="51dp"
  67. android:checked="true"
  68. android:text="记住登录状态"
  69. app:layout_constraintStart_toStartOf="@+id/radioGroup"
  70. app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
  71. <Button
  72. android:id="@+id/skip"
  73. android:layout_width="379dp"
  74. android:layout_height="54dp"
  75. android:layout_marginTop="8dp"
  76. android:backgroundTint="@color/grey"
  77. android:text="登录"
  78. android:textColor="@color/blue"
  79. app:layout_constraintEnd_toEndOf="@+id/radioGroup"
  80. app:layout_constraintHorizontal_bias="0.0"
  81. app:layout_constraintStart_toStartOf="@+id/checkBox"
  82. app:layout_constraintTop_toBottomOf="@+id/checkBox" />
  83. <RadioGroup
  84. android:id="@+id/radioGroup"
  85. android:layout_width="378dp"
  86. android:layout_height="51dp"
  87. android:layout_marginTop="12dp"
  88. android:orientation="horizontal"
  89. app:layout_constraintEnd_toEndOf="@+id/password"
  90. app:layout_constraintHorizontal_bias="0.0"
  91. app:layout_constraintStart_toStartOf="@+id/password"
  92. app:layout_constraintTop_toBottomOf="@+id/password">
  93. <RadioButton
  94. android:id="@+id/nameButton"
  95. android:layout_width="198dp"
  96. android:layout_height="wrap_content"
  97. android:checked="true"
  98. android:text="用户名登录" />
  99. <RadioButton
  100. android:id="@+id/emalButton"
  101. android:layout_width="154dp"
  102. android:layout_height="wrap_content"
  103. android:text="邮箱名登录" />
  104. </RadioGroup>
  105. <EditText
  106. android:id="@+id/name"
  107. android:layout_width="380dp"
  108. android:layout_height="51dp"
  109. android:layout_marginTop="9dp"
  110. android:ems="10"
  111. android:hint="用户名"
  112. android:inputType="textPersonName"
  113. app:layout_constraintEnd_toEndOf="parent"
  114. app:layout_constraintStart_toStartOf="parent"
  115. app:layout_constraintTop_toBottomOf="@+id/textView" />
  116. <EditText
  117. android:id="@+id/password"
  118. android:layout_width="380dp"
  119. android:layout_height="54dp"
  120. android:layout_marginTop="7dp"
  121. android:ems="10"
  122. android:hint="请输入密码"
  123. android:inputType="textPassword"
  124. app:layout_constraintEnd_toEndOf="@+id/name"
  125. app:layout_constraintStart_toStartOf="@+id/name"
  126. app:layout_constraintTop_toBottomOf="@+id/name" />
  127. <EditText
  128. android:id="@+id/email"
  129. android:layout_width="380dp"
  130. android:layout_height="51dp"
  131. android:layout_marginTop="9dp"
  132. android:ems="10"
  133. android:hint="邮箱"
  134. android:inputType="textPersonName"
  135. android:visibility="invisible"
  136. app:layout_constraintEnd_toEndOf="parent"
  137. app:layout_constraintStart_toStartOf="parent"
  138. app:layout_constraintTop_toBottomOf="@+id/textView" />

运行结果:

(二)、事件监听应用

编写程序,实现如下功能:

  1. 设计一个应用程序,包含1EditText,1TextView1Button。在EditText输入框中输入银行卡号,输入的同时用较大字体四个一组分隔在TextView显示出来。点击清空button时,所有数据清空。
  1. 要求:(1)使用OnKeyListener监听;
  2. (2)监听到响应事件类型KeyEvent为KeyEvent.ACTION_UP才分割显示;

代码部分:

  1. activity_second.xml
  2. <Button
  3. android:id="@+id/back"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_marginBottom="300dp"
  7. android:backgroundTint="#2196F3"
  8. android:text="返回登录页面"
  9. android:textSize="16sp"
  10. app:layout_constraintBottom_toBottomOf="parent"
  11. app:layout_constraintEnd_toEndOf="parent"
  12. app:layout_constraintStart_toStartOf="parent" />
  13. <Button
  14. android:id="@+id/clear"
  15. android:layout_width="75dp"
  16. android:layout_height="48dp"
  17. android:layout_marginTop="34dp"
  18. android:backgroundTint="#2196F3"
  19. android:text="清空"
  20. android:textSize="16sp"
  21. app:layout_constraintEnd_toEndOf="parent"
  22. app:layout_constraintStart_toStartOf="parent"
  23. app:layout_constraintTop_toBottomOf="@+id/textView2" />
  24. <EditText
  25. android:id="@+id/editTextNumber"
  26. android:layout_width="368dp"
  27. android:layout_height="49dp"
  28. android:layout_marginTop="22dp"
  29. android:ems="10"
  30. android:hint="在此输入银行卡号"
  31. android:inputType="number"
  32. app:layout_constraintEnd_toEndOf="parent"
  33. app:layout_constraintStart_toStartOf="parent"
  34. app:layout_constraintTop_toTopOf="parent" />
  35. <TextView
  36. android:id="@+id/textView2"
  37. android:layout_width="366dp"
  38. android:layout_height="51dp"
  39. android:layout_marginTop="17dp"
  40. android:textSize="26sp"
  41. app:layout_constraintEnd_toEndOf="@+id/editTextNumber"
  42. app:layout_constraintStart_toStartOf="@+id/editTextNumber"
  43. app:layout_constraintTop_toBottomOf="@+id/editTextNumber" />
  44. SecondActivity
  45. public class SecondActivity extends AppCompatActivity {
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_second);
  50. Button button = (Button) findViewById(R.id.back);/*返回按钮*/
  51. button.setOnClickListener(new View.OnClickListener() {/*监听按钮*/
  52. @Override
  53. public void onClick(View view) {
  54. Intent intent = new Intent(SecondActivity.this, MainActivity.class);
  55. startActivity(intent);
  56. }
  57. });
  58. /*实例化*/
  59. Button qk=(Button)findViewById(R.id.clear);
  60. EditText edit=(EditText)findViewById(R.id.editTextNumber);/*编辑框*/
  61. TextView text =(TextView)findViewById(R.id.textView2);/*文本框*/
  62. /*确定按键的监听以及事件处理*/
  63. edit.setOnKeyListener(new View.OnKeyListener() {
  64. @Override
  65. public boolean onKey(View view, int i, KeyEvent keyEvent) {
  66. String number = edit.getText().toString(); //获取编辑框的内容
  67. StringBuffer newnum=new StringBuffer(number);/*字符串内容*/
  68. if(keyEvent.getAction()==KeyEvent.ACTION_UP) {/*如果事件为按下键盘*/
  69. if (number.length() >= 5 || number.length() >= 8 || number.length() >= 13 || number.length() >= 17) {
  70. /*四个为一组分隔*/
  71. if (number.length() >= 5) {
  72. newnum.insert(4, " ");
  73. }
  74. if (number.length() >= 8) {
  75. newnum.insert(9, " ");
  76. }
  77. if (number.length() >= 13) {
  78. newnum.insert(14, " ");
  79. }
  80. if (number.length() >= 17) {
  81. newnum.insert(19, " ");
  82. }
  83. }
  84. }
  85. text.setText(newnum);//设置文本
  86. return false;
  87. }
  88. });
  89. /*清除按键的监听以及事件处理*/
  90. qk.setOnClickListener(new View.OnClickListener() {
  91. @Override
  92. public void onClick(View v) {
  93. TextView textView =(TextView)findViewById(R.id.textView2);
  94. textView.setText(null); //设置空文本
  95. EditText editText=(EditText)findViewById(R.id.editTextNumber);
  96. editText.setText(null);
  97. }
  98. });
  99. }
  100. }

运行结果:

(三)、Spinner控件应用

编写程序,实现如下功能:

  1. UI界面由1EditText2个按钮、1Spinner控件组成。当用户EditText中输入内容,点击“添加”按钮,能够将其动态添加到Spinner中;当用户在Spinner列表中点击某项时,选项文本内容显示在EditText中, 如果点击“删除”按钮,能够将该项从Spinner项中删除。为Spinner添加事件,使用Toast显示选择内容

代码部分:

  1. activity_third.xml
  2. <EditText
  3. android:id="@+id/sr"
  4. android:layout_width="383dp"
  5. android:layout_height="54dp"
  6. android:layout_marginTop="16dp"
  7. android:ems="10"
  8. android:hint="请输入"
  9. android:inputType="textPersonName"
  10. app:layout_constraintEnd_toEndOf="parent"
  11. app:layout_constraintStart_toStartOf="parent"
  12. app:layout_constraintTop_toTopOf="parent" />
  13. <Spinner
  14. android:id="@+id/spinner2"
  15. android:layout_width="383dp"
  16. android:layout_height="48dp"
  17. android:layout_marginTop="20dp"
  18. app:layout_constraintEnd_toEndOf="@+id/sr"
  19. app:layout_constraintHorizontal_bias="0.0"
  20. app:layout_constraintStart_toStartOf="@+id/sr"
  21. app:layout_constraintTop_toBottomOf="@+id/sr" />
  22. <Button
  23. android:id="@+id/add"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_marginStart="80dp"
  27. android:layout_marginTop="64dp"
  28. android:text="添加"
  29. app:layout_constraintStart_toStartOf="parent"
  30. app:layout_constraintTop_toBottomOf="@+id/spinner2" />
  31. <Button
  32. android:id="@+id/back666"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_marginTop="152dp"
  36. android:text="返回"
  37. app:layout_constraintEnd_toEndOf="parent"
  38. app:layout_constraintStart_toStartOf="parent"
  39. app:layout_constraintTop_toBottomOf="@+id/spinner2" />
  40. <Button
  41. android:id="@+id/delete"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_marginTop="64dp"
  45. android:text="删除"
  46. app:layout_constraintEnd_toEndOf="parent"
  47. app:layout_constraintHorizontal_bias="0.458"
  48. app:layout_constraintStart_toEndOf="@+id/add"
  49. app:layout_constraintTop_toBottomOf="@+id/spinner2" />
  50. ThirdActivity
  51. public class ThirdActivity extends AppCompatActivity {
  52. private Spinner spinner;//声明这个Spinner控件
  53. private List lesson; //定义一个String类型的List数组作为数据源
  54. private ArrayAdapter adapter; //定义一个ArrayAdapter适配器作为spinner的数据适配器
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.activity_third);
  59. spinner = (Spinner) findViewById(R.id.spinner2);
  60. Button add = (Button) findViewById(R.id.add);/*添加按钮*/
  61. Button dele = (Button) findViewById(R.id.delete);/*删除按钮*/
  62. Button skip6=(Button) findViewById(R.id.back666);/*跳转按钮*/
  63. final EditText shuru=findViewById(R.id.sr);
  64. skip6.setOnClickListener(new View.OnClickListener() {/*监听按钮*/
  65. @Override
  66. public void onClick(View view) {
  67. Intent intent6 = new Intent(ThirdActivity.this, SecondActivity.class);
  68. startActivity(intent6);
  69. }
  70. });
  71. lesson = new ArrayList();/*列表数组*/
  72. lesson.add("");
  73. lesson.add("数字图像处理");
  74. lesson.add("Maya基础");
  75. lesson.add("Android软件开发");
  76. adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,lesson);
  77. /*绑定列表*/ adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  78. //为适配器设置下拉列表下拉时的菜单样式
  79. spinner.setAdapter(adapter);/*绑定适配器*/
  80. add.setOnClickListener(new View.OnClickListener() {/*监听添加按钮*/
  81. @Override
  82. public void onClick(View view) {
  83. String lan= shuru.getText().toString(); //获取编辑框的内容
  84. Toast addok=Toast.makeText(ThirdActivity.this,"成功添加 "+shuru.getText().toString(),Toast.LENGTH_LONG);
  85. addok.setGravity(Gravity.CENTER,0,0);
  86. addok.show();
  87. lesson.add(lan);
  88. }
  89. });
  90. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  91. @Override
  92. public void onNothingSelected(AdapterView<?> adapterView) {
  93. }/*选择为空*/
  94. @Override
  95. //选中项监听
  96. public void onItemSelected(AdapterView parent, View view, int position, long id) {
  97. shuru.setText(spinner.getSelectedItem().toString());
  98. /*设置编辑框为选中文字*/
  99. Toast choose=Toast.makeText(ThirdActivity.this,"你已选中 "+spinner.getSelectedItem().toString(),Toast.LENGTH_LONG);
  100. choose.setGravity(Gravity.CENTER,0,0);
  101. choose.show();
  102. dele.setOnClickListener(new View.OnClickListener() {
  103. /*删除键监听*/
  104. @Override
  105. public void onClick(View view) {
  106. String del= spinner.getSelectedItem().toString();
  107. //获取编辑框的内容
  108. Toast chosedel=Toast.makeText(ThirdActivity.this,"成功删除 "+spinner.getSelectedItem().toString(),Toast.LENGTH_LONG);
  109. chosedel.setGravity(Gravity.CENTER,0,0);
  110. chosedel.show();
  111. lesson.remove(del);
  112. shuru.setText(null);/*清空编辑框*/
  113. spinner.setSelection(0);/*设置选择为空*/
  114. }
  115. });
  116. }
  117. });
  118. }
  119. }

运行结果:


本文转载自: https://blog.csdn.net/qq_59343209/article/details/127297902
版权归原作者 酒精过敏w 所有, 如有侵权,请联系我们删除。

“Android开发实验-UI控件及事件响应”的评论:

还没有评论