各代码所在处
在这里插入图片描述
main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="MENU"
        android:textAlignment="center"
        android:textSize="50sp" />

    <GridView
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:numColumns="3"

        android:verticalSpacing="10dp" />




</LinearLayout>

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    int[] IMAGES={R.drawable.americano,R.drawable.icecream,R.drawable.yogurt,R.drawable.tea,
            R.drawable.capucino,R.drawable.icelatte,R.drawable.americano,R.drawable.icecream,
            R.drawable.yogurt,R.drawable.tea,R.drawable.capucino,R.drawable.icelatte,
            R.drawable.americano,R.drawable.icecream,R.drawable.yogurt,R.drawable.tea,
            R.drawable.capucino,R.drawable.icelatte};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        GridView gridView=(GridView)findViewById(R.id.gridview);
        ImageAdapter imageAdapter=new ImageAdapter();
        gridView.setAdapter(imageAdapter);
        gridView.setOnItemClickListener(this);

    }



    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long id) {
        Intent intent=new Intent(MainActivity.this,DrinkActivity.class);
        intent.putExtra(DrinkActivity.EXTRA_DRINKNO, i);
        startActivity(intent);
    }

    class ImageAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return IMAGES.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int i, View convertView, ViewGroup parent) {
            ImageView imageView =new ImageView(MainActivity.this);
            imageView.setLayoutParams(new GridView.LayoutParams(300,250));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(IMAGES[i]);
            return imageView;
        }
    }
}
****

为了能在画面里显示出如下的图标,我们必须得添加library
在这里插入图片描述
找大象图 build.gradle(Module:app) -------->最底下添加代码

   implementation 'com.cepheuen.elegant-number-button:lib:1.0.3'

在这里插入图片描述
之后我们要设计给客户看的画面了


drink_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DrinkActivity">


    <ImageView
        android:id="@+id/photo"
        android:layout_width="match_parent"
        android:layout_height="253dp"
        android:layout_marginTop="15dp" />

    <TextView
        android:id="@+id/name"
        android:layout_below="@id/photo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAlignment="center"
        android:textSize="30sp"/>

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:textAlignment="center"
        android:layout_marginTop="20dp"
        android:textSize="25sp"/>

    <CheckBox
        android:id="@+id/hot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/description"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="20dp"
        android:text="Hot" />

    <CheckBox
        android:id="@+id/cold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/description"
        android:layout_below="@id/description"
        android:layout_marginRight="100dp"
        android:layout_alignTop="@+id/hot"
        android:text="Cold" />

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cold"
        android:textSize="25dp"
        android:layout_alignEnd="@+id/photo"
        android:layout_alignRight="@+id/photo"
        android:layout_marginTop="30dp"
        android:ems="10"
        android:text="수량:" />


    <com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
        android:id="@+id/count_button"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/count"
        android:layout_alignRight="@id/count"
        android:layout_marginTop="-3dp"
        android:layout_marginRight="80dp" />

    <Button
        android:id="@+id/button_correct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button_cancel"
        android:layout_toLeftOf="@id/button_cancel"
        android:layout_marginRight="10dp"
        android:text="확인" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/count"
        android:layout_alignRight="@id/description"
        android:layout_marginTop="50dp"
        android:layout_marginRight="50dp"
        android:text="취소" />





</RelativeLayout>

DrinkActivity.java

package com.example.myapplication;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;

import static android.widget.Toast.LENGTH_LONG;

public class DrinkActivity extends Activity  {

    public  static final String EXTRA_DRINKNO ="drinkNo";
    protected int count;

    protected void onCreate(Bundle savedInstanceState)   {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drink_activity);

        int drinkNo=(Integer)getIntent().getExtras().get(EXTRA_DRINKNO);
        Drink drink=Drink.drinks[drinkNo];


        ImageView photo=(ImageView)findViewById(R.id.photo);
        photo.setImageResource(drink.getImageResourceId());
        photo.setContentDescription(drink.getName());

        TextView name=(TextView)findViewById(R.id.name);
        name.setText(drink.getName());

        TextView description=(TextView)findViewById(R.id.description);
        description.setText(drink.getDescription());


        final ElegantNumberButton button_count=(ElegantNumberButton) findViewById(R.id.count_button);
        button_count.setOnClickListener(new ElegantNumberButton.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num=button_count.getNumber();
                Log.e("NUM",num);
            }
        });


        Button button_correct=(Button)findViewById(R.id.button_correct);
        button_correct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(DrinkActivity.this,"카드를 넣어주십시오", LENGTH_LONG).show();
            }
        });

        Button button_cancel=(Button)findViewById(R.id.button_cancel);
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}

Drink.java

package com.example.myapplication;

public class Drink {
    private String name;
    private String description;
    private int imageResourceId;

    public  static final Drink[] drinks={
            new Drink("capucino","부드러운 카푸치노",R.drawable.capucino),
            new Drink("icecream","달콤한 아이스크림 ",R.drawable.icecream),
            new Drink("icelatte","겨울에도 시원함을 잃지 않다",R.drawable.icelatte),
            new Drink("americano","깊은 향기 찐한 아메리카노",R.drawable.americano),
            new Drink("yogurt","키즈들의 입맛",R.drawable.yogurt),

            new Drink("capucino","부드러운 카푸치노",R.drawable.capucino),
            new Drink("icecream","달콤한 아이스크림 ",R.drawable.icecream),
            new Drink("icelatte","겨울에도 시원함을 잃지 않다",R.drawable.icelatte),
            new Drink("americano","깊은 향기 찐한 아메리카노",R.drawable.americano),
            new Drink("yogurt","키즈들의 입맛",R.drawable.yogurt),
            new Drink("capucino","부드러운 카푸치노",R.drawable.capucino),
            new Drink("icecream","달콤한 아이스크림 ",R.drawable.icecream),
            new Drink("icelatte","겨울에도 시원함을 잃지 않다",R.drawable.icelatte),
            new Drink("americano","깊은 향기 찐한 아메리카노",R.drawable.americano),
            new Drink("yogurt","키즈들의 입맛",R.drawable.yogurt)


    };

    private  Drink(String name,String description,int imageResourceId){
        this.name=name;
        this.description=description;
        this.imageResourceId=imageResourceId;
    }
    public  String getDescription(){
        return description;
    }
    public String getName(){
        return name;
    }
    public int getImageResourceId(){
        return imageResourceId;
    }


}

整体图:

在这里插入图片描述
在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐