android:building your first app

AndroidManifest.xml
The manifest file describes the fundamental characteristics of the app and defines each of its components. You'll learn about various declarations in this file as you read more training classes.
src/
Directory for your app's main source files. By default, it includes an Activity class that runs when your app is launched using the app icon.
res/
Contains several sub-directories for app resources. Here are just a few:
drawable-hdpi/
Directory for drawable objects (such as bitmaps) that are designed for high-density (hdpi) screens. Other drawable directories contain assets designed for other screen densities(密度).
layout/(用户界面)
Directory for files that define your app's user interface.
values/(存储变量值)
Directory for other various XML files that contain a collection of resources, such as string and color definitions.
 
//创建一个activity_main.xml,match_parent代表着充满整个屏幕
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
</LinearLayout>
<!-- 
首次定义一个id需要用 @+id/message相当于form表单中的id="message"
android:hint代表这默认的文本框为空时填写的内容
@string/edit_message表示从string.xml中取出名为edit_message的字段值
wrap_content代表充满view
下面为一个文本框和一个按钮
-->
<EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content" <!-- 可改为0 -->
        android:layout_height="wrap_content" <!-- 可改为0dp -->
        android:hint="@string/edit_message" />
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" 
		android:onClick="sendMessage"/> <!-- 点击时,调用MainActivity.java下的sendMessage方法 -->
 
//创建一个strings.xml放在res/values下,用来存储变量值
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>
 
//创建一个MainActivity.java在src/com.exmple.my/下面
package com.example.my.first.app;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
 
public class MainActivity extends Activity {
	//为了intent.putExtra方法的使用,必须现在这里定义EXTRA_MESSAGE
	public final static String EXTRA_MESSAGE = "com.example.my.MESSAGE";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	//必须是公共的,有void返回值,view作为唯一的参数
	public void sendMessage(View view) {
		//intent是一个对象,经常被用来启动另一个活动
		//第一个参数为this,第二个为要启动的活动
		Intent intent = new Intent(this, DisplayMessageActivity.class);
 
		//intent还可以将数据传递给要调用的activity
		//根据id获取输入的内容
    	EditText editText = (EditText) findViewById(R.id.edit_message);
 
		//赋值给message
    	String message = editText.getText().toString();
 
		//putExtra的参数相当于key,value
    	intent.putExtra(EXTRA_MESSAGE, message);
 
    	//开始activity
    	startActivity(intent);
	}
}
 
//创建一个DisplayMessageActivity.java在src/com.exmple.my/下面
package com.example.my;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class DisplayMessageActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //所有的Activity必须继承父Activity的onCreate方法
 
        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 
        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
 
        //为活动布局设置文本视图
        setContentView(textView);
    }
}

Related posts:

Leave a Reply

Your email address will not be published.