AndroidStudio模擬制作一個簡易的訂餐交易小案例【日常作業練習】
————安德風QQ1652102745
一、最終效果圖:
二、布局設計activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity" > 9 10 <ImageView 11 android:id="@+id/imageView6" 12 android:layout_width="126dp" 13 android:layout_height="150dp" 14 android:layout_marginStart="12dp" 15 android:layout_marginLeft="12dp" 16 android:layout_marginTop="148dp" 17 app:layout_constraintStart_toEndOf="@+id/imageView5" 18 app:layout_constraintTop_toTopOf="parent" 19 app:srcCompat="@drawable/hx" /> 20 21 <ImageView 22 android:id="@+id/imageView5" 23 android:layout_width="130dp" 24 android:layout_height="150dp" 25 android:layout_marginTop="148dp" 26 app:layout_constraintStart_toStartOf="parent" 27 app:layout_constraintTop_toTopOf="parent" 28 app:srcCompat="@drawable/cx" /> 29 30 <CheckBox 31 android:id="@+id/cb3" 32 android:layout_width="96dp" 33 android:layout_height="36dp" 34 android:layout_marginStart="44dp" 35 android:layout_marginLeft="44dp" 36 android:layout_marginTop="28dp" 37 android:text="叫花雞(55元)" 38 app:layout_constraintStart_toEndOf="@+id/cb2" 39 app:layout_constraintTop_toBottomOf="@+id/imageView7" /> 40 41 <CheckBox 42 android:id="@+id/cb2" 43 android:layout_width="100dp" 44 android:layout_height="36dp" 45 android:layout_marginStart="44dp" 46 android:layout_marginLeft="44dp" 47 android:layout_marginTop="28dp" 48 android:text="海鮮美食(60元)" 49 app:layout_constraintStart_toEndOf="@+id/cb1" 50 app:layout_constraintTop_toBottomOf="@+id/imageView6" /> 51 52 <CheckBox 53 android:id="@+id/cb1" 54 android:layout_width="95dp" 55 android:layout_height="36dp" 56 android:layout_marginStart="16dp" 57 android:layout_marginLeft="16dp" 58 android:layout_marginTop="28dp" 59 android:text="川香排骨(45元)" 60 app:layout_constraintStart_toStartOf="parent" 61 app:layout_constraintTop_toBottomOf="@+id/imageView5" /> 62 63 <Button 64 android:id="@+id/pay" 65 android:layout_width="143dp" 66 android:layout_height="51dp" 67 android:layout_marginTop="68dp" 68 android:text="付款" 69 app:layout_constraintEnd_toEndOf="parent" 70 app:layout_constraintHorizontal_bias="0.518" 71 app:layout_constraintStart_toStartOf="parent" 72 app:layout_constraintTop_toBottomOf="@+id/cb2" /> 73 74 <ImageView 75 android:id="@+id/imageView7" 76 android:layout_width="130dp" 77 android:layout_height="150dp" 78 android:layout_marginStart="12dp" 79 android:layout_marginLeft="12dp" 80 android:layout_marginTop="148dp" 81 app:layout_constraintStart_toEndOf="@+id/imageView6" 82 app:layout_constraintTop_toTopOf="parent" 83 app:srcCompat="@drawable/jhj" /> 84 85 <TextView 86 android:id="@+id/textView5" 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" 89 android:text="歡迎來到小浩訂餐助手" 90 android:textSize="35sp" 91 app:layout_constraintBottom_toTopOf="@+id/imageView6" 92 app:layout_constraintEnd_toEndOf="parent" 93 app:layout_constraintStart_toStartOf="parent" 94 app:layout_constraintTop_toTopOf="parent" /> 95 96 </androidx.constraintlayout.widget.ConstraintLayout>
三、layout目錄中創建qian.xml布局文件(用於自定義彈框)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <ImageView 9 android:id="@+id/imageView" 10 android:layout_width="match_parent" 11 android:layout_height="39dp" 12 app:srcCompat="@drawable/q" /> 13 14 <TextView 15 android:id="@+id/tv" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:gravity="center_horizontal" 19 android:text="TextView" 20 android:textSize="24sp" /> 21 </LinearLayout>
四、功能實現MainActivity.java
1 package com.example.tocast; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.CheckBox; 10 import android.widget.CompoundButton; 11 import android.widget.TextView; 12 import android.widget.Toast; 13 14 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 15 CheckBox cb1,cb2,cb3;//聲明復選按鈕1/2/3變量分別為cb1/cb2/cb3 16 Button pay;//聲明付款按鈕控件變量為 pay 17 private int count=0;//用於結算總付價格 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 cb1=findViewById(R.id.cb1);//尋找復選按鈕1的ID 24 cb2=findViewById(R.id.cb2);//尋找復選按鈕2的ID 25 cb3=findViewById(R.id.cb3);//尋找復選按鈕3的ID 26 pay=findViewById(R.id.pay);//尋找付款按鈕的ID 27 28 29 // cb1.setOnCheckedChangeListener(this);//給復選按鈕1安裝監聽器(OnCheckedChangeListener) 30 // cb2.setOnCheckedChangeListener(this);//給復選按鈕2安裝監聽器(OnCheckedChangeListener) 31 // cb3.setOnCheckedChangeListener(this);//給復選按鈕3安裝監聽器(OnCheckedChangeListener) 32 33 pay.setOnClickListener(this);//給付款按鈕安裝監聽器(OnClickListener) 34 35 } 36 37 38 39 40 //實現按鈕支付功能 41 @Override 42 public void onClick(View v) { 43 if (cb1.isChecked()) //如果選中復選按鈕1 商品(川香排骨)需要支付45元 44 count+=45; 45 if (cb2.isChecked()) //如果選中復選按鈕2 商品(海鮮美食)需要支付60元 46 count+=60; 47 if (cb3.isChecked()) //如果選中復選按鈕3 商品(叫花雞)需要支付55元 48 count+=55; 49 50 // 普通Toast彈出框方法(默認顯示在當前Activity最下面) 51 // Toast.makeText(this, "您支付了"+count+"元", Toast.LENGTH_SHORT).show(); 52 //彈框輸出:您支付了XXX元(彈框默認在最小角出現,出現時間較短,會自動消失) 53 54 55 56 //自定義彈框方法 57 LayoutInflater inflater=getLayoutInflater();// 創建LayoutInflater(布局加載器)將布局文件孵化為View對象 58 View layout=inflater.inflate(R.layout.qian,null);//孵化方法為inflate(作用:指定將孵化的View布局視圖存放在什么容器,一般使用null) 59 TextView tv=layout.findViewById(R.id.tv);//尋找布局內中的文本標簽控件(id為tv)的id 60 tv.setText("您支付了"+count+"元");//文本標簽控件輸出內容 61 Toast toast=new Toast(MainActivity.this);//創建一個Toast對象需要一個上下文參數(參數設置當前的本地類MainActivity) 62 toast.setView(layout);//設置toast彈出框輸出布局視圖文件中的內容 63 toast.setDuration(Toast.LENGTH_SHORT);//設置toast彈出框設置顯示內容持續時間(設置為Toast.LENGTH_SHORT短時間) 64 toast.show();//設置toast彈出框顯示 65 count=0;//付款總額清零 66 } 67 68 69 70 }
五、總結
本次練習新增知識點,主要
①掌握Toast用法實現彈框,實現支付交易成功。
②自定義彈框實現支付交易(提升用戶體驗感)其中提到了孵化方法inflate,本次新增的知識點並不多,更多的對舊的知識點加以鞏固。
好了以上就是本次練習的總結,感謝大家的觀看,如有問題歡迎指正;感謝您的關注與支持。