下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

如何用安卓制作一个简单的购物车APP

作者:课课家教育     来源: http://www.kokojia.com点击数:2651发布时间: 2019-06-10 10:19:40

标签: 安卓游戏开发手机游戏开发android游戏开发

  其实,现在Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用"安卓"或"安致"。Android操作系统最初由AndyRubin开发,主要支持手机。2005年8月由Google收购注资。2007年11月,Google与84家硬件制造商、软件开发商及电信营运商组建开放手机联盟共同研发改良Android系统。随后Google以Apache开源许可证的授权方式,发布了Android的源代码。第一部Android智能手机发布于2008年10月。Android逐渐扩展到平板电脑及其他领域上,如电视、数码相机、游戏机、智能手表等。

  效果图(可自行修改布局):

如何用安卓制作一个简单的购物车APP_安卓游戏开发_ 手机游戏开发 _android游戏_课课家

  首先,我们需要在java内容中添加运行代码,代码如下:

MyAdapter:

public class MyAdapter extends BaseAdapter {
// 上下文
Context context;
// 数据源
ArrayList<Goods> lists;

MainActivity mainActivity;

int MONEY = 0;

HashMap<Integer,Boolean> isSelected;

public MyAdapter(Context context, ArrayList<Goods> lists) {
this.context = context;
this.lists = lists;
isSelected = new HashMap<>();
mainActivity = (MainActivity) context;
initData();
}

private void initData() {
for (int i = 0 ; i < lists.size();i++){
isSelected.put(i,false);
}
}

@Override
public int getCount() {
return lists == null ? 0 : lists.size();
}

@Override
public Object getItem(int position) {
return lists.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final MyHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.item,null);
holder = new MyHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.item_check);
holder.tv = (TextView) convertView.findViewById(R.id.item_text);
holder.price = (TextView) convertView.findViewById(R.id.item_price);
convertView.setTag(holder);
}else{
holder = (MyHolder) convertView.getTag();
}

holder.cb.setChecked(isSelected.get(position));
holder.tv.setText(lists.get(position).name);
holder.price.setText(lists.get(position).price+"");
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*if(isChecked){ // 这个写法影响到商品的计价,取消选择商品时不会减价。
isSelected.put(position,isChecked);
}*/
isSelected.put(position,isChecked);

MONEY = 0;
for (int i = 0 ; i <lists.size();i++){
if (isSelected.get(i)){
MONEY+=lists.get(i).price;
}
}
mainActivity.changeMoney(MONEY);
}
});

return convertView;
}

class MyHolder{
TextView tv,price;
CheckBox cb;
}
}

  第二步,我们在布局中添加相应的代码,代码如下:

public class MainActivity extends AppCompatActivity {

CheckBox btn_all, btn_revers;
ArrayList<Goods> lists;
MyAdapter adapter;
ListView listView;
TextView all_money;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn_all = (CheckBox) findViewById(R.id.btn_all);
btn_revers = (CheckBox) findViewById(R.id.btn_revers);
lists = new ArrayList<>();
listView = (ListView) findViewById(R.id.listView);
all_money = (TextView) findViewById(R.id.all_money);
for (int i = 1; i < 11; i++) {
lists.add(new Goods("商品"+i,i*10));
}
adapter = new MyAdapter(MainActivity.this, lists);
listView.setAdapter(adapter);

btn_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i < lists.size(); i++) {
adapter.isSelected.put(i, isChecked);
}
adapter.notifyDataSetChanged();
}
});
btn_revers.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
for (int i = 0 ; i < lists.size();i++){
if (adapter.isSelected.get(i)){
adapter.isSelected.put(i,!isChecked);
}else{
adapter.isSelected.put(i,isChecked);
}
}
}else{
for (int i = 0 ; i < lists.size();i++){
if (adapter.isSelected.get(i)){
adapter.isSelected.put(i,isChecked);
}else{
adapter.isSelected.put(i,!isChecked);
}
}
}
adapter.notifyDataSetChanged();
}
});
}
public void changeMoney(int money){
all_money.setText("总价:"+money);
}}

  最后一步,把相应的列表布局代码也写上,格式是XML,希望大家要注意:

主界面代码:

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<CheckBox
android:id="@+id/btn_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选" />

<CheckBox
android:id="@+id/btn_revers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="400dp"></ListView>

<TextView
android:id="@+id/all_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总价"
android:textStyle="bold"
android:textSize="30dp"/>
</LinearLayout>

  小编结语:其实,做一个安卓开发是非常简单的,不过也是需要大家去学习,才能做出来,希望大家能够好好学习。

赞(13)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程