如何写一个简单的登录
这里我们采用写固定的账户名为:admin , 密码为12345.真正的代码肯定不能够写固定的。切记!
界面如下:
以下为Layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff" tools:context=".MainActivity"> <EditText android:id="@+id/edt_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="20dp" android:layout_marginTop="10dp" android:textColorHint="#707070" android:hint="账户名.."/> <EditText android:id="@+id/edt_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:layout_marginTop="10dp" android:textSize="20dp" android:textColorHint="#707070" android:hint="密码.."/> <LinearLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/remember_pass" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:textColor="#000000" android:layout_marginLeft="10dp" android:textSize="20dp"/> </LinearLayout> <Button android:id="@+id/login" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:textSize="20dp" android:textAllCaps="false"/> </LinearLayout>
接着我们来写主代码:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText edt_account,edt_password;
private CheckBox rememberPassword;
private Button login;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = PreferenceManager.getDefaultSharedPreferences(this);
edt_account = (EditText)findViewById(R.id.edt_account);
edt_password = (EditText)findViewById(R.id.edt_password);
rememberPassword = (CheckBox)findViewById(R.id.remember_pass);
login = (Button)findViewById(R.id.login);
boolean isRemember = pref.getBoolean("remember_password",false);
if(isRemember){
String account = pref.getString("account","");
String password = pref.getString("password","");
edt_account.setText(account);
edt_password.setText(password);
rememberPassword.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = edt_account.getText().toString();
String password = edt_password.getText().toString();
if(account.equals("admin")&&password.equals("12345")){
editor = pref.edit();
if(rememberPassword.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("account",account);
editor.putString("password",password);
}else{ editor.clear();
}
editor.apply();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
finish();
} else{
Toast.makeText(MainActivity.this, "Not right!", Toast.LENGTH_SHORT).show();
}
} });
}
}搞定!
点击查看更多内容
以上内容来自于网络,如有侵权联系即删除
相关文章
- Markdwon的特殊用法(二)插入图片
- Tensorflow安装使用
- 牛x!一个比传统数据库快 100-1000 倍的数据库!
- 如何让你编辑的东西可以在关闭软件,重新启动时也不会丢失?
- spring-boot-route(十六)使用logback生产日志文件
- 运行快应用rpk文件只需要3分钟4个步骤?
- Git 基本应用
- SQL如何添加数据?
上一篇: 【6】进大厂必须掌握的面试题-Java面试-Hibernate
下一篇: Java 集合看这一篇就够了