App開發預備工做

工做中有作過手機App項目,前端和android或ios程序員配合完成整個項目的開發,開發過程當中與ios程序配合基本沒什麼問題,而android各類機子和rom的問題不少,這也讓我產生了學習android和ios程序開發的興趣。因而凌晨一點睡不着寫了第一個android程序HelloAndroid,po出來分享給其餘也想學習android開發的朋友,這麼傻瓜的Android開發入門文章,有一點開發基礎的應該都能看懂。

1、準備工做

主要以我本身的開發環境爲例,下載安裝JDK和Android SDK,假如你沒有現成的IDE,你能夠直接下載SDK完整包,裏面包含了Eclipse,若是有IDE那麼你能夠滾動到下面選擇USE AN EXISTING IDE,而後安裝SDK,若是你的SDK在安裝時找不到JDK目錄,你能夠在系統環境變量裏添加JAVA_HOME變量,路徑爲你的JDK目錄,個人IDE是IntelliJ IDEA,都裝好之後開始配置IDE增長SDK支持。

首先,打開Android SDK Manager把Android 4.0以上版本的未安裝的都打勾裝上,根據你我的實際狀況,若是你只打算用本身的手機測試,那就把你機子系統同樣版本的SDK包裝上,下載時間有點長。

Android SDK Manager

而後打開IDE建立新項目,IDEA比較智能,若是你裝好了SDK,新建項目裏就會出現Android的Application Module,選擇後右邊Project SDK爲空,點擊New按鈕,找到SDK目錄肯定,下拉列表就會列出已經安裝的各個版本的SDK,選擇本身須要的版本,若是是第一次設置,IDE會提醒你先設置JDK,根據提示找到JDK目錄便可。

select-android-sdk

填好項目名稱後下一步選擇USB Device,而後完成項目構建,IDE會自動生成基本的項目所需的文件及目錄。

new-android-project

android-project-files

2、代碼編寫

作好準備工做後,終於能夠開始寫咱們的hello android了,在開始編寫代碼以前,咱們先了解幾個文件:

res/layout/main.xml App主窗體佈局文件,你的應用長什麼樣都在這邊定義,有Design和Text兩種模式

res/values/strings.xml 能夠理解爲i18n文件,這個文件用來存放程序調用的各類字符串

src/com/example/helloandroid/MyActivity.java 這個就是咱們的主程序類,等下要實現的功能都在這個文件裏添加

首先爲應用添加一個id爲hellotextView的textview和一個id爲hellobutton的button,mail.xml 代碼以下:
複製代碼 代碼以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="180dp"
            android:text="@string/default_message"
            android:id="@+id/hellotextView" android:textColor="#00ff00" android:gravity="center"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send"
            android:id="@+id/hellobutton" android:layout_gravity="center"/>
</LinearLayout>

代碼和控件用到的字符串定義以下:

複製代碼 代碼以下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">helloandroid by hiwanz</string>
    <string name="button_send">Say something</string>
    <string name="default_message">Click button below!</string>
    <string name="interact_message">You just clicked on the Button!</string>
</resources>

主程序中定義button點擊後改變textview顯示的文本,而且彈出Toast提示信息,代碼以下:
複製代碼 代碼以下:

package com.example.helloandroid;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //獲得按鈕實例
        Button hellobtn = (Button)findViewById(R.id.hellobutton);
        //設置監聽按鈕點擊事件
        hellobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //獲得textview實例
                TextView hellotv = (TextView)findViewById(R.id.hellotextView);
                //彈出Toast提示按鈕被點擊了
                Toast.makeText(MyActivity.this,"Clicked",Toast.LENGTH_SHORT).show();
                //讀取strings.xml定義的interact_message信息並寫到textview上
                hellotv.setText(R.string.interact_message);
            }
        });
    }
 
}

代碼寫好後,電腦經過USB數據線鏈接手機,手機系統設置裏的開發人員選項裏打開USB調試,在IDE中直接點Run就能夠在手機上看到運行的效果了。

helloandroid-1

helloandroid-2

應用打包

應用開發完成後就要打包發佈了,在IDE的Build菜單下選擇Generate Signed APK來打包應用

generate-signed-apk

在彈出的Wizard對話框中須要指定簽名的Key,一開始沒有Key你能夠點擊Create New來新建一個Key用於簽名,填入簽名所需的一些字段後生成Key文件
signification-keygen

使用生成的Key來簽名應用包

apk-publish-wizard

apk-publish-wizard-done

完成編譯後會在剛纔咱們設置的Designation APK path下生成咱們的helloandroid.apk應用包,接下來要怎麼安裝應用應該不用說了吧,咱們的第一個Android App就這樣誕生了。app開發推薦

如對本文有疑問,請提交到交流社區,廣大熱心網友會爲你解答!! 點擊進入社區
您可能感興趣的文章:

    Android基礎之使用Fragment控制切換多個頁面
    六款值得推薦的android(安卓)開源框架簡介
    android TextView設置中文字體加粗實現方法
    Android Bitmap詳細介紹
    android壓力測試命令monkey詳解
    Android 動畫之ScaleAnimation應用詳解
    android調試工具DDMS的使用詳解
    android PopupWindow 和 Activity彈出窗口實現方式
    android Handler詳細使用方法實例
    Android按鈕單擊事件的四種經常使用寫法總結
    解決Android SDK下載和更新失敗的方法詳解
    android listview優化幾種寫法詳細介紹
    Android 如何修改APK的默認名稱

Tags:Android APP開發 入門教程
相關文章

    2016-03-03解析Android中的Serializable序列化
    2017-03-03Android 中ScrollView嵌套GridView,ListView的實例
    2017-03-03android RecyclerView側滑菜單,滑動刪除,長按拖拽,下拉刷新上
    2016-11-11Android打造流暢九宮格抽獎活動效果
    2016-09-09Android RecyclerView 數據綁定實例代碼
    2016-10-10Android仿小米安全中心檢測進度條效果
    2017-03-03Android常見的幾種內存泄漏小結
    2016-08-08Android自定義控件之自定義屬性(二)
    2017-01-01Android 彈出軟鍵盤所遇到的坑及解決方法
    2017-02-02Android實現網絡加載時的對話框功能前端

相關文章
相關標籤/搜索