resvalues目录下dimens.xml、strings,xml
Button文本自动大写
Java知识 Java官方开发文档
Java变量命名规则:不可过短或过长、首字母小写驼峰式。Language Basics-Variables
bug复现后易于debug(通过查看系统日志,搜索关键词并Google,逐行阅读不现实),难以复现的bug最难debug。即使是拥有11年经验编程经验的程序员,依然有许多不懂且需每天学习。
@Override,伪代码,复写/重载。
- 帮助自己检查是否正确的复写了父类中已有的方法
- 告诉读代码的人,这是一个复写的方法
局部变量、全局变量。局部变量作用域,使用后销毁、其他方法中重复定义。
int 整型 integer
String message = "The total trip will be: " + totalLength + " miles.";//定义并赋值字符串类型的变量可使用字符串与数字变量的组合形式,具体方式如左边所示。
确保已开启 Auto Import:
- 对于 Windows,请依次转到“文件 (File)”>“设置 (Settings)”>“编辑器 (Editor)”>“常规 (General)”>“自动导入 (Auto Import)”
- 对于 Mac,请依次转到 Android Studio >“偏好设置 (Preferences)”>“编辑器 (Editor)”>“常规 (General)”>“自动导入 (Auto Import)”
选中以下所有选项:
- Show import popup
- Optimize imports on the fly
- Add unambiguous imports on the fly
- Insert imports on paste → All
断点调试
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:paddingBottom="@dimen/activity_vertical" 8 android:paddingLeft="@dimen/activity_horizon" 9 android:paddingRight="@dimen/activity_horizon" 10 android:paddingTop="@dimen/activity_vertical" 11 tools:context="com.example.administrator.justjava.MainActivity"> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="@string/quantity" 17 android:textAllCaps="true" /> 18 19 <LinearLayout 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:orientation="horizontal" 23 android:paddingBottom="@dimen/linear_vertical" 24 android:paddingTop="@dimen/linear_vertical"> 25 26 <Button 27 android:layout_width="@dimen/small_button" 28 android:layout_height="@dimen/small_button" 29 android:onClick="decrement" 30 android:text="@string/minus" 31 android:textSize="@dimen/text_size" /> 32 33 <TextView 34 android:id="@+id/quantity_text_view" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:paddingLeft="@dimen/quantity_horizon" 38 android:paddingRight="@dimen/quantity_horizon" 39 android:text="@string/value" 40 android:textColor="@android:color/black" 41 android:textSize="@dimen/text_size" /> 42 43 <Button 44 android:layout_width="@dimen/small_button" 45 android:layout_height="@dimen/small_button" 46 android:onClick="increment" 47 android:text="@string/plus" 48 android:textSize="@dimen/text_size" /> 49 50 </LinearLayout> 51 52 <TextView 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" 55 android:text="@string/price" 56 android:textAllCaps="true" /> 57 58 <TextView 59 android:id="@+id/price_text_view" 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" 62 android:layout_marginBottom="@dimen/price_vertical" 63 android:layout_marginTop="@dimen/price_vertical" 64 android:text="@string/price_value" 65 android:textColor="@android:color/black" 66 android:textSize="@dimen/text_size" /> 67 68 <Button 69 android:id="@+id/button" 70 android:layout_width="wrap_content" 71 android:layout_height="wrap_content" 72 android:onClick="submitOrder" 73 android:text="@string/button"/> 74 75 </LinearLayout>
1 <resources> 2 <string name="app_name">Just Java</string> 3 <string name="quantity">Quantity</string> 4 <string name="value">2</string> 5 <string name="price">price</string> 6 <string name="price_value">$0</string> 7 <string name="button">Order</string> 8 <string name="plus">+</string> 9 <string name="minus">-</string> 10 </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="activity_vertical">8dp</dimen> <dimen name="activity_horizon">16dp</dimen> <dimen name="linear_vertical">16dp</dimen> <dimen name="small_button">48dp</dimen> <dimen name="quantity_horizon">8dp</dimen> <dimen name="text_size">16sp</dimen> <dimen name="price_vertical">16dp</dimen> </resources>
1 package com.example.administrator.justjava; 2 3 /** 4 * Add your package below. Package name can be found in the project's AndroidManifest.xml file. 5 * This is the package name our example uses: 6 * 7 * package com.example.android.justjava; 8 */ 9 10 import android.os.Bundle; 11 import android.support.v7.app.AppCompatActivity; 12 import android.view.View; 13 import android.widget.TextView; 14 15 import java.text.NumberFormat; 16 17 /** 18 * This app displays an order form to order coffee. 19 */ 20 public class MainActivity extends AppCompatActivity { 21 22 int quantity = 2; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 } 29 30 /** 31 * This method is called when the add button is clicked. 32 */ 33 public void increment(View view) { 34 quantity = quantity + 1; 35 display(quantity); 36 } 37 38 /** 39 * This method is called when the minus button is clicked. 40 */ 41 public void decrement(View view) { 42 quantity = quantity - 1; 43 display(quantity); 44 } 45 46 /** 47 * This method is called when the order button is clicked. 48 */ 49 public void submitOrder(View view) { 50 int price = quantity * 5; 51 String priceMessage = "Total $" + price; 52 priceMessage = priceMessage + " Thank you!"; 53 displayMessage(priceMessage); 54 } 55 56 /** 57 * This method displays the given quantity value on the screen. 58 */ 59 private void display(int number) { 60 TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view); 61 quantityTextView.setText("" + number); 62 } 63 64 /** 65 * This method displays the given price on the screen. 66 */ 67 private void displayPrice(int number) { 68 TextView priceTextView = (TextView) findViewById(R.id.price_text_view); 69 priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); 70 } 71 72 /** 73 * This method displays the given text on the screen. 74 */ 75 private void displayMessage(String message) { 76 TextView priceTextView = (TextView) findViewById(R.id.price_text_view); 77 priceTextView.setText(message); 78 } 79 80 }