学习记录(3.16)
学习时长:6h
代码行数:约294行
今天主要完成了登陆界面的优化,可以在一个页面进入账号密码登录和验证码登录两种登陆方式,并且可以独立显示和进行。在此之后完成了忘记密码后的密码重置。
因为今天要给社团中的大一新生上乐理课,所以还制作了相应的ppt占用了不少时间,所以并没有编写太多代码,明天继续加油
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <RadioGroup android:id= "@+id/rg_Login" android:layout_width= "match_parent" android:layout_height= "@dimen/item_layout_height" android:orientation= "horizontal" > <RadioButton android:id= "@+id/rb_password" android:layout_width= "0dp" android:layout_height= "match_parent" android:layout_weight= "1" android:text= "密码登录" android:textSize= "@dimen/common_font_size" android:checked= "true" /> <RadioButton android:id= "@+id/rb_verifycode" android:layout_width= "0dp" android:layout_height= "match_parent" android:layout_weight= "1" android:text= "验证码登录" android:textSize= "@dimen/common_font_size" /> </RadioGroup> <LinearLayout android:layout_width= "match_parent" android:layout_height= "@dimen/item_layout_height" android:orientation= "horizontal" > <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "手机号" android:textSize= "30dp" /> <EditText android:layout_width= "match_parent" android:layout_height= "match_parent" android:id= "@+id/a_phone" android:hint= "请输入手机号" android:inputType= "number" /> </LinearLayout> <LinearLayout android:layout_width= "match_parent" android:layout_height= "@dimen/item_layout_height" android:orientation= "horizontal" > <TextView android:id= "@+id/tv_password" android:layout_width= "wrap_content" android:layout_height= "match_parent" android:text= "密 码" android:textSize= "30dp" /> <RelativeLayout android:layout_width= "0dp" android:layout_height= "match_parent" android:layout_weight= "1" > <EditText android:layout_width= "match_parent" android:layout_height= "match_parent" android:hint= "请输入密码" android:id= "@+id/a_password" android:inputType= "textPassword" /> <Button android:id= "@+id/btn_forget" android:layout_width= "wrap_content" android:layout_height= "match_parent" android:layout_alignParentEnd= "true" android:text= "忘记密码" android:background= "@color/teal_200" /> </RelativeLayout> </LinearLayout> <CheckBox android:id= "@+id/ck_remember" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "保存密码" /> <LinearLayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" android:gravity= "center" > <Button android:id= "@+id/iv_denglu" android:layout_width= "200dp" android:layout_height= "wrap_content" android:text= "登录" android:textSize= "40dp" android:background= "@color/teal_200" /> <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= " " /> <Button android:id= "@+id/zhuceb" android:layout_width= "200dp" android:layout_height= "wrap_content" android:text= "注册" android:textSize= "40dp" android:background= "@color/teal_200" /> </LinearLayout> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | package com.example.myapplication1; import static com.example.myapplication1.R.id.a_password; import static com.example.myapplication1.R.id.iv_denglu; import static com.example.myapplication1.R.id.rg_Login; import static com.example.myapplication1.R.id.tv_password; import static com.example.myapplication1.R.id.zc_phone; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import com.google.android.material.internal.ViewUtils; import java.text.BreakIterator; import java.util.Random; import java.util.concurrent.atomic.AtomicReference; public class Main3Activity extends AppCompatActivity implements View.OnFocusChangeListener, RadioGroup.OnCheckedChangeListener, View.OnClickListener { private EditText a_phone; private TextView tv_password; private EditText a_password; private Button btn_forget; private CheckBox ck_remember; private RadioButton rb_password; private RadioButton rb_verifycode; private ActivityResultLauncher<Intent> regist; private Button iv_denglu; private String mpassword= "20214121" ; private String verifyCode; private SharedPreferences preferences; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main3); RadioGroup rg_login = findViewById(rg_Login); tv_password = findViewById(R.id.tv_password); a_password = findViewById(R.id.a_password); btn_forget = findViewById(R.id.btn_forget); ck_remember = findViewById(R.id.ck_remember); rb_password = findViewById(R.id.rb_password); rb_verifycode = findViewById(R.id.rb_verifycode); rg_login.setOnCheckedChangeListener( this ); btn_forget.setOnClickListener( this ); regist = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { Intent intent =result.getData(); if (intent!= null && result.getResultCode() == Activity.RESULT_OK){ mpassword=intent.getStringExtra( "new_password" ); } } }); a_phone = findViewById(R.id.a_phone); a_password.setOnFocusChangeListener( this ); Button button = findViewById(R.id.zhuceb); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(Main3Activity. this , Zhuce. class ); startActivity(intent); } }); preferences = getSharedPreferences( "confine" , Context.MODE_PRIVATE); iv_denglu = findViewById(R.id.iv_denglu); iv_denglu.setOnClickListener( this ); reload(); } public void reload(){ boolean isremember = preferences.getBoolean( "remember" , false ); if (isremember){ String phone = preferences.getString( "phone" , "" ); a_phone.setText(phone); String password = preferences.getString( "password" , "" ); a_password.setText(password); } } @Override public void onFocusChange(View view, boolean b) { if (b){ String phone = a_phone.getText().toString(); if (TextUtils.isEmpty(phone) || phone.length()< 11 ){ a_phone.requestFocus(); Toast.makeText( this , "请输入11位手机号码" , Toast.LENGTH_SHORT).show(); } } } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.rb_password: tv_password.setText( "密 码" ); a_password.setText( "请输入六位密码" ); btn_forget.setText( "忘记密码" ); ck_remember.setVisibility(View.VISIBLE); break ; case R.id.rb_verifycode: tv_password.setText( "验证码" ); a_password.setText( "请输入验证码" ); btn_forget.setText( "获取验证码" ); ck_remember.setVisibility(View.GONE); break ; } } @Override public void onClick(View view) { String phone= a_phone.getText().toString(); switch (view.getId()){ case R.id.btn_forget: if (phone.length()< 11 ){ Toast.makeText( this , "请输入11位手机号" ,Toast.LENGTH_SHORT).show(); return ; } if (rb_password.isChecked()){ Intent intent = new Intent( this , Forget. class ); intent.putExtra( "phone" , phone); regist.launch(intent); } else if (rb_verifycode.isChecked()){ verifyCode = String.format( "%06d" , new Random().nextInt( 999999 )); AlertDialog.Builder builder = new AlertDialog.Builder( this ); builder.setTitle( "请记住验证码" ); builder.setMessage( "尊敬的用户" +phone+ "您本次的验证码为" + verifyCode); builder.setPositiveButton( "确认" , null ); AlertDialog dialog = builder.create(); dialog.show(); } break ; case R.id.iv_denglu: if (rb_password.isChecked()){ if (!mpassword.equals(a_password.getText().toString())){ Toast.makeText( this , "请输入正确的密码" ,Toast.LENGTH_SHORT).show(); return ; } loginSuccess(); } else if (rb_verifycode.isChecked()){ if (!verifyCode.equals(a_password.getText().toString())){ Toast.makeText( this , "请输入正确的验证码" ,Toast.LENGTH_SHORT).show(); return ; } loginSuccess(); } } } private void loginSuccess() { Intent intent = new Intent(); intent.setClass(Main3Activity. this , Menu. class ); startActivity(intent); if (ck_remember.isChecked()) { SharedPreferences.Editor editor = preferences.edit(); editor.putString( "phone" ,a_phone.getText().toString()); editor.putString( "password" ,a_password.getText().toString()); editor.putBoolean( "remember" ,ck_remember.isChecked()); editor.commit(); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用