本文实例为大家分享了Android Studio实现购买售卖系统的具体代码,供大家参考,具体内容如下
本项目基于安卓系统开发的界面设计,包括登录,主页面,展示页面,购买页面等六个页面
ShopActivity
package com.example.tryfirst;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ShopActivity extends AppCompatActivity implements View.OnClickListener{
private ItemInfo itemInfo0;
private ItemInfo itemInfo1;
private ItemInfo itemInfo2;
private ItemInfo itemInfo3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
itemInfo0 = new ItemInfo("空军一号");
itemInfo1 = new ItemInfo("李宁足球鞋");
itemInfo2 = new ItemInfo("匹克太极篮球鞋");
itemInfo3 = new ItemInfo("林丹羽毛球鞋");
findViewById(R.id.btn_0).setOnClickListener(this);
findViewById(R.id.btn_1).setOnClickListener(this);
findViewById(R.id.btn_2).setOnClickListener(this);
findViewById(R.id.btn_3).setOnClickListener(this);
}
public void onClick(View v){
Intent intent;
switch (v.getId()) {
case R.id.btn_0:
intent = new Intent();
intent.putExtra("shoe",itemInfo0);
setResult(1,intent);
finish();
break;
case R.id.btn_1:
intent = new Intent();
intent.putExtra("shoe",itemInfo1);
setResult(1,intent);
finish();
break;
case R.id.btn_2:
intent = new Intent();
intent.putExtra("shoe",itemInfo0);
setResult(1,intent);
finish();
break;
case R.id.btn_3:
intent = new Intent();
intent.putExtra("shoe",itemInfo0);
setResult(1,intent);
finish();
break;
}
}
}
ShowActivity
package com.example.tryfirst;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ShowActivity extends AppCompatActivity {
private TextView tv_name;
private TextView tv_password;
private TextView shoe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String password = intent.getStringExtra("password");
tv_name = (TextView) findViewById(R.id.tv_name);
tv_password = (TextView) findViewById(R.id.tv_password);
tv_name.setText("用户名为: " + name);
tv_password.setText("密码为: " + password);
shoe = (TextView) findViewById(R.id.tv_food_progress);
}
public void click0(View view){
Intent intent = new Intent(this, FriendActivity.class);
startActivity(intent);
}
public void click1(View view){
Intent intent = new Intent(this,ShopActivity.class);
startActivityForResult(intent,1);
}
public void click2(View view){
Intent intent = new Intent(this,ListActivity.class);
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(data !=null){
if(requestCode==1){
if(resultCode==1){
ItemInfo info =
(ItemInfo) data.getSerializableExtra("food");
updateProgress(info);
}
}
}
}
private void updateProgress(ItemInfo info){shoe.setText(info.getName());
}
}
ListActivity
package com.example.tryfirst;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends AppCompatActivity {
private ListView mListView;
private String[] names = {"郭艾伦aj34"};
private int[] herd = {R.drawable.l};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mListView = (ListView) findViewById(R.id.lv);
MyBaseAdapter myAdapter = new MyBaseAdapter();
mListView.setAdapter(myAdapter);
}
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int position) {
return names[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//将list.xml文件找出来转化为View对象
View view = View.inflate(ListActivity.this,R.layout.list,null);
TextView mTextView = (TextView) view.findViewById(R.id.tv);
mTextView.setText(names[position]);
ImageView imageView = (ImageView) view.findViewById(R.id.imge);
imageView.setBackgroundResource(herd[position]);
return view;
}
}
}
Activity_Shop .xml
<?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:background="@drawable/back"
android:orientation="vertical"
tools:context=".ShopActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#307f7f7f"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="80dp"
android:layout_height="200dp"
android:background="@drawable/lin"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="空军一号"
android:layout_weight="3"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_8"
android:text="加入购物车"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_0"
android:text="购买"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="200dp"
android:background="@drawable/longtuos"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="李宁足球鞋"
android:textSize="35sp"
android:layout_weight="3"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_9"
android:text="加入购物车"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_1"
android:text="购买"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="200dp"
android:background="@drawable/longtuos"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="匹克太极篮球鞋"
android:textSize="35sp"
android:layout_weight="3"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_6"
android:text="加入购物车"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_2"
android:text="购买"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="200dp"
android:background="@drawable/longtuos"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="林丹羽毛球鞋"
android:textSize="35sp"
android:layout_weight="3"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_3"
android:text="加入购物车"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_4"
android:text="购买"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Activity_Show .xml
<?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:background="@drawable/back"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginBottom="50dp"
android:orientation="horizontal"
android:padding="20dp">
<ImageView
android:id="@+id/pet"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/head"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="40dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textSize="20sp"
android:textColor="#33cc00"/>
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text=" "
android:textSize="20sp"
android:textColor="#33cc00"/>
</LinearLayout>
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="背景是北宋郭忠恕的明皇避暑宫图"
android:textColor="#ff0000"
android:textSize="25sp" />
<TextView
android:id="@+id/tv_food_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text=" "
android:textColor="#99ff77"
android:textSize="40sp"/>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_gravity="center"
android:layout_marginLeft="28dp"
android:layout_marginRight="50dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/a"
android:onClick="click0"/>
<Button
android:layout_gravity="center"
android:layout_marginRight="28dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/buy"
android:layout_marginTop="90px"
android:onClick="click1"/>
</LinearLayout>
<Button
android:layout_width="900px"
android:layout_height="100px"
android:layout_marginTop="50dp"
android:text="鞋的列表展示"
android:onClick="click2"
android:textColor="#ff0000"
android:background="#99ff00"/>
</LinearLayout>
运行结果展示图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。