今天主要是跟大家分享一个简单的手机本地密码登录模块,包括用户和密码的注册,登录和修改等主要功能;实现原理十分简单,主要运用到了SharedPreferences存储技术作为密码用户信息的保存。
模块演示图片
Screenshot_20200512-213654.jpg Screenshot_20200512-213658.jpg Screenshot_20200512-213706.jpg实现代码
LoginActivity(登录)
主要是实现软件的登录和注册判断跳转逻辑。
public class LoginActivity extends AppCompatActivity {
EditText lg_user;
EditText lg_password;
Button lg_login;
Button registered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
SharedPreferences pref=getSharedPreferences("yonghu",MODE_PRIVATE);
String text=pref.getString("user","");
lg_user=(EditText) findViewById(R.id.lg_user);
lg_user.setText(text);
lg_password=(EditText) findViewById(R.id.lg_password);
lg_login=(Button)findViewById(R.id.login);
lg_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref=getSharedPreferences("yonghu",MODE_PRIVATE);
String text01=pref.getString("user","");
String text02=pref.getString("password","");
if(text01.equals("")==false&&text01.equals(lg_user.getText().toString())==true&&
text02.equals("")==false&&text02.equals(lg_password.getText().toString())==true){
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
else{
Toast.makeText(LoginActivity.this, "账户或密码错误", Toast.LENGTH_SHORT)
.show();
lg_password.setText("");
}
}
});
registered=(Button)findViewById(R.id.lg_registered);
registered.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref=getSharedPreferences("yonghu",MODE_PRIVATE);
String text01=pref.getString("user","");
String text02=pref.getString("password","");
if(text01.equals("")==true){
Intent intent=new Intent(LoginActivity.this,RegisteredActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
else{
Toast.makeText(LoginActivity.this, "已拥有账户,您可以修改密码", Toast.LENGTH_SHORT)
.show();
Intent intent=new Intent(LoginActivity.this,ChangeActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
activity_login(登录界面)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@drawable/color_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户:"/>
<EditText
android:id="@+id/lg_user"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:id="@+id/lg_password"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
<Button
android:id="@+id/lg_registered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
</LinearLayout>
RegisteredActivity(注册)
主要是实现软件的注册用户相关的逻辑。
public class RegisteredActivity extends AppCompatActivity {
EditText user;
EditText password01;
EditText password02;
Button registered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registered);
user=(EditText) findViewById(R.id.rg_user);
password01=(EditText) findViewById(R.id.rg_password01);
password02=(EditText) findViewById(R.id.rg_password02);
registered=(Button)findViewById(R.id.rg_registered);
registered.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text01 = user.getText().toString();
String text02 = password01.getText().toString();
String text03 = password02.getText().toString();
if(text01.equals("")==true){
Toast.makeText(RegisteredActivity.this, "账户不能为空", Toast.LENGTH_SHORT)
.show();
}else if(text02.equals("")==true) {
Toast.makeText(RegisteredActivity.this, "密码不能为空", Toast.LENGTH_SHORT)
.show();
}else if (text02.equals(text03) == false) {
Toast.makeText(RegisteredActivity.this, "密码不匹配,请重新输入密码", Toast.LENGTH_SHORT)
.show();
password02.setText("");
}else if (text02.equals(text03) == true){
SharedPreferences.Editor editor=getSharedPreferences("yonghu",
MODE_PRIVATE).edit();
editor.putString("user",text01);
editor.putString("password",text02);
editor.apply();
Log.d("RegisteredActivity","user is"+text01);
Log.d("RegisteredActivity","password is"+text02);
Toast.makeText(RegisteredActivity.this, "注册成功,请登录系统", Toast.LENGTH_SHORT)
.show();
Intent intent=new Intent(RegisteredActivity.this,LoginActivity.class);
startActivity(intent);
RegisteredActivity.this.finish();
}
}
});
}
}
Activity_Registered(注册界面)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@drawable/color_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册用户:"/>
<EditText
android:id="@+id/rg_user"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册密码:"/>
<EditText
android:id="@+id/rg_password01"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"/>
<EditText
android:id="@+id/rg_password02"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/rg_registered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
</LinearLayout>
ChangeActivity(修改用户信息)
public class ChangeActivity extends AppCompatActivity {
EditText user01;
EditText user02;
EditText password01;
EditText password02;
EditText password03;
Button registered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
SharedPreferences pref=getSharedPreferences("yonghu",MODE_PRIVATE);
String text=pref.getString("user","");
user01=(EditText) findViewById(R.id.cg_user01);
user01.setText(text);
user02=(EditText) findViewById(R.id.cg_user02);
password01=(EditText) findViewById(R.id.cg_password01);
password02=(EditText) findViewById(R.id.cg_password02);
password03=(EditText) findViewById(R.id.cg_password03);
registered=(Button)findViewById(R.id.cg_registered);
registered.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text01 = user01.getText().toString();
String text02 = user02.getText().toString();
String text11 = password01.getText().toString();
String text12 = password02.getText().toString();
String text13 = password03.getText().toString();
SharedPreferences pref=getSharedPreferences("yonghu",MODE_PRIVATE);
String zhanghu=pref.getString("user","");
String mima=pref.getString("password","");
if(text01.equals(zhanghu)&&text11.equals(mima)){
if(text02.equals("")==true){
Toast.makeText(ChangeActivity.this, "账户不能为空", Toast.LENGTH_SHORT)
.show();
}else if(text12.equals("")==true){
Toast.makeText(ChangeActivity.this, "密码不能为空", Toast.LENGTH_SHORT)
.show();
}else if(text12.equals(text13)==true&&text02.equals("")==false){
SharedPreferences.Editor editor=getSharedPreferences("yonghu",
MODE_PRIVATE).edit();
editor.putString("user",text02);
editor.putString("password",text12);
editor.apply();
Log.d("RegisteredActivity","user is"+text02);
Log.d("RegisteredActivity","password is"+text12);
Toast.makeText(ChangeActivity.this, "修改成功,请登录系统", Toast.LENGTH_SHORT)
.show();
Intent intent=new Intent(ChangeActivity.this,LoginActivity.class);
startActivity(intent);
ChangeActivity.this.finish();
}else if(text12.equals(text13)==false){
Toast.makeText(ChangeActivity.this, "密码输入错误", Toast.LENGTH_SHORT)
.show();
password03.setText("");
}
}else{
Toast.makeText(ChangeActivity.this, "原账户或密码错误", Toast.LENGTH_SHORT)
.show();
password01.setText("");
}
}
});
}
}
Activity_Change(修改信息界面)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@drawable/color_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原用户:"/>
<EditText
android:id="@+id/cg_user01"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原密码:"/>
<EditText
android:id="@+id/cg_password01"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新的用户:"/>
<EditText
android:id="@+id/cg_user02"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新的密码:"/>
<EditText
android:id="@+id/cg_password02"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"/>
<EditText
android:id="@+id/cg_password03"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/cg_registered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认修改"/>
</LinearLayout>
</LinearLayout>
登录成功
登录成功的逻辑和页面代码就不展示了,随意一个空白或者软件窗口都可以使用。
总结
这只是一个特别简单实现的登录模块,只是用到了简单的存储方式,实现起来不会费很大的劲,对于接触过安卓的朋友们可以一起探讨一下,如果觉得有什么需要改进的地方可以向我提一下。后续会继续更新关于android开发的一些经验。
网友评论