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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.ersanaskin.notopening.MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TOPPINGS" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp"> <CheckBox android:text="CheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/checkBox" android:layout_weight="1" android:layout_marginRight="30dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Whipped Cream" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="QUANTITY" android:layout_marginTop="20dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp"> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:text="-" android:onClick="decreaseQuantity"/> <TextView android:id="@+id/quantity_text_view" android:layout_width="50dp" android:layout_height="wrap_content" android:text="0" android:textAlignment="center"/> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:text="+" android:onClick="increaseQuantity"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ORDER SUMMARY" android:layout_marginTop="15dp"/> <TextView android:id="@+id/order_summary_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ORDER" android:onClick="displayMessage"/></LinearLayout>
JAVA
package com.example.ersanaskin.notopening; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView; public class MainActivity extends AppCompatActivity { int quantity = 0; int coffeePrice = 10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void increaseQuantity(View view){ quantity ++; display(quantity); } public void decreaseQuantity(View view){ quantity --; display(quantity); } private void display(int number) { TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view); quantityTextView.setText("" + number); } public void displayMessage(View v) { String message = "Hello Ersan. You bought " + quantity + " coffee. Please pay $" + quantity * coffeePrice + "."; TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view); orderSummaryTextView.setText(message); } }
Hiç yorum yok:
Yorum Gönder