studyjams大餐之: justjava 第一版

1 目标

  • button 事件响应
  • 基础的单步调试方法
  • 变量作用域

2 过程

code


package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/** * This app displays an order form to order coffee. */
public class MainActivity extends ActionBarActivity {

    // Number of cups of coffee ordered
    int quantity = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /** * This method is called when the plus button is clicked. */
    public void increment(View view) {
        quantity = quantity + 1;
        display(quantity);
    }

    /** * This method is called when the minus button is clicked. */
    public void decrement(View view) {
        quantity = quantity - 1;
        display(quantity);
    }

    /** * This method is called when the order button is clicked. */
    public void submitOrder(View view) {
        int price = quantity * 5;
        String priceMessage = "这是最简单的android demo: $" + price;
        priceMessage = priceMessage + "\n期待更多交互功能加入!";
        displayMessage(priceMessage);
    }

    /** * 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);
    }

    /** * This method displays the given price value on the screen. */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }

    /** * This method displays the given text on the screen. */
    private void displayMessage(String message) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(message);
    }

}

layout

<!-- 这是正规的注释方法 -->
<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" 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=".MainActivity" android:weightSum="1">

    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:text="Quantity" android:textAllCaps="true" />

    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">

        <Button  android:layout_width="48dp" android:layout_height="48dp" android:onClick="decrement" android:text="-" />

        <TextView  android:id="@+id/quantity_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="2" android:textColor="@android:color/black" android:textSize="16sp" />

        <Button  android:layout_width="48dp" android:layout_height="48dp" android:onClick="increment" android:text="+" />

    </LinearLayout>

    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Price" android:textAllCaps="true" />

    <TextView  android:id="@+id/price_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="$10" android:textColor="@android:color/black" android:textSize="16sp" />

    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:onClick="submitOrder" android:text="Order" />

    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="[email protected]" android:textColor="@android:color/black" android:textSize="16sp" android:layout_marginBottom="16dp" />

    <ImageView  android:layout_width="190dp" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_weight="0.55" android:src="@mipmap/ic_launcher" />


</LinearLayout>

3 实现截图

studyjams大餐之: justjava 第一版_第1张图片

4思考

  • 实现截屏gif的演示遇到x86不兼容
  • 放入汉字在模拟器中遇到乱码
  • res文件中放入自定义文件夹不识别

你可能感兴趣的:(studyjams大餐之: justjava 第一版)