标签:version auto order deb event code XML super value
任务:做这样一个界面,选择数量,自动计算价格。超级简单。。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:orientation="vertical"> <TextView android:id="@+id/quantityText_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Quantity" android:textColor="@android:color/darker_gray"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="48dp" android:layout_height="wrap_content" android:text="+" android:onClick="increment" /> <TextView android:id="@+id/quantity_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="16sp" android:paddingLeft="8dp" android:paddingRight="8dp" android:textColor="@android:color/black" /> <Button android:layout_width="48dp" android:layout_height="wrap_content" android:text="-" android:onClick="decrement"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Price" android:textColor="@android:color/darker_gray"/> <TextView android:id="@+id/price_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/black" android:textSize="16sp" android:text="$0"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ORDER" android:onClick="submitOrder" tools:ignore="OnClick" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
package com.learn.nianhao.justjava; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { int number =0; int price = 5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); display(number); displayPrice(number*price); } /** * This method is called when the order button is clicked. */ public void submitOrder(View view) { display(number); displayPrice(number*price); } /** * This method displays the given quantity value on the screen. */ private void display(int number) { TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view); quantityTextView.setText("" + number); } private void displayPrice(int price){ TextView priveTextView = (TextView) findViewById(R.id.price_text_view); priveTextView.setText("$"+price); } public void increment(View view) { number+=1; display(number); displayPrice(number*price); } public void decrement(View view) { if(number>0){ number-=1; display(number); displayPrice(number*price); } } }
标签:version auto order deb event code XML super value
原文地址:https://www.cnblogs.com/superxuezhazha/p/9426084.html