LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private Button login;
private EditText username;
private EditText password;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private CheckBox remember;
private String usernamevalue;
private String passwordvalue;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pref = PreferenceManager.getDefaultSharedPreferences(this);
username = (EditText) findViewById(R.id.e_username);
password = (EditText) findViewById(R.id.e_password);
login = (Button) findViewById(R.id.login);
remember = (CheckBox) findViewById(R.id.checkbox);
boolean isremember = pref.getBoolean("remember", false);
if (isremember) {//若选择了记住密码,则下次登陆直接进入登陆后页面
remember.setChecked(true);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
Log.e("LoginActivity", "自动登陆");
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
usernamevalue = username.getText().toString();
passwordvalue = password.getText().toString();
if (usernamevalue.equals("admin") && passwordvalue.equals("123456")) {
editor = pref.edit();
if (remember.isChecked()) {
editor.putBoolean("remember", true);
editor.putString("username", usernamevalue);
editor.putString("password", passwordvalue);
} else {
editor.clear();
}
editor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "username or password is invalid", Toast.LENGTH_SHORT).show();
}
}
});
}
}
acitivity_login.xml
<?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"
tools:context="com.example.period.password.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_marginTop="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UserName"
android:layout_weight="4"
android:gravity="center"
/>
<EditText
android:id="@+id/e_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="15dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_weight="4"
android:gravity="center"/>
<EditText
android:id="@+id/e_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="15dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remember password"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.java //登陆成功后跳转的页面
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="welcome!"/>
网友评论