2017Google Study Jams系列之实践3A&

2017-06-02  本文已影响21人  娄叔啊喂

@极简主义患者/社交控/伪技术宅/沉迷幻想不能自拔的文艺少年
不定期更新的文字平台:微博 简书

1.方法

2.资源

资源文件都在项目的res文件夹下,Java文件都在java文件夹下,资源包括图像/文本字符串/颜色/宽度和高度的维度/XML文件/音频视频文件等
应用编译时,AAPT工具会产生一个R类(R.java),在这个类中每一个资源都自己的ID,且此ID在R类中有自己相应的资源类型格式

Resource Type In Java Code In XML
Image R.drawable.photo @drawable/photo
String R.string.hello @string/hello
Layout XML file R.layout.activity_main @layout/activity_main
ID R.id.price_text_view @id/price_text_view
Color R.color.red @color/red

3.类和对象

//Object Variable Name.Method Name(Input Arguments)
titleTextView.setText("News");
/*if use this method in the class
Method Name(Input Arguments)
or
this.Method Name(Input Arguments)
*/
setText("News");
this.setText("News");

*4.完善卖咖啡app界面etc.

此节为视频中既有实例的代码片段,可参考之从而思考造轮子的基本步骤,学会举一反三,利用基础知识+Search Engines完成

<CheckBox
            android:id="@+id/whipped_cream_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="24dp"
            android:text="Whipped cream"
            android:textSize="16sp"/>
CheckBox whippedCreamCheckBox = (CheckBox)findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
<EditText
            android:id="@+id/name_field"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Name"
            android:inputType="textCapWords"/>
EditText text = (EditText)findViewById(R.id.name_field);
String name = text.getText().toString();
private int calculatePrice(boolean addWhippedCream, boolean addChocolate){
        int basePrice = 5;
        if(addWhippedCream){
            basePrice += 1;
        }
        if(addChocolate){
            basePrice += 2;
        }
        return quantity * basePrice;
}
public void increment(View view) {
        if(quantity == 100){
            Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
        }
        quantity = quantity + 1;
        displayQuantity(quantity);
    }
public void decrement(View view) {
        if(quantity == 1){
            Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
        }
        quantity = quantity - 1;
        displayQuantity(quantity);
    }
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + name);
        intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
        if(intent.resolveActivity(getPackageManager()) != null){
            startActivity(intent);
        }
上一篇 下一篇

猜你喜欢

热点阅读