目录
一、项目测试视频
网络资源模板--基于Android Studio 二手交易平台
二、项目简介
包含注册登录,密码校验,下拉列表框,ListView列表,SQLiter数据库的增删改查等等。可以进行闲置物品的发布,进行闲置物品的分类。在个人中心可以查看自己的个人信息、发布的商品信息,密码修改,退出登录,多达10几个页面。
三、项目测试环境
四、项目详情设计
1.登陆注册页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg2"
tools:context="com.example.school.LoginMainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="#516D2C"
android:text="校园二手交易平台"
android:textSize="35dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/buy_3"></ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">
<ImageView
android:id="@+id/pic_user"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginLeft="25dp"
android:src="@drawable/user"
/>
<EditText
android:id="@+id/login_user"
android:layout_width="match_parent"
android:layout_height="55dp"
android:maxLines="1"
android:layout_toRightOf="@+id/pic_user"
android:layout_marginLeft="15dp"
android:layout_marginRight="50dp"
android:hint="请输入用户账号"
android:textSize="25dp"
android:background="@drawable/editext_selector"
android:textColor="#000000"
android:inputType="text"
android:textColorHint="#708090"
/>
<ImageView
android:id="@+id/pic_pass"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginLeft="25dp"
android:src="@drawable/passwd"
android:layout_marginTop="15dp"
android:layout_below="@+id/pic_user"
/>
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="55dp"
android:maxLines="1"
android:layout_alignBottom="@+id/pic_pass"
android:layout_toRightOf="@+id/pic_pass"
android:layout_marginLeft="15dp"
android:layout_marginRight="50dp"
android:hint="请输入用户密码"
android:textSize="25dp"
android:textColorHint="#708090"
android:background="@drawable/editext_selector"
android:textColor="#000000"
android:inputType="text"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp">
<TextView
android:id="@+id/toRegister"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="新用户注册"
android:textSize="20dp"
android:textColor="#CCCCCC"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="登 录"
android:textColor="#FFFFFF"
android:background="@drawable/btn_selector"
android:textSize="23sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
这是一个基本的Android应用布局文件,主要用于创建一个校园二手交易平台的登录界面。
-
整体布局使用了LinearLayout,设置了垂直方向的布局。背景使用了drawable文件bg2。
-
第一个LinearLayout包含了一个TextView,用于显示标题“校园二手交易平台”。
-
第二个LinearLayout包含了一个ImageView展示购买相关的图像,接着是一个RelativeLayout,包含了用户名和密码输入框。
-
在RelativeLayout中,分别使用了ImageView和EditText来展示和输入用户名和密码。用户名部分包含了用户图标和输入框,密码部分也包含了密码图标和输入框。
-
下方的LinearLayout包含了一个TextView用于新用户注册的提示。
-
最后是一个登录按钮,用于提交用户的登录信息。
整体布局简洁明了,使用了一些常见的Android控件和属性来构建用户友好的界面。
package com.example.school;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.school.R;
public class LoginMainActivity extends Activity {
private EditText User;
private EditText Password;
private Button button_login;
private TextView first;
private TextView toRegister;
private RadioButton button1;
private RadioButton button2;
private RadioButton button3;
protected Intent intent;
protected static String post_userid;
String user=null;
String password=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_main);
User=(EditText)findViewById(R.id.login_user);
Password=(EditText)findViewById(R.id.login_password);
button_login=(Button)findViewById(R.id.login);
toRegister=(TextView)findViewById(R.id.toRegister);
post_userid="";
//登录验证,成功后跳转到主页
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user=User.getText().toString();
password=Password.getText().toString();
if(user.equals("")||user==null){
Toast.makeText(getApplicationContext(), "请输入用户账号!", Toast.LENGTH_SHORT).show();
}
if(password.equals("")||password==null){
Toast.makeText(getApplicationContext(), "请输入用户密码!", Toast.LENGTH_SHORT).show();
}
checkUser(user,password);
}
});
//跳转到注册页面
toRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(LoginMainActivity.this,RegisterMainActivity.class);
startActivity(intent);
}
});
}
private void checkUser(String user,String password){
DatabaseHelper dbhelper = new DatabaseHelper(this);
SQLiteDatabase db=dbhelper.getReadableDatabase();
try{
String sql="SELECT * FROM users WHERE userId=? and passWord=?";
Cursor cursor=db.rawQuery(sql,new String[]{user,password});
if(cursor.getCount()==0){
Toast.makeText(getApplicationContext(), "用户密码错误!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginMainActivity.this,main_page.class);
post_userid=user;
startActivity(intent);
}
cursor.close();
db.close();
}catch (SQLiteException e){
Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
}
}
}
这是一个用于校园二手交易平台的Android应用登录页面的Java代码。
-
在LoginMainActivity类中,定义了一些私有变量,包括EditText用于输入用户账号和密码,Button用于登录操作,TextView用于提示用户注册,RadioButton等。
-
在onCreate()方法中,设置了布局文件为activity_login_main,并初始化了各个控件。
-
在登录按钮的点击事件监听器中,获取用户输入的账号和密码,并进行验证。如果账号或密码为空,则会弹出提示消息。接着调用checkUser方法进行用户账号密码的校验。
-
在跳转到注册页面的点击事件监听器中,创建一个Intent对象用于从当前页面跳转到RegisterMainActivity.class页面。
-
checkUser方法中,首先创建了一个DatabaseHelper对象用于操作数据库。然后查询数据库中是否存在对应的用户账号和密码,如果不存在则提示用户密码错误,如果存在则提示登录成功,并跳转到主页main_page.class页面。最后关闭游标和数据库。
-
如果在数据库操作过程中出现异常,则会提示登录失败。
这段代码实现了用户登录功能,包括用户输入验证和跳转到主页的操作。同时也包含了错误处理的逻辑。
2.首页
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="78dp"
android:layout_marginTop="-4dp"
android:src="@drawable/main_header" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:id="@+id/kind1"
android:layout_width="93dp"
android:layout_height="match_parent"
android:src="@drawable/buy_kind1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<ImageView
android:id="@+id/kind2"
android:layout_width="85dp"
android:layout_height="match_parent"
android:src="@drawable/buy_kind2"
android:layout_toRightOf="@id/kind1"/>
<ImageView
android:id="@+id/kind3"
android:layout_width="93dp"
android:layout_height="match_parent"
android:src="@drawable/buy_kind3"
android:layout_toRightOf="@id/kind2"/>
<ImageView
android:id="@+id/kind4"
android:layout_width="93dp"
android:layout_height="match_parent"
android:src="@drawable/buy_kind4"
android:layout_toRightOf="@id/kind3"/>
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:src="@drawable/line"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="horizontal">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:gravity="bottom"
android:orientation="vertical">
<View
android:id="@+id/line"
android:background="#333333"
android:layout_width="match_parent"
android:layout_height="5dp"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--第一个RadioButton-->
<RadioButton
android:id="@+id/button_1"
android:text="首页"
android:textStyle="bold"
android:button="@null"
android:textColor="#666666"
android:gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/pic_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<View
android:background="#CCCCCC"
android:layout_width="2dp"
android:layout_height="match_parent"/>
<!--第二个RadioButton-->
<RadioButton
android:id="@+id/button_2"
android:text="发布闲置"
android:button="@null"
android:textStyle="bold"
android:textColor="#666666"
android:gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/pic_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--第三个RadioButton-->
<View
android:background="#CCCCCC"
android:layout_width="2dp"
android:layout_height="match_parent"/>
<RadioButton
android:id="@+id/button_3"
android:text="个人中心"
android:textStyle="bold"
android:button="@null"
android:textColor="#666666"
android:gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/pic_my"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
package com.example.school;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SimpleAdapter;
import androidx.appcompat.app.AppCompatActivity;
import com.example.school.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class main_page extends AppCompatActivity implements View.OnClickListener{
String TABLENAME = "iteminfo";
Intent intent;
byte[] imagedata;
Bitmap imagebm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
DatabaseHelper database = new DatabaseHelper(this);
final SQLiteDatabase db = database.getWritableDatabase();
ListView listView = (ListView)findViewById(R.id.listView);
Map<String, Object> item; // 列表项内容用Map存储
final List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); // 列表
Cursor cursor = db.query(TABLENAME,null,null,null,null,null,null,null); // 数据库查询
if (cursor.moveToFirst()){
while (!cursor.isAfterLast()){
item = new HashMap<String, Object>(); // 为列表项赋值
item.put("id",cursor.getInt(0));
item.put("userid",cursor.getString(1));
item.put("title",cursor.getString(2));
item.put("kind",cursor.getString(3));
item.put("info",cursor.getString(4));
item.put("price",cursor.getString(5));
imagedata = cursor.getBlob(6);
imagebm = BitmapFactory.decodeByteArray(imagedata, 0, imagedata.length);
item.put("image",imagebm);
cursor.moveToNext();
data.add(item); // 加入到列表中
}
}
/*
item = new HashMap<String, Object>();
item.put("userid","ysh");
item.put("image", R.drawable.buy_item1);
item.put("title","一个九成新的篮球");
item.put("kind","体育用品");
item.put("info", "刚买没多久,希望转卖出去...");
item.put("price", "59元");
data.add(item);
item = new HashMap<String, Object>();
item.put("userid","xg");
item.put("image", R.drawable.buy_item2);
item.put("title","一个八成新的篮球");
item.put("kind","体育用品");
item.put("info", "刚买没多久,希望转卖出去...");
item.put("price", "59元");
data.add(item);
item = new HashMap<String, Object>();
item.put("userid","hdq");
item.put("image", R.drawable.buy_item3);
item.put("title","一个八成新的篮球");
item.put("kind","体育用品");
item.put("info", "刚买没多久,希望转卖出去...");
item.put("price", "59元");
data.add(item);
*/
// 使用SimpleAdapter布局listview
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.listitem, new String[] { "image", "title", "kind", "info", "price" },
new int[] { R.id.item_image, R.id.title, R.id.kind, R.id.info, R.id.price });
simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if(view instanceof ImageView && data instanceof Bitmap){
ImageView iv = (ImageView)view;
iv.setImageBitmap( (Bitmap)data );
return true;
}else{
return false;
}
}
});
listView.setAdapter(simpleAdapter);
ImageView kind1 = (ImageView) findViewById(R.id.kind1);
kind1.setOnClickListener(this);
ImageView kind2 = (ImageView) findViewById(R.id.kind2);
kind2.setOnClickListener(this);
ImageView kind3 = (ImageView) findViewById(R.id.kind3);
kind3.setOnClickListener(this);
ImageView kind4 = (ImageView) findViewById(R.id.kind4);
kind4.setOnClickListener(this);
// 为列表项设置监听器
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
intent = new Intent(main_page.this, item_info.class);
intent.putExtra("id", data.get(position).get("id").toString()); // 获取该列表项的key为id的键值,即商品的id,将其储存在Bundle传递给打开的页面
startActivity(intent);
}
});
RadioButton btn1 = (RadioButton)findViewById(R.id.button_1);
RadioButton btn2 = (RadioButton)findViewById(R.id.button_2);
RadioButton btn3 = (RadioButton)findViewById(R.id.button_3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.kind1:
Intent KindIntent1 = new Intent(this,kind_page1.class);
startActivity(KindIntent1);
break;
case R.id.kind2:
Intent KindIntent2 = new Intent(this,kind_page2.class);
startActivity(KindIntent2);
break;
case R.id.kind3:
Intent KindIntent3 = new Intent(this,kind_page3.class);
startActivity(KindIntent3);
break;
case R.id.kind4:
Intent KindIntent4 = new Intent(this,kind_page4.class);
startActivity(KindIntent4);
break;
case R.id.button_1:
Intent button1 = new Intent(main_page.this,main_page.class);
startActivity(button1);
break;
case R.id.button_2:
Intent button2 = new Intent(this,AddItem.class);
startActivity(button2);
break;
case R.id.button_3:
Intent button3 = new Intent(this,MyselfActivity.class);
startActivity(button3);
break;
}
}
}
3.发布页面
4.个人中心
package com.example.school;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.school.R;
public class MyselfActivity extends AppCompatActivity {
private RadioButton button1;
private RadioButton button2;
private RadioButton button3;
private Button myself;
private Button myshow;
private Button changepwd;
private Button about;
private Button login;
private TextView myId;
protected Intent intent;
private String a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myself);
button1=(RadioButton)findViewById(R.id.button_1);
button2=(RadioButton)findViewById(R.id.button_2);
button3=(RadioButton)findViewById(R.id.button_3);
myself=(Button)findViewById(R.id.myself);
myshow=(Button)findViewById(R.id.myShow);
changepwd=(Button)findViewById(R.id.changepwd);
about=(Button)findViewById(R.id.about);
login=(Button)findViewById(R.id.login) ;
myId=(TextView)findViewById(R.id.myId);
a=LoginMainActivity.post_userid;
myId.setText(a);//显示用户账号
Log.i("123",a);
if(a.equals("")||a==null){
login.setText("登 录");
}else{
login.setText("退出登录");
}
//跳转到主页
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MyselfActivity.this,main_page.class);
startActivity(intent);
}
});
//跳转到发布闲置
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {if(a.equals("")||a==null){
Toast.makeText(getApplicationContext(), "请先登录!", Toast.LENGTH_SHORT).show();
intent = new Intent(MyselfActivity.this,LoginMainActivity.class);
startActivity(intent);
}
intent = new Intent(MyselfActivity.this,AddItem.class);
startActivity(intent);
}
});
//跳转到个人中心
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MyselfActivity.this,MyselfActivity.class);
startActivity(intent);
}
});
//跳转到个人信息页面
myself.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(a.equals("")){
Toast.makeText(getApplicationContext(), "请先登录!", Toast.LENGTH_SHORT).show();
intent = new Intent(MyselfActivity.this,LoginMainActivity.class);
startActivity(intent);
}
Log.i("123","111111111");
intent = new Intent(MyselfActivity.this,userMsgActivity.class);
startActivity(intent);
}
});
//跳转到个人发布页面
myshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(a.equals("")||a==null){
Toast.makeText(getApplicationContext(), "请先登录!", Toast.LENGTH_SHORT).show();
intent = new Intent(MyselfActivity.this,LoginMainActivity.class);
startActivity(intent);
}
else {
intent = new Intent(MyselfActivity.this, MyItems.class);
startActivity(intent);
}
}
});
//跳转到修改密码页面
changepwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(a.equals("")||a==null){
Toast.makeText(getApplicationContext(), "请先登录!", Toast.LENGTH_SHORT).show();
intent = new Intent(MyselfActivity.this,LoginMainActivity.class);
startActivity(intent);
}
intent = new Intent(MyselfActivity.this,changepwdActivity.class);
startActivity(intent);
}
});
//跳转到关于页面
about.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MyselfActivity.this,AboutMainActivity.class);
startActivity(intent);
}
});
//跳转到登录页面
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(a.equals("")||a==null){
intent = new Intent(MyselfActivity.this,LoginMainActivity.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "退出成功", Toast.LENGTH_SHORT).show();
LoginMainActivity.post_userid="";
intent = new Intent(MyselfActivity.this,LoginMainActivity.class);
startActivity(intent);
}
}
});
}
}
Get 项目模板源码
👇👇👇快捷获取方式👇👇👇