当前位置: 移动技术网 > IT编程>移动开发>Android > Android studio创建第一个app

Android studio创建第一个app

2019年07月24日  | 移动技术网IT编程  | 我要评论

万年党建,宜良县事业单位招聘,异侠小说网

本文实例为大家介绍了android studio创建第一个app的详细步骤,供大家参考,具体内容如下

1.创建helloworld项目

任何编程语言写出的第一个程序毫无疑问都会是hello world,这已经是自20世纪70年代一直流传下来的传统,在编程界已成为永恒的经典,那么今天就来使用android studio创建第一个app(hello world)。
首先运行android studio,在弹出的快捷窗口中新建一个项目。

这里写图片描述

点击上面截图中的“star a new android studio project”,然后会弹出“create new project”对话框。其中application name代表应用名称,此应用安装带手机后会在手机上显示该名称。在这里我们填入hello world(在以后的项目名通常不加空格)。如下图所示

这里写图片描述

接下来直接点击“next”,会弹出“add a activity to mobile”对话窗口,在这里我们选择“ empty activity”,至于其他的 activity(后面称为活动)以后会介绍。

这里写图片描述

点击next后填入activity name就行,在这里activity name就命名为helloworld activity。

这里写图片描述

点击finish,一个android项目就创建完成了。

2.运行helloworld项目
2.1使用 android studio自带模拟器运行。

这里写图片描述

2.2使用第三方模拟器 。

在这里我是用的是genymotion模拟器,个人觉得非常好用。同样也能够通过插件跟android studio关联在一起。
至于怎么下载genymotion模拟器并且跟android studio关联在一起,这些都是平台的 搭建,作为一个开发者首先就应该学会怎样磨好自己的工具,中国古话工欲善其事必先利其器,咳咳,扯远了!但是真的要自己动手搭建平台。

这里写图片描述 

在genymotion里面,我已经下载好了 一款模拟器,android 4.1版本的samsung note 2。
首先,在上面截图中点击“star”将其打开,样子就是这样的 (桌面背景我换了下)。

这里写图片描述

然后,在android studio中点击“run”这里写图片描述,在弹出的窗口中选择刚刚打开的模拟器。

 这里写图片描述 

点击“ok”一切都好了,等待一下(根据电脑速度)就会在模拟器中看到刚刚创建的hello world项目,并且android studio已经帮我们将其他的代码都创建好了。

 这里写图片描述

3、分析下创建的项目目录。

这里写图片描述

3.1、manifest
这里面其实是androidmanifest.xml文件,这是整个android项目的配置文件,在程序中定义的四大组件(activity–活动,service–服务,broadcastreceiver–广播接收器,content provider–内容提供器)。下面代码就是androidmanifest.xml。

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.chencong.helloworld"
     xmlns:android="http://schemas.android.com/apk/res/android">

  <application
    android:allowbackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsrtl="true"
    android:theme="@style/apptheme">
    <activity android:name=".helloworldactivity">
      <intent-filter>
        <action android:name="android.intent.action.main"/>
        <category android:name="android.intent.category.launcher"/>
      </intent-filter>
    </activity>
  </application>
</manifest>

在手机桌面上的应用程序,用户点击后会启动程序,并且看到的第一个活动(页面)是程序的主活动,主活动需要在androidmanifest.xml中进行声明。

<activity android:name=".helloworldactivity">
      <intent-filter>
        <action android:name="android.intent.action.main"/>
        <category android:name="android.intent.category.launcher"/>
      </intent-filter>
    </activity>

如果该活动(activity)不是主活动,那么在androidmanifest.xml的配置应该省去intent-filter标签中的内容。因为里面的action是声明这个activity是主活动。

3.2、java
这里所放的是java代码的地方,它的含义和 在eclipse中java项目中src是一样的 ,打开后会发现刚刚我们创建的helloworldactivity文件就在里面。

package com.example.chencong.helloworld;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;

public class helloworldactivity extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_hello_world);
  }
}

在这里会插入点activity的生命周期,每个活动都是从oncreate( )方法开始的。

这里写图片描述

了解更过activity生命周期的知识也可以看看这篇博客。activity生命周期
首先我们需要了解的是,项目中的任何活动都应该重写activity的oncreate( )方法(高版本为appcompatactivity).代码如下:

public class helloworldactivity extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
  }
}

可以看到,oncreate( )方法很简单,就是调用父类的oncreate( )方法,当然这只是默认的,后面我们可以在里面加入 自己的逻辑。

例如:时间点击事件。(使用显示intent)

public class firstactivity extends appcompatactivity {

  protected void oncreate(bundle savedinstancestate){
    super.oncreate(savedinstancestate);
    /*不在活动中显示标题栏,一定要在setcontentview()之前执行*/
    setcontentview(r.layout.first_layout);
    button button1=(button)findviewbyid(r.id.button_1);
    button1.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent=new intent(firstactivity.this,secondactivity.class);
        startactivity(intent);
      }
    });
  }

但是我们发现里面其实还有一行代码

setcontentview(r.layout.activity_hello_world);
在代码中引用布局文件,调用r.layout.activity_hello_world就可以看到activity_hello_world.xml的id,然后将值传给setcontentview( )方法即可。

3.3、res
这个目录下的文件就有点多了,简单点说就是在你的项目中使用到的图片,布局,字符串等资源都要存放在这个目录下。当然在这个目录下还有很多子目录。

3.3.1、mipmap

仅仅用于存放应用图标,而且还是多个相同名称的,只是分辨率不同,系统可以根据分辨率不同进行优化使用不同的分辨率的

图标。

这里写图片描述

3.3.2、drawable

其他的图标资源

3.3.3、values

字符串

这里写图片描述

在上面创建的项目中,运行时显示hello world,其字符串就是存放在string.xml文件当中

<resources>
    <string name="app_name">hello world</string>
</resources>

3.3.4、layout
布局文件

这里写图片描述

下面是activity_hello_world.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
  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:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  tools:context="com.example.chencong.helloworld.helloworldactivity">

  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world!"/>
</relativelayout>

这仅仅是一个布局文件,但是上面的textview标签中的android:text=”hello world “并不是程序运行时显示的字符串,真正的在string.xml文件当中,在上面values中已经讲解了。

以上就是本文的全部内容,希望对大家使用android studio创建你的第一个项目hello world有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网