Android写一个登录页面
时间: 2023-11-10 15:14:00 浏览: 45
好的,以下是一个简单的Android登录页面的示例代码:
首先,我们需要在布局文件中定义登录页面的界面元素。在res/layout/目录下创建一个新的XML文件,命名为activity_login.xml,然后添加以下代码:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/login_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/username_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Log In" />
</LinearLayout>
```
这个布局文件包含了一个垂直排列的LinearLayout,其中包含了两个EditText和一个Button。
接下来,在Java代码中处理登录逻辑。在src/main/java/目录下创建一个新的Java类,命名为LoginActivity,并添加以下代码:
```java
public class LoginActivity extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
private Button mLoginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mUsernameEditText = findViewById(R.id.username_edit_text);
mPasswordEditText = findViewById(R.id.password_edit_text);
mLoginButton = findViewById(R.id.login_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = mUsernameEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
if (username.equals("admin") && password.equals("password")) {
// 登录成功,跳转到主界面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 登录失败,弹出提示框
Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
在这个Java类中,我们首先找到布局文件中定义的界面元素,并设置了一个点击监听器来处理登录按钮的点击事件。在点击事件中,我们获取了用户名和密码的输入值,并检查它们是否匹配我们的预设值。如果匹配成功,则跳转到主界面;否则,弹出一个提示框显示“无效的用户名或密码”。
最后,我们需要在AndroidManifest.xml文件中注册该Activity:
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity"></activity>
<activity android:name=".MainActivity"></activity>
<activity android:name=".RegisterActivity"></activity>
</application>
</manifest>
```
现在,我们就完成了一个简单的Android登录页面的开发。当我们启动应用程序时,将会看到登录界面。如果我们输入正确的用户名和密码,我们将会被重定向到MainActivity。如果输入错误的用户名或密码,我们将会看到一个提示框。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)