1首先我们创建3个activity,登录页面,欢迎页面,主页面。
欢迎页面:
package com.example.class7;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Login_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText usernameText=findViewById(R.id.editTextTextPersonName);
EditText passwordText=findViewById(R.id.editTextTextPersonName2);
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=usernameText.getText().toString();
String password=passwordText.getText().toString();
if(username.equals("crq")&&password.equals("123456")){
//todo
SharedPreferences sharedPreferences=getSharedPreferences("user1",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("username",username);
editor.putBoolean("islogin",true);
editor.commit();
Intent intent=new Intent(Login_Activity.this,MainActivity.class);
startActivity(intent);
}
else{
Toast.makeText(Login_Activity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}
package com.example.class7;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class Welcom_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcom);
//等待几秒钟创建线程
Thread thread=new Thread()
{
@Override
public void run() {
super.run();
try {
Thread.sleep(5000);
SharedPreferences sharedPreferences = getSharedPreferences("user1", MODE_PRIVATE);
Boolean isLogin=sharedPreferences.getBoolean("islogin",false);
if(isLogin)
{
Intent intent=new Intent(Welcom_Activity.this,MainActivity.class);
startActivity(intent);
}
else{
Intent intent=new Intent(Welcom_Activity.this,Login_Activity.class);
startActivity(intent);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)