標籤

3C 科技 (1) 免費資源 (1) 使用心得 (2) 版權註記 (1) 遊記 (3) 網路購物 (1) 學習 (4) Android Studio (10) MVC 學習 (2) Unity 學習 (6)
顯示具有 Android Studio 標籤的文章。 顯示所有文章
顯示具有 Android Studio 標籤的文章。 顯示所有文章

2016年1月7日 星期四

webView 旋轉會destroy activity的問題解法

利用webView顯示url後,如果旋轉螢幕,webView的內容就會不見,這是因為旋轉螢幕會將原ACTIVITY destroy 後再重新create,造成部份變數、設定等會消失,參考網路上解法如下:

只要在 AndroidManifest.xml 中的activity 部份加入紅色部份即可。

<activity android:name=".MainActivity" android:configChanges="screenSize|orientation">

2015年11月12日 星期四

使用 NFC 功能

可以參考以下文章

1. http://android-er.blogspot.tw/2014/04/example-of-programming-android-nfc.html

2. http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

下載 android studio example 檔案: (使用 Android Studio 1.4 開發)
https://drive.google.com/file/d/0B1BLJ4r7axiQcFZNTGlDdXFDLTg/view?usp=sharing

使用 ZXING 進行條碼(1~2維) 掃描

這個議題在網路上的範例相當多,但是我也花了許多功夫,才找到可執行的範例,特紀錄如下。

1. 使用 Android Studio 1.4
2. New 一個 Empty Activity的 project
3. 參考 http://lazycatnote.blogspot.tw/2015/03/androidqr-code.html 的說明,但是加的位置及內容略為調整。
 (a) 在 Gradle Scripts / build gradle (Module: app) 加入程式碼:

repositories {
    mavenCentral()

    maven {
        url "http://dl.bintray.com/journeyapps/maven"    }
}

dependencies {
    // Supports Android 4.0.3 and later (API level 15)    compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
    // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.    // If you only plan on supporting Android 4.0.3 and up, you don't need to include this.    compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'
    // Convenience library to launch the scanning and encoding Activities.    // It automatically picks the best scanning library from the above two, depending on the    // Android version and what is available.    compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'
    // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.    // This mostly affects encoding, but you should test if you plan to support these versions.    // Older versions e.g. 2.2 may also work if you need support for older Android versions.    compile 'com.google.zxing:core:3.0.1'}

當出現需要 Sync的訊息時,按下 Sync now (此時會自動下載相關檔案,無需自行下載 ZXING 套件)

(b) MainActivity 內容

package com.example.ray.myapplication2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import com.google.zxing.integration.android.*;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private Activity mainactivity;
    private TextView scan_content;
    private TextView scan_format;
    private Button scan_btn;
    @Override    protected void onCreate(Bundle saveInstanceStat){
        super.onCreate(saveInstanceStat);
        setContentView(R.layout.activity_main);
        init_view();
        scan_btn.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                IntentIntegrator scanIntegrator = new IntentIntegrator(mainactivity);
                scanIntegrator.initiateScan();
            }
        });
    }
    public void onActivityResult(int requestCode, int resultCode, Intent intent){
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if(scanningResult!=null){
            String scanContent=scanningResult.getContents();
            String scanFormat=scanningResult.getFormatName();
            scan_content.setText(scanContent);
            scan_format.setText(scanFormat);
        }else{
            Toast.makeText(getApplicationContext(),"nothing",Toast.LENGTH_SHORT).show();
        }
    }
    private void init_view(){
        this.scan_content=(TextView)findViewById(R.id.scan_content);
        this.scan_format=(TextView)findViewById(R.id.scan_format);
        this.mainactivity=this;
        this.scan_btn = (Button)findViewById(R.id.scan_btn);
    }
}

(c) active_main.xml 加入相對的Button及Textview


4. 完工

5. 範例下載: https://drive.google.com/file/d/0B1BLJ4r7axiQczVIVGhySy1uRGs/view?usp=sharing

2015年10月29日 星期四

Mac 下使用 android studio 的 adb 指令

近來發現手上有不少Mac機器,想乾脆在Mac下裝Android studio。結果第一步就發現找不到adb。尋訪google大神後,發現以下方法:

1. 先將mac開機(按著 cmd+r) 進入recovery mode.
2. 選擇選單上的工具程式 / 終端機
3. 輸入 csrutil disable
4. 輸入 reboot 重開機
5. 正常進入系統後,進入終端機輸入:
   sudo cp ~/Library/Android/sdk/platform-tools/adb  /usr/bin/adb
6. 完工。

參考網址:
1. kiraku.tw/2014/12/3714/
2. http://stackoverflow.com/questions/32659348/operation-not-permitted-when-on-root-el-capitan-rootless-disabled 的回答。

2015年6月5日 星期五

Short Note:快速產生 Override 方法

善用Android Studio 的協助,可以減少記憶一些固定的方法名稱及參數等。以覆寫固有的方法來說,可以用下述方法,快速建出基本的方法框架。

1. 以滑鼠點選要插入方法的位置。
2. 選擇功能表的 Code / Override Methods (如下圖)

3. 選擇所要的方法 (可複選)

4. 按下OK就完工了。




2015年5月20日 星期三

L4: 畫圖

想要在手機上畫個圖,說起來容易,可是似乎又有些摸不著頭緒。在這節中,將要達到一個簡單的目的: 在螢幕上顯示一個圓。單純的目的,讓我們可以一步步了解Android 畫圖的基礎。

1. 要自製一個View的類別(Class),首先在 app / java 下與 MainActivity 同一套件下產生一個新類別 PaintView (自訂名稱)。



按下OK後,會出現 public PaintView 的程式,將其改寫為 public PaintView extends View,然後如前面L3所說,當出現紅色底線毛毛蟲時,按下alt-Enter,讓Android Studio幫忙,選擇建立Constructor,就可得到下列程式。

public class PaintView extends View {
    public PaintView(Context context) {
        super(context);
    }
}

2. 為 PaintView 加上 onDraw方法的改寫,讓App在重繪 PaintView時,能畫出一個圓形。

public class PaintView extends View {
    public PaintView(Context context) {
        super(context);
    }
    protected void onDraw(Canvas canvas){
        Paint paint = new Paint();
        canvas.drawColor(Color.WHITE);  //設定畫布顏色
        paint.setColor(Color.BLACK);    //設定畫筆顏色
        canvas.drawCircle(100,100,50,paint); //在座標100,100處,以paint屬性畫出一個半徑50的圓
    }
}
PaintView 在這個例子中這樣就夠了,但是以後要再加功能,就要到這裡修改。

3. 為 App 畫面加上 PaintView這個CustomView。
如上圖選擇後,將它擺到畫面中即可。

4. 最後將 MainActivity.java中的onCreate 改寫如下即可。
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);    PaintView pp = new PaintView(this);
    setContentView(pp);
}

5. 完工。



2015年5月16日 星期六

L2: UI 設計

Android Studio 中UI的設計與Visual Studio系列的用法不同,如果直接像在Visual Studio中一樣拖拉,光對齊就會讓人火冒三丈,因此下方用一個簡單的範例來介紹,這個範例也可以用來做下一課的界面。假設要設計下面的界面,應該如何進行?


1. Android Studio 提供不同界面排版的方式,在此例中將以 LinearLayout來製作。在產生新專案時,內定為RelativeLayout,因此先點開 app / res / layout / activity_main.xml,找到第一行:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

將 RelativeLayout 改成 LinearLayout。

2. LinearLayout 將畫面視為縱向(Vertical)與橫向(Horizontal)兩種,在此我們將畫面視為4個橫列(Horizontal)由上而下排列(Vertical),如下圖:

所以在component Tree中點選LinearLayout,然後在Properties中將其 orientation選為Vertical.


3. 原專案中就有的 TextView可以先將其刪除。接下來在Palette中拉出4個LinearLayout(Horizontal),然後在Component Tree中選取這四個LinearLayout,在Properties中將Layout:height 輸入要的高度,例如 100dp

4. 從 Pallete中Widgets拉出一個Plain TextView及Text Fields裡的Number到第一橫列,同樣也拉一個Plain TextView及Number到第二橫列,一個Widgets中的Button到第三橫列,最後兩個Plain TextView到第四橫列。

5. 設定字串常數,詳細操作請看前一課介紹。
<string name="height">身高</string>
<string name="weight">體重</string>
<string name="bmi">BMI</string>
<string name="calculate">計算</string>

6. 點選第一個textView,並更改其text屬性為@string/height (按旁邊的 ... 按鈕,就可列出所有可用資源,string中定義的大概都在最下面,點選就ok,不用自己輸入),textSize 為 24dp,再將第一個editText的textSize屬性也設定為24dp,textColor 設定為喜歡的顏色。最後點選第一橫列 LinearLayout(horizontal),設定 gravity為center_vertical。依此動作設定各元件屬性,就可完工囉。

2015年5月15日 星期五

L1: Android 程式的字串常數庫

開發程式時,應該需要顯示一些字串,這些字串可以直接打入程式中,方便但是未來的維護可能較費力,也不利於發展多語言介面。因此可以將字串常數存於特定的xml檔案中,以方便未來維護。

以第一個Hello World程式而言,如果想要將它改成中文版,要如何做呢?

1. 點開 app / res / values,可以看到strings.xml,找出下面一行,這一行就是定義一個字串常數叫 hello_world,內容是 Hello world!。

   <string name="hello_world">Hello world!</string>

將它改成

   <string name="hello_world">歡迎!</string>

再執行程式,就可在手機上看到中文的歡迎了。

2. 點開 app / res / layout / activity_main.xml,注意下圖的各個紅框,你就可以大概了解其中奧妙了。

L1: 手機的第一個程式: Hello World

1. 開啟Android Studtio.
2. 選擇新專案

3. 按下一步 (Next)

4. 選擇需要的SDK,第一個專案勾選第一項就好,下拉選單中支援的Android 系統版本如果沒有所需要的,就要到SDK Manager中去下載及安裝。

5. 後面步驟都案下一步即可,直到按下Finish後,看到下面畫面就可開始動工了。


6. 將手機以 USB線與電腦連結(注意手機要已經打開開發者模式,在與電腦連結後,手機畫面會出現確認允許USB偵錯,確定後即與電腦相連),再按下上圖紅色箭頭所指的 Android Device Monitor,應該就可看到手機了,如果沒看到,重複插拔手機應該就可以了。

7. 按下執行按鈕(或 Shift-F10),可能要等一下子,然後會出現選擇設備對話框,選擇手機,按下OK,你就可以在手機上看到Hello World 的訊息了。

8. 此時在手機中已經安裝了這個app, (都沒改的話,應該叫 My Application),第一個手機程式完工。

Android Studio 與手機無線相連


在PC 下,找到Android的platform-tools目錄 (一般安裝會在c:\使用者\xxx\AppData\Local\Android\sdk\platform-tools,AppData是隱藏目錄,要將檔案總管設定為顯示隱藏目錄才看的到),在命令模式下執下列adb 指令:


adb tcpip 5555

adb connect 手機ip

手機需與電腦在同一子網路, 且手機需要開啟開發人員選項.

經過上方指令即可將Android Studio與手機無線連接.