详情看官网链接:

http://developer.android.com/training/basics/firstapp/index.html

一、创建一个Android 项目


创建android 和创建一个java项目没有什么区别。New -> Android Application Project,出现如下界面,注意的是你需要选择你的SDK 版本

上官网学android之二(Building your First APP)_第1张图片

Minimum Required SDK通常为了让你写的APP支持尽可能多的设备,应该选择最低的可用版本。

Target SDK通常为最高版本

Compile With编译版本,通常也为最高版本的

Theme主题,决定界面的风格

二、启动程序

通过上一步我们已经创建一个出一个安卓的项目了,接下来介绍下里面重要的文件和目录。

AndroidManifest.xml

此文件就再项目的根目录下,它描述了项目基本的特性和定义的组件。

其中一个重要的是<uses-sdk>标签,它说明了项目的SDK版本,其实是根据上一步我们设置来的。通常是这样


上官网学android之二(Building your First APP)_第2张图片

在真实设备上运行你的应用

1. 手机通过USB插上电脑->开启手机的USB debuging功能 -> 设置-> 辅助功能 –> 开发人员工具-> USB 调试 (我的手机是MX2,系统版本应该android 4.2 ,不同的手机可能设置稍有不同)

2. 右键项目,Run as -> Android Application, 稍等片刻出现如下界面


上官网学android之二(Building your First APP)_第3张图片

3. 选择你的设备,点击OK即可,此时注意你的手机应该安装到你的手机上,并且运行了,通常你看的界面如下图


上官网学android之二(Building your First APP)_第4张图片

在虚拟设备上运行你的应用

首先你需要创建一个虚拟设备,在菜单栏下面有一个android virtual device manager,打开新建一个即可


上官网学android之二(Building your First APP)_第5张图片

三、写一个简单的用户 界面

安卓的用户界面通过View ViewGroup对象来完成,View可以理解为视图组件,ViewGroup可以理解为布局,关系如下图。

上官网学android之二(Building your First APP)_第6张图片
创建一个Linear Layout

Layout 是一个布局,在Linear Layout这个布局中的元素(View)都是线性排列的

比如我们修改我们的/res/layout/activity_main.xml 如下

<?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>
接下来我们在linearLayout内部添加一个文本框

<EditText android:id="@+id/edit_message"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="@string/edit_message" />
这时候我们的IDE可以出现错误,这是因为我们没有定义edit_message的字符串,现在我们就去写一个

在/res/valuse/strings.xml中添加如下xml元素,修改之后应该是下面这样

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">MyFirstApp</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="edit_message">Entry a message</string></resources>
修改然后保存这个xml文件之后,错误就没有了。

同理,我们添加一个按钮,在LinearLayout里添加如下代码

  <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button_send" />
同理我们需要在strings.xml中加入button_send的字符串定义,最后我们的activity_main.xml的文件是这样的

<?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" >        <EditText android:id="@+id/edit_message"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="@string/edit_message" />      <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button_send" /></LinearLayout>
strings.xml文件是这样的

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">MyFirstApp</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="edit_message">Entry a message</string>    <string name="button_send">Send</string></resources>
利用上面的启动程序的方法, 看是不是变了?

四、多另外一个Activity

现在我们想当点击按钮的时候另一个界面显示这个界面的文本输入框的内容,怎么做呢

为按钮添加事件,如下图我添加了一个点击事件

<Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button_send"        android:onClick="sendMessage" />
sendMessage就是Activity 中的方法了,我们在类/src/cn/tant/test/MainActivity.java 添加如下方法

public void sendMessage(View view) {    // Do something in response to button}
我们通过Intent类来在两个页面传递数据,在传递数据之前我们先新建一个叫DisplayMessageActivity的Activity 吧

New > Other >Android >Android Activity ,如下图

上官网学android之二(Building your First APP)_第7张图片
此处注意 Hierarchical Parent

完成之后我们得到如下一个类

package cn.tang.test;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.support.v4.app.NavUtils;import android.annotation.TargetApi;import android.os.Build;public class DisplayMessageActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_display_message);// Show the Up button in the action bar.setupActionBar();}/** * Set up the {@link android.app.ActionBar}, if the API is available. */@TargetApi(Build.VERSION_CODES.HONEYCOMB)private void setupActionBar() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {getActionBar().setDisplayHomeAsUpEnabled(true);}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.display_message, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case android.R.id.home:// This ID represents the Home or Up button. In the case of this// activity, the Up button is shown. Use NavUtils to allow users// to navigate up one level in the application structure. For// more details, see the Navigation pattern on Android Design://// http://developer.android.com/design/patterns/navigation.html#up-vs-back//NavUtils.navigateUpFromSameTask(this);return true;}return super.onOptionsItemSelected(item);}}
现在说下Intent的使用,在消息发送端即MainActivity.java中的sendMesage方法中发送消息
public void sendMessage(View view) {Intent intent = new Intent(this, DisplayMesssageActivity.class);    EditText editText = (EditText) findViewById(R.id.edit_message);    String message = editText.getText().toString();    intent.putExtra(EXTRA_MESSAGE, message);    startActivity(intent);}
在接收端DisplayMessageActivity.java中的oncreate方法中接收消息

protected void onCreate(Bundle savedInstanceState) { // 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);}
别忘记了全局变量

public class MainActivity extends Activity {public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

接着我们再运行下程序,是不成功了呢?

更多相关文章

  1. Android 各个版本WebView
  2. Google Maps Android API V2 版本更新导致的科研瓶颈
  3. Android 博客园客户端 (二) 新界面&部分功能
  4. Android原生运行uniapp使用5+app创建项目时的SDK集成步骤(一)
  5. 界面可视化工具------DroidDraw
  6. Android开源项目第二篇——工具库篇
  7. Android项目Android Studio目录结构
  8. Android Ant更新项目
  9. AndroidManifest.xml中 andorid 版本号 versionCode, versionNam

随机推荐

  1. Android新建工程出错-Android+Packaging+
  2. Android绯荤粺鏋舵瀯鍥惧強绠€鍗曠殑绯
  3. Android之自定义TextView学习笔记
  4. Android小项目之九 两种上下文的区别
  5. 鍦?Android(安卓)涓婇€氳繃妯℃嫙 HTTP
  6. android Activity之间数据传递 Parcelabl
  7. google终于把ADB输出的回车换行问题改掉
  8. Android瀛︿範绯诲垪(19)--App绂荤嚎涓嬭浇
  9. Android(安卓)搴旂敤鎬ц兘浼樺寲锛堜竴
  10. Android(安卓)轻量级缓存框架ASimpleCach