StudyJams-第04课_点咖啡应用

来源:互联网 发布:2016电气设备行业数据 编辑:程序博客网 时间:2024/06/04 17:53

0.边写Linux笔记看完了2A课程,我承认这对于初学者来说是一部很棒的教学视频,并不是吹嘘自己多强。至少不该花整整两个小时专心致志看这期视频。嗯……废话不多说,代码说事儿。
1.功能:点咖啡并结算
2.控件需求:

UI数量用处备注TextView4显示数量(QUANTITY)的text静态显示当前数量动态显示当前总价(PRICE)的text静态显示当前总价动态Button3增加咖啡数量增加咖啡减少咖啡数量减少咖啡结算按钮略显多余。可以用前两个按钮代替其功能。如果这个按钮作为提交订单,进行支付操作的话,还是需要的。

3.开始干活。上一期写过完整的新建AndroidStudio项目的过程,这次这里就省略过去)
   3.0 贴士:尤其是从Eclipse转AndroidStudo的童鞋(以下是Eclipse和AS的各个名词的级别对应关系):






EclipseAndroidStudio —Workspace WorkspaceProject ProjectModule
   3.1 这一次我们不用像上次一样新建Project了,只要新建Module就好了。
   新建Module
   3.2 接下来的操作与新建Project一致,不赘述。
   3.3 先开始写布局文件:
      3.3.1 删掉多余代码:
       删除多余代码
      3.3.2 将根布局修改为LinearLayout,并加上android:orientation="vertical"属性。
       vertical
      3.3.3 先写上需要的控件:4个TextView+3个Button
      所需控件
      3.3.4 定位布局,并加上必要显示数据。布局完成。
       布局+数据
      3.3.5 加上id和触发的onClick方法。(不截图了,直接贴代码。)
      activity_main.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:orientation="vertical"    tools:context="wang.relish.ordercoffee.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:layout_marginStart="16dp"        android:text="quantity"        android:textAllCaps="true" />    <Button        android:layout_width="48dp"        android:layout_height="48dp"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:layout_marginStart="16dp"        android:onClick="increase"        android:text="+" />    <TextView        android:id="@+id/tvQuantity"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:layout_marginStart="16dp"        android:text="0" />    <Button        android:layout_width="48dp"        android:layout_height="48dp"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:layout_marginStart="16dp"        android:onClick="decrease"        android:text="-" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:layout_marginStart="16dp"        android:text="Price"        android:textAllCaps="true" />    <TextView        android:id="@+id/tvPrice"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:layout_marginStart="16dp"        android:text="$0" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="16dp"        android:layout_marginStart="16dp"        android:onClick="submitOrder"        android:text="Order" /></LinearLayout>

      3.3.5 完善MainActivity.java文件。(添加3个OnClick方法,实例化2个TextView)

package wang.relish.ordercoffee;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    TextView tvQuantity;    TextView tvPrice;    int unitPrice = 5;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tvQuantity = (TextView) findViewById(R.id.tvQuantity);        tvPrice = (TextView) findViewById(R.id.tvPrice);    }    public void increase(View v) {        String currentQuantityStr = tvQuantity.getText().toString();        if (!currentQuantityStr.matches("[0-9]+")) {            Toast.makeText(this, "数字格式错误", Toast.LENGTH_SHORT).show();            return;        }        int currentQuantity = Integer.parseInt(currentQuantityStr);        tvQuantity.setText(String.valueOf(currentQuantity + 1));    }    public void decrease(View v) {        String currentQuantityStr = tvQuantity.getText().toString();        if (!currentQuantityStr.matches("[0-9]+")) {            Toast.makeText(this, "数字格式错误", Toast.LENGTH_SHORT).show();            return;        }        int currentQuantity = Integer.parseInt(currentQuantityStr);        if (currentQuantity == 0) {            return;        }        tvQuantity.setText(String.valueOf("$" + (currentQuantity - 1)));    }    public void submitOrder(View v) {        int currentQuantity = Integer.parseInt(tvQuantity.getText().toString());        int price = currentQuantity * unitPrice;        tvPrice.setText(String.valueOf(price));        Toast.makeText(this, "收银:$" + price, Toast.LENGTH_SHORT).show();    }}

      3.3.6 运行截图:
      运行截图

1 0
原创粉丝点击