• AndroidStudio案例——登录页面的切换


    简单的登录页面

    【效果展示】

     

    xml代码】

    1. "1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity"
    8. android:orientation="vertical"
    9. android:padding="10dp"
    10. >
    11. <RadioGroup
    12. android:id="@+id/login"
    13. android:orientation="horizontal"
    14. android:layout_width="wrap_content"
    15. android:layout_height="wrap_content"
    16. android:paddingLeft="15dp">
    17. <RadioButton
    18. android:id="@+id/password_login"
    19. android:layout_width="wrap_content"
    20. android:layout_height="wrap_content"
    21. android:checked="true"
    22. android:text="密码登录"
    23. android:textSize="18dp"
    24. />
    25. <RadioButton
    26. android:id="@+id/yzm_login"
    27. android:layout_width="wrap_content"
    28. android:layout_height="wrap_content"
    29. android:layout_marginStart="80dp"
    30. android:text="验证码登录"
    31. android:textSize="18dp"
    32. />
    33. RadioGroup>
    34. <LinearLayout
    35. android:id="@+id/phone"
    36. android:orientation="horizontal"
    37. android:layout_width="match_parent"
    38. android:layout_height="wrap_content"
    39. android:gravity="center"
    40. android:layout_marginTop="10dp"
    41. android:layout_below="@+id/login">
    42. <TextView
    43. android:layout_width="wrap_content"
    44. android:layout_height="wrap_content"
    45. android:text="手机号码:"
    46. android:textSize="20dp" />
    47. <EditText
    48. android:layout_width="270dp"
    49. android:layout_height="50dp"
    50. android:singleLine="true"
    51. android:hint="请输入手机号"
    52. android:background="@drawable/editext_selector"
    53. />
    54. LinearLayout>
    55. <LinearLayout
    56. android:id="@+id/password_view"
    57. android:orientation="horizontal"
    58. android:layout_width="match_parent"
    59. android:layout_height="wrap_content"
    60. android:gravity="center"
    61. android:layout_marginTop="10dp"
    62. android:layout_below="@+id/phone">
    63. <TextView
    64. android:id="@+id/password_txt"
    65. android:layout_width="wrap_content"
    66. android:layout_height="wrap_content"
    67. android:text="登录密码:"
    68. android:textSize="20dp" />
    69. <RelativeLayout
    70. android:layout_width="wrap_content"
    71. android:layout_height="wrap_content">
    72. <EditText
    73. android:id="@+id/password_value"
    74. android:layout_width="160dp"
    75. android:layout_height="50dp"
    76. android:singleLine="true"
    77. android:hint="请输入密码"
    78. android:background="@drawable/editext_selector"
    79. tools:ignore="MissingConstraints" />
    80. <Button
    81. android:id="@+id/login_yzm"
    82. android:layout_width="110dp"
    83. android:layout_height="50dp"
    84. android:layout_toEndOf="@id/password_value"
    85. android:text="登录"
    86. android:background="@color/colorPrimaryDark"/>
    87. RelativeLayout>
    88. LinearLayout>
    89. <CheckBox
    90. android:id="@+id/btn_forget"
    91. android:layout_width="wrap_content"
    92. android:layout_height="wrap_content"
    93. android:text="记住密码"
    94. android:layout_below="@id/password_view"/>
    95. <Button
    96. android:layout_width="match_parent"
    97. android:layout_height="60dp"
    98. android:layout_below="@+id/btn_forget"
    99. android:gravity="center"
    100. android:text="登录" />
    101. RelativeLayout>

     【Java代码】

    1. package com.example.a10133;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.text.InputType;
    5. import android.view.View;
    6. import android.widget.Button;
    7. import android.widget.CheckBox;
    8. import android.widget.EditText;
    9. import android.widget.RadioGroup;
    10. import android.widget.TextView;
    11. public class MainActivity extends AppCompatActivity {
    12. TextView password_txt;
    13. EditText password_value;
    14. Button login_yzm;
    15. RadioGroup login;
    16. CheckBox btn_forget;
    17. @Override
    18. protected void onCreate(Bundle savedInstanceState) {
    19. super.onCreate(savedInstanceState);
    20. //加载布局
    21. setContentView(R.layout.activity_main);
    22. //获取控件
    23. // 密码登录
    24. password_txt = findViewById(R.id.password_txt);
    25. // 输入的密码框
    26. password_value = findViewById(R.id.password_value);
    27. // 登录及获取验证码按钮
    28. login_yzm = findViewById(R.id.login_yzm);
    29. // 忘记密码按钮
    30. btn_forget = findViewById(R.id.btn_forget);
    31. // 选择两种登录方式
    32. login = findViewById(R.id.login);
    33. //单选按钮组绑定监听器
    34. login.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    35. @Override
    36. public void onCheckedChanged(RadioGroup group, int checkedId) {
    37. if (checkedId == R.id.password_login) {
    38. password_txt.setText("登录密码:");
    39. password_value.setHint("请输入密码");
    40. login_yzm.setText("登录");
    41. btn_forget.setVisibility(View.VISIBLE);
    42. } else if (checkedId == R.id.yzm_login) {
    43. password_txt.setText(" 验证码:");
    44. password_value.setHint("请输入验证码");
    45. login_yzm.setText("获取验证码");
    46. btn_forget.setVisibility(View.INVISIBLE);
    47. }
    48. }
    49. });
    50. }
    51. }

    附件

    drawable中

    【editext_selector.xml代码】

    1. "1.0" encoding="utf-8"?>
    2. <selector
    3. xmlns:android="http://schemas.android.com/apk/res/android">
    4. <item android:state_focused="true"
    5. android:drawable="@drawable/shape_edit_focus"/>
    6. <item android:drawable="@drawable/shape_edit_normal"/>
    7. selector>

    【shape_edit_focus.xml代码】

    1. "1.0" encoding="utf-8"?>
    2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    3. <solid android:color="#ffffff" />
    4. <stroke
    5. android:width="1dp"
    6. android:color="#ff0000ff" />
    7. <corners
    8. android:bottomLeftRadius="5dp"
    9. android:bottomRightRadius="5dp"
    10. android:topLeftRadius="5dp"
    11. android:topRightRadius="5dp" />
    12. <padding
    13. android:bottom="2dp"
    14. android:left="2dp"
    15. android:right="2dp"
    16. android:top="2dp" />
    17. shape>

    【shape_edit_normal.xml代码】

    1. "1.0" encoding="utf-8"?>
    2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    3. <solid android:color="#ffffff" />
    4. <stroke
    5. android:width="1dp"
    6. android:color="#ffaaaaaa" />
    7. <corners
    8. android:bottomLeftRadius="5dp"
    9. android:bottomRightRadius="5dp"
    10. android:topLeftRadius="5dp"
    11. android:topRightRadius="5dp" />
    12. <padding
    13. android:bottom="2dp"
    14. android:left="2dp"
    15. android:right="2dp"
    16. android:top="2dp" />
    17. shape>

    values中

    【colors.xml添加的代码】

    1. <color name="colorPrimary">#008577color>
    2. <color name="colorPrimaryDark">#00574Bcolor>
    3. <color name="colorAccent">#D81B60color>

    【themes.xml添加的代码】

    1. <item name="colorPrimary">@color/colorPrimaryitem>
    2. <item name="colorAccent">@color/colorAccentitem>

  • 相关阅读:
    《深入理解java虚拟机 第三版》学习笔记二
    类加载与类文件结构
    解决:华为ensp软件中AR和AC,AP设备无法启动报错“40”的问题
    2022-45~46周(10.24-11.06) 项目问题整理
    故障009:改写多表关联同时更新且互换列值
    Windows 11可使AMD芯片性能下降15%
    jq获取和设置偏移量:offset()、position()
    Uniapp语言切换动态修改Js文件
    官宣|Apache Flink 1.15 发布公告
    【新知实验室 认识TRTC+四步跑通音视频demo】
  • 原文地址:https://blog.csdn.net/m0_52177571/article/details/127447444