Android Training: 创建第一个APP
搭建环境
- 下载 Android Studio
- 下载 SDK tools SDK Manager
创建应用
app/src/main/res/layout/activity_my.xml
布局文件
Text View
App bar
Floating action button
Material Design
app/src/main/java/com.example.myfirstapp/MyActivity.java
start activity
load layout file
app/src/main/AndroidManifest.xml
fundamental characteristics of the app
define components
app/build.gradle
- 使用Gradle编译和构建App
- compiledSdkVersion: 编译App的平台版本
- applicationId
- minSdkVersion
- targetSdkVersion
- Building Your Project with Gradle
/res包含应用的resources
- drawable-density: drawable resources
- layout: user interface
- menu
- mipmap: Launcher icons
- values
在真机上运行
- Enable USB debugging
通过command line运行
gradlew.bat assembleDebug
adb install app/build/outputs/apk/app-debug.apk
创建简单User Interface
- View: UI widgets such as buttions or text fields
- ViewGroup: invisible view containers
- Define UI in XML using a hierarchy of UI elements
- Layouts are subclasses of the ViewGroup
创建线性布局 Linear Layout
- 修改content_my.xml为:
<LinearLayout 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"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my">
添加 Text Field
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
添加 Button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"/>
Make the Input Box Fill in the Screen Width
android:layout_weight="1"
android:layout_width="0dp"
启动另一个Activity
- 为Button添加android:onClick属性
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
- 在MyActivity.java中添加sendMessage方法
public void sendMessage(View view) {
// Do something in response to button
}
public
void
参数唯一(View类型,代表被点击的视图)
创建Intent
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
创建DisplayMessageActivity
接收Intent中的消息
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);