代码记录
Posted pomodoro
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码记录相关的知识,希望对你有一定的参考价值。
activity_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout1"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="账号:" />
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_below="@id/layout1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="密码:" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_below="@id/layout2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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:textSize="18sp"
android:text="记住密码" />
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_below="@id/layout3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#FF4081"
android:text="登录" />
<Button
android:id="@+id/register"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textSize="16sp"
android:textColor="#928E8E"
android:text="注册" />
</RelativeLayout>
loginActivity.java:
package com.example.bizarredictionary;
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 LoginActivity extends BaseActivity implements View.OnClickListener
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private Button login, register;
private CheckBox rememberPass;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pref = PreferenceManager.getDefaultSharedPreferences(this); //获取SharedPreferences对象,自动使用当前应用程序的包名作为前缀来命名SharedPreferences文件。
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
rememberPass = (CheckBox) findViewById(R.id.remember_pass);
Button login = findViewById(R.id.login);
Button regidter = findViewById(R.id.register);
login.setOnClickListener(this);
regidter.setOnClickListener(this);
boolean isRemember = pref.getBoolean("remember_password", false);
boolean iskeep = pref.getBoolean("keep_login", false);
if (iskeep)
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
if (isRemember)
// 将账号和密码都设置到文本框中
String account = pref.getString("account", "");
String password = pref.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
@Override
public void onClick(View v)
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
switch(v.getId())
case R.id.login:
// 如果账号是admin且密码是123456,就认为登录成功
if (account.equals("admin") && password.equals("123456"))
editor = pref.edit();
if (rememberPass.isChecked()) // 检查复选框是否被选中
editor.putBoolean("remember_password", true);
editor.putString("account", account);
editor.putString("password", password);
else
editor.clear();
editor.putBoolean("keep_login", true);
editor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
else
Toast.makeText(LoginActivity.this, "account or password is invalid",
Toast.LENGTH_SHORT).show();
case R.id.register:
//if (account.equals(db.))
break;
以上是关于代码记录的主要内容,如果未能解决你的问题,请参考以下文章