Android项目逻辑。
一,画ui。
二,根据ui写逻辑,写出布局代码。
?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_username"
android:hint="用户名"/>
<EditText
android:layout_marginTop="10dp"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
android:hint="密码"/>
<RelativeLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb_ischeck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
<Button
android:layout_marginRight="20dp"
android:id="@+id/login"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</RelativeLayout>
</LinearLayout>
三,写事件处理。
package com.tjgx.sharedpreferences;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
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 username,password; //账号密码编辑框
private CheckBox checkBox; //记住密码框
private Button login; //登录按钮
private SharedPreferences sp; //保存用的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp =getSharedPreferences("config",0); //SharedPreferences初始化
initview();
}
private void initview() {
//找到需要控件。
username = findViewById(R.id.et_username);
password = findViewById(R.id.et_password);
checkBox = findViewById(R.id.cb_ischeck);
login = findViewById(R.id.login);
login.setOnClickListener(new MyonclickListener());
//从保存的文件中取出账号密码
String s1=sp.getString("name","");
String s2=sp.getString("pwd","");
username.setText(s1);
password.setText(s2);
}
class MyonclickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
String name = username.getText().toString().trim();
String pwd = password.getText().toString().trim();
if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
// Sharedpreferences.putString(MainActivity.this,name,pwd);
Log.v("log","登录失败");
Toast.makeText(MainActivity.this,"账号或密码为空",Toast.LENGTH_LONG).show();
}else{
Log.e("login","登录成功"+name+pwd);
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();
if (checkBox.isChecked()){
SharedPreferences.Editor edit = sp.edit();
edit.putString("name",name);
edit.putString("pwd",pwd);
edit.apply();
}
}
}
}
}
四,打包程序。
项目中SharedPreferences是Android提供的保存数据的。
使用方法分4步:
1,获取实例对象
sp =getSharedPreferences("config",0); //SharedPreferences初始化
2.获取编辑器
SharedPreferences.Editor edit = sp.edit();
3.存数据
edit.putString("name",name);
edit.putString("pwd",pwd);
4.应用生效
edit.apply();
当你把记住密码勾上之后,下次进入记录上次数据。