Android学习1

来源:互联网 发布:巨人网络公司总部 编辑:程序博客网 时间:2024/06/11 23:13

一  、安装

首先安装java的jdk(这个就不说了,既然你都开始学习Android,java基础肯定是没问题的),安装好了过后,然后就去这个地址:http://developer.android.com/sdk/index.html 下载Android的SDK(我选的是64位的)。解压后你会看到里面有两个文件夹一个Eclipse和一个SDK  这个是google为了方便我们的。

我们先点SDK Manager.exe  安装一些东西


这样android的SDK基本都安装完了!  最后我也存在一个疑问,因为我也是自学吗!   网上有些人没说需要把E:\Program Files\Android   环境\adt-bundle-windows-x86_64-20140321\sdk\tools这个目录放到环境变量path中去  有些又要求放,最后我是放了的!

至于eclipse的话  就用刚刚下下来的就好,他是已经给我们安装好了的。

二、  第一个程序<HelloWorld>

跟建java项目一样右键后选择Android Application project,然后再按照图操作


下一步:默认,再点Next



再点击下一步过后   什么也不用改先用默认的我们做一遍看看!  然后直接finish

Android项目创建好了,我们再创建一个虚拟机



做好这些后直接点击ok   虚拟机就创建好了  现在选中你创建好的虚拟机点击右边的Starts  启动,第一次启动有点慢!


这是我刚刚创建的


运行下我们前面已经创建好的android项目,和运行java项目基本一样

选中项目名右键,选择Run AS   ----》  Android Application  然后就等待一会儿   就会在虚拟机上面呈现出结果。(本来是应该显示hello World的,但被我改了个文件所以现在就是这样的,我们分析下这个代码)


三、  分析

第一个程序运行成功,我们现在分析一下。

程序包结构是:com.example.helloandroid,包内只有一个类:MainActivity,我们先看看:

package com.example.helloandroid;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.os.Build;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (savedInstanceState == null) {getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}/** * A placeholder fragment containing a simple view. */public static class PlaceholderFragment extends Fragment {public PlaceholderFragment() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_main, container,false);return rootView;}}}

MainActivity.java继承自Activity,内部只有两个函数,均是覆盖Activity的。每一个 Android程序必须继承Activity或其子类,有关Activity生命周期相关的有以下主要函 数,这些函数交给系统调研。

MainActivity.java程序很简单,程序建立时,onCreate中加载 R.layout.activity_main资源,onCreateOptionsMenu加载R.menu.activity main资源, 这两个资源都是int类型,定义在R.java类中,R类不要手动编辑,系统会根据res设置自 动修改。

layout.fragment_main和menu.main均是int类型,分别对应工程资源列表 res下layout的fragment_main.xml,menu的main.xml。 layout.fragment_main.xml中定义了TextView,text是 @string/helloworld,menu.main.xml中定义了item,title是 @string/action_settings,这两个都是字符串,定义在res下values的strings.xml,strings.xml定义了三个字符串,程序运行后,主界面显示一个TextView 控件,内容是strings.xml中的helloworld(Hello world!),菜单只有一个item,名字是strings.xml 中的action_settings(Settings),

我将Values下的String.xml里面的值改了所以你们看到的结果不是helloAndroid了。

layout.fragment_main.xml

<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.helloandroid.MainActivity$PlaceholderFragment" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

menu.main.xml

<menu 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"    tools:context="com.example.helloandroid.MainActivity" >    <item        android:id="@+id/action_settings"        android:orderInCategory="100"        android:title="@string/action_settings"        app:showAsAction="never"/></menu>

values.strings.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">firest android</string>    <string name="hello_world">the machine of awareness!</string>    <string name="action_settings">first menu</string></resources>

作者:杨静(YangJing)
出处: [杨静の专栏]   (博文连接)




0 0
原创粉丝点击