android studio登录成功并跳转页面源码
时间: 2023-05-08 07:02:15 浏览: 146
首先,我们需要在Android Studio中创建一个新的项目,选择Empty Activity作为模板。接着,我们可以打开activity_main.xml文件,在布局中添加一个登录表单,包括用户名和密码输入框以及一个登录按钮。
当用户点击登录按钮时,我们需要在MainActivity.java中编写代码来验证用户输入的用户名和密码是否正确。如果验证成功,则我们可以通过Intent对象来启动另一个Activity,例如HomeActivity。
此外,在HomeActivity中,我们还需要添加几个控件,例如一个欢迎语和一个退出按钮,以方便用户浏览应用程序。
下面是一个简单的示例代码:
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private EditText etUsername, etPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取输入框和按钮控件
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
btnLogin = findViewById(R.id.btn_login);
// 设置按钮点击事件
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 验证用户名和密码是否正确
if (etUsername.getText().toString().equals("admin") &&
etPassword.getText().toString().equals("123456")) {
// 登录成功,启动HomeActivity
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
} else {
// 登录失败,弹出提示框
Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
HomeActivity.java:
```java
public class HomeActivity extends AppCompatActivity {
private TextView tvWelcome;
private Button btnLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// 获取欢迎语和退出按钮控件
tvWelcome = findViewById(R.id.tv_welcome);
btnLogout = findViewById(R.id.btn_logout);
// 设置欢迎语
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
String username = sp.getString("username", "");
tvWelcome.setText("欢迎您," + username);
// 设置退出按钮点击事件
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 退出登录,返回MainActivity
Intent intent = new Intent(HomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
在以上示例代码中,我们使用了SharedPreferences来保存用户的登录信息。在MainActivity中,当用户输入正确的用户名和密码时,我们将用户名保存到SharedPreferences中,在HomeActivity中读取并显示出来。当用户点击退出按钮时,我们将返回MainActivity,并关闭当前Activity。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)