StudyJams-第04课_点咖啡应用
来源:互联网 发布:2016电气设备行业数据 编辑:程序博客网 时间:2024/06/04 17:53
0.边写Linux笔记看完了2A课程,我承认这对于初学者来说是一部很棒的教学视频,并不是吹嘘自己多强。至少不该花整整两个小时专心致志看这期视频。嗯……废话不多说,代码说事儿。
1.功能:点咖啡并结算
2.控件需求:
3.开始干活。上一期写过完整的新建AndroidStudio项目的过程,这次这里就省略过去)
3.0 贴士:尤其是从Eclipse转AndroidStudo的童鞋(以下是Eclipse和AS的各个名词的级别对应关系):
3.1 这一次我们不用像上次一样新建Project了,只要新建Module就好了。
3.2 接下来的操作与新建Project一致,不赘述。
3.3 先开始写布局文件:
3.3.1 删掉多余代码:
3.3.2 将根布局修改为LinearLayout,并加上
android:orientation="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
- StudyJams-第04课_点咖啡应用
- StudyJams-第03课_生日快乐APP+咖啡问答
- StudyJams-第05课_嵌套布局和字符串
- StudyJams-第06课_篮球记分板
- StudyJams-第07课_面向对象和创建实例
- StudyJams-第08课_多语言支持
- StudyJams-第02课_线性布局(LinearLayout)、相对布局(RelativeLayout)、填充(padding)、边距(margin)
- StudyJams-第01课_初识Android的View(TextView、ImageView、Button)
- StudyJams第一课的学习
- 第一杯咖啡
- 第一杯咖啡 :)
- 我的第一篇博客--感谢google的StudyJams活动
- @星巴克这样点咖啡
- 第一篇_随便乱写点
- Java咖啡馆---第一杯咖啡
- 咖啡小屋开张第一天
- Java咖啡馆---第一杯咖啡
- Android应用界面开发_学习笔记_第一周
- 使用Dnsmasq搭建本地dns服务器上网
- RelativeLayout相对布局
- 银行卡BIN码大全
- 数据库学习FAQ
- [pYUV]如何打开YUV/RGB图片
- StudyJams-第04课_点咖啡应用
- Java入门教程-5.1Java内部类及其实例化
- 方法重载(Overloading)和方法重写(Overriding)都是多态性的表现
- oracle函数trunc的使用
- 缓存图片并显示在adapter
- java入门教程-5.2Java静态内部类、匿名内部类、成员式内部类和局部内部类
- Git忽略规则及.gitignore规则不生效的解决办法
- CSS实现多行文字两端对齐的效果
- java入门教程-5.3Java抽象类的概念和使用