我可以创建没有数据库的添加到购物车功能?

问题描述 投票:-2回答:1

我是Android Studio的新手,我想创建几个项目的添加到购物车,所以我可以在此代码中创建没有数据库的复选框和单选按钮吗?

XML文件:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_weight="1"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:title="@string/app_name">

    <ImageView
        android:layout_width="250px"
        android:layout_height="250px"
        android:src="@drawable/master_baker"
        android:layout_marginLeft="150dp" />

</android.support.v7.widget.Toolbar>

<android.support.v7.widget.CardView
    android:layout_marginTop="58dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/img">

    <ImageView
        android:layout_width="720px"
        android:layout_height="720px"
        android:src="@drawable/pasta"
        android:layout_gravity="center"/>
</android.support.v7.widget.CardView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="00dp"
    android:layout_below="@+id/img">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Price: 15 AED"
        android:textSize="25dp"
        android:gravity="center_horizontal" />

    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Choise:"
        android:textStyle="bold"
        android:textSize="25dp"
        android:textColor="#000000"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:fadeScrollbars="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical">

               <RadioGroup
                   android:id="@+id/radioGroup"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content">

                   <RadioButton
                       android:id="@+id/radio1"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="White sauce"
                       android:textSize="20dp"
                       android:buttonTint="@color/colorPrimaryDark"
                       android:checked="false" />

                   <RadioButton
                       android:id="@+id/radio2"
                       android:layout_marginTop="50dp"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="Mix sauce"
                       android:textSize="20dp"
                       android:buttonTint="@color/colorPrimaryDark"
                       android:checked="false" />

                   <RadioButton
                       android:id="@+id/radio3"
                       android:layout_marginTop="50dp"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="Red sauce"
                       android:textSize="20dp"
                       android:buttonTint="@color/colorPrimaryDark"
                       android:checked="false" />

               </RadioGroup>

            </LinearLayout>

            <LinearLayout
                android:layout_marginLeft="70dp"
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <CheckBox
                    android:id="@+id/box"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Garlic"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark" />

                <CheckBox
                    android:id="@+id/box2"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hotdog"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box3"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Chicken"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box4"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Tomato"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box5"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Brocolli"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box6"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Mushroom"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box7"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Bellpepper"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box8"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Spicy"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box9"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Zaater"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <CheckBox
                    android:id="@+id/box10"
                    android:layout_marginTop="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Cheese"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimaryDark"
                    android:checked="false"/>

                <ImageButton
                    android:layout_marginTop="20dp"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:src="@drawable/cart"
                    android:background="#00ffffff"
                    android:layout_gravity="end"
                    android:id="@+id/cart"/>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>
</LinearLayout>
</RelativeLayout>

Java文件:

package ae.ac.aurak.myapplication2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MB_Food_Submenu_Pasta extends AppCompatActivity {

private RadioGroup radioGroup;
private RadioButton radioButton;
private CheckBox b, b2, b3, b4, b5, b6, b7, b8, b9, b10;
private ImageButton cart;
//private RadioButton r, r2, r3;
//View v;

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

    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    //radioButton = (RadioButton) 
findViewById(radioGroup.getCheckedRadioButtonId());

    b = (CheckBox) findViewById(R.id.box);
    b2 = (CheckBox) findViewById(R.id.box2);
    b3 = (CheckBox) findViewById(R.id.box3);
    b4 = (CheckBox) findViewById(R.id.box4);
    b5 = (CheckBox) findViewById(R.id.box5);
    b6 = (CheckBox) findViewById(R.id.box6);
    b7 = (CheckBox) findViewById(R.id.box7);
    b8 = (CheckBox) findViewById(R.id.box8);
    b9 = (CheckBox) findViewById(R.id.box9);
    b10 = (CheckBox) findViewById(R.id.box10);

    b.setEnabled(false);
    b2.setEnabled(false);
    b3.setEnabled(false);
    b4.setEnabled(false);
    b5.setEnabled(false);
    b6.setEnabled(false);
    b7.setEnabled(false);
    b8.setEnabled(false);
    b9.setEnabled(false);
    b10.setEnabled(false);

    Toast.makeText(MB_Food_Submenu_Pasta.this, "Choose which SAUCE you 
prefer first first", Toast.LENGTH_LONG).show();


    radioGroup.setOnCheckedChangeListener(new 
RadioGroup.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            radioButton = (RadioButton) group.findViewById(checkedId);

            boolean isChecked = radioButton.isChecked();
            if(isChecked)
            {
                b.setEnabled(true);
                b2.setEnabled(true);
                b3.setEnabled(true);
                b4.setEnabled(true);
                b5.setEnabled(true);
                b6.setEnabled(true);
                b7.setEnabled(true);
                b8.setEnabled(true);
                b9.setEnabled(true);
                b10.setEnabled(true);
            }
        }
    });
}
}
java android
1个回答
0
投票

是的,您可以使用共享首选项。创建一个arraylist并添加您的项目然后存储到首选项。

© www.soinside.com 2019 - 2024. All rights reserved.