eclipse安卓开发关于身份信息实例

在Android开发中,身份信息是一个非常常见的需求,例如登录注册功能、用户信息修改等等。本文将介绍在Eclipse环境下,如何实现一个简单的身份信息实例,包含登录、注册、修改个人信息和退出四个功能。

1. 创建一个新的Eclipse项目

在Eclipse中创建一个新的Android项目,选择"Empty Activity",然后命名为"MyIdentity"。

2. 创建布局文件

在res/layout目录下创建如下布局文件:

1)activity_main.xml

```xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账号:"

android:textSize="20sp"/>

android:id="@+id/username"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20sp"/>

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码:"

android:textSize="20sp"/>

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20sp"/>

android:id="@+id/loginBtn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录"

android:textSize="20sp"/>

android:id="@+id/register"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="right"

android:clickable="true"

android:text="注册"

android:paddingRight="20dp"

android:textColor="#0000FF"

android:textSize="16sp"/>

```

2)activity_register.xml

```xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账号:"

android:textSize="20sp"/>

android:id="@+id/username"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20sp"/>

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码:"

android:textSize="20sp"/>

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20sp"/>

android:id="@+id/registerBtn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="注册"

android:textSize="20sp"/>

```

3)activity_update.xml

```xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="原密码:"

android:textSize="20sp"/>

android:id="@+id/oldPassword"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20sp"/>

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="新密码:"

android:textSize="20sp"/>

android:id="@+id/newPassword"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20sp"/>

android:id="@+id/updateBtn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="修改"

android:textSize="20sp"/>

```

3. 实现逻辑代码

在src目录下创建一个新的Java类文件,命名为"MainActivity.java"。在MainActivity类中编写如下代码:

```java

package com.example.myidentity;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

private EditText usernameEdit;

private EditText passwordEdit;

private Button loginBtn;

private TextView registerText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViews();

setListeners();

}

private void findViews() {

usernameEdit = (EditText) findViewById(R.id.username);

passwordEdit = (EditText) findViewById(R.id.password);

loginBtn = (Button) findViewById(R.id.loginBtn);

registerText = (TextView) findViewById(R.id.register);

}

private void setListeners() {

loginBtn.setOnClickListener(loginListener);

registerText.setOnClickListener(registerListener);

}

private OnClickListener loginListener = new OnClickListener() {

@Override

public void onClick(View v) {

// 获取用户输入的账号和密码

String username = usernameEdit.getText().toString().trim();

String password = passwordEdit.getText().toString().trim();

// 调用登录接口,实现身份验证逻辑

boolean result = login(username, password);

if (result) {

Intent intent = new Intent(MainActivity.this, UpdateActivity.class);

startActivity(intent);

} else {

Toast.makeText(MainActivity.this, "账号或密码错误", Toast.LENGTH_LONG).show();

}

}

};

private OnClickListener registerListener = new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, RegisterActivity.class);

startActivity(intent);

}

};

private boolean login(String username, String password) {

// 调用API接口,实现登录逻辑,这里只是简单的模拟用户名密码验证过程

if (username.equals("test") && password.equals("123456")) {

return true;

} else {

return false;

}

}

}

```

同理,在src目录下创建RegisterActivity.java和UpdateActivity.java两个类文件,分别负责注册和修改个人信息的逻辑。实现代码类似MainActivity。

4. 运行程序

在Eclipse中选择"Run As"->"Android Application",然后就可以在手机或模拟器上运行程序了。在运行之前,请确保Android SDK已经配置好,并且连接的手机或模拟器已经开启了USB调试模式。

综上所述,通过Eclipse可以很方便、快捷地实现一个简单的身份信息实例,帮助开发者熟悉Android开发的基本流程与技巧。

川公网安备 51019002001728号