發表文章

目前顯示的是有「Android」標籤的文章

Xamarin : Designer show error : you need to use a theme.appcompat theme (or descendant) with the design library

圖片
這個問題是出現在使用 Visual Studio 2017 Xamarin 建立 Android 單一檢視應用程式時發生的問題,主要原因應該 原因是這個專案預設使用了 appcompat 主題的 android.support.design.widget.CoordinatorLayout 框架,而預設主題無法解析這個框架導致錯誤 如果你在 Designer 的畫面上點擊 show error 就可以看到這個相關訊息 java.lang.IllegalArgumentException was throw  you need to use a theme.appcompat theme (or descendant) with the design library 如果堅持使用 CoordinatorLayout 解決方式就是去更改 Designer 上的 主題(Theme),將它改為 Theme.AppCompat 樣式 就可以正常顯示了

Xamarin : Android 取得媒體儲存裝置 retrieve emulated or removable storage path

圖片
這是一個很觀念的問題,其實android對於儲存位置的概念跟Windows是不一樣的。 我們所知的 Windows 的儲存媒體,通常都會是以『磁碟機』(Device)概念呈現的,每個 Device 都會被賦予一個『磁碟機代號』(Device Letter),像是 『C:\』『 D:\』 等等。 但是除了 Windows 以外的世界,基本上會是用『路徑』來呈現,由其 Android 的 Dalvik 系統本身就是 java 的支線,所以儲存空間會像是 『/Storage/dev0』『/Storage/dev1』之類的呈現法。 瞭解了以上的概念之後,再來瞭解 Android 對於儲存空間的認定。 一般我們在看儲存位置的時候,直覺上會把手機的儲存空間分為『內建』『內部』『外部』『擴充』等等的用法,因為 Android 有分為內建記憶空間和一個可以擴充的 SD 卡(或是TF卡)。 而我們程式在開發時相關文件都會去 Google 找類似 Internal 或是 External 類的單字搜尋,但實際上找到的文件範例基本上都會牛頭不對馬嘴,最大的原因應該會出在 Android 開發團隊對於這種一般人認定的常規式用法式有很大出入的。 Android 對於內建記憶空間和擴充的SD卡認定如下: Emulated - 內部記憶空間,用的單字是『仿真』,而不是 Internal (內部),是因為 Android 真正內部記憶體指的是ROM,也就是作業系統OS存在的地方,而ROM本身不能經常寫入,因為有寫入壽命限制,但是作為一個作業系統難免會有許多設定或變更或紀錄,那這樣要存在哪裡呢?事實上, Android 手機真正可以讀寫的內建空間其實也是一個 SD 卡,指是手機製造商會在手機裡面直接燒上SD的儲存晶片作為內建儲存空間,凡是OS以外的程式APP或是紀錄都會存在此處,所以把這種內建 SD 記憶體稱為『仿真記憶體』。 Removable - 擴充的記憶體(SD卡或TF卡),這被稱為『可移除式』媒體,名字簡而易瞭,代表這記憶體可以被移除或是安插。 External - 而這個『外部』的意思就是泛指除了 OS 的 ROM 之外的都會稱為 External,所以上面兩個『Emulated』、『Removable』都算在 External 下面。而 Ext

Xamarin : Android : FilePicker 檔案瀏覽

圖片
其實這是用來搞懂 這一篇  Browse Files - Xamarin 範例則來自 GitHub 的 mgmclemore 收集的  A collection of Xamarin.Android sample projects 由於 android 瀏覽檔案時沒有像 Windows 一般有『現成』的檔案瀏覽介面(SHELL)可以使用,所以就非常麻煩的必須自己去作出像檔案瀏覽器一樣的介面。 而且 Xamarin 開發的介面中也沒有可以直接使用的元件,所以這完全必須依賴外部套件才有辦法作出來。 這個套件引用了 Xamarin.Android.Support.v4 這個套件,所以必須先到NuGet去下載這個套件到專案內。

Xamarin : android : 停用按鈕聲音

正常的狀況下,按下Android APP 內的按鈕 Button,裝置都會發出『達、達』『滴、滴』等的聲音,這個聲音不是 APP 產生或是開發出來的。 這是 Android 內置的系統聲音。 如果某些情境開發下,希望能夠關閉這個聲音,可以透過下面兩種方式實現: 1.針對按鈕 Button 操作屬性 程式方式: button. setSoundEffectsEnabled ( false ); 屬性變更 <Button     ...     android: soundEffectsEnabled =" false " /> 2.針對全環境(APP)操作: 在資源檔內建立 res/values/styles.xml 樣式檔 <?xml version="1.0" encoding="utf-8"?> <resources> <style name=" AppBaseTheme " parent=" android:Theme.Black.NoTitleBar ">     <!--         Theme customizations available in newer API levels can go in         res/values-vXX/styles.xml, while customizations related to         backward-compatibility can go here.     --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme">     <!-- All customizations that are NOT specific to a particular API-level can go here. -->     <item name="

Xamarin : Android : 自訂繼承控制項 Coustom class inherit by TextView or other

圖片
很多時候,原始的控制項功能無法滿足社計上的需要時就必須修改控制項了。 這個範例是繼承自 TextView 的自訂元件。 1.首先到專案內新增一個類別 (新增項目→C#類別),名稱為 MyTextView.cs 2.首先要引用下面這個類別,才能使用 IAttributeSet using Android.Util; 3.然後讓這個類別繼承 View,然後要實現相關的 建構子 namespace TestAnimator {     //讓 MyTextView 繼承自 TextView     public class MyTextView : TextView     {         //實現基本的建構子         public MyTextView(Context context) : base(context) { }         public MyTextView(Context context, IAttributeSet attributeSet) : base(context, attributeSet) { }         public MyTextView(Context context, IAttributeSet attributeSet, int defaultStyle) : base(context, attributeSet, defaultStyle) { } 4.接下來,我希望這個 TextView 有一個 MeMe 的屬性,而這屬性是 int 型別,當這個 MeMe 數值改變時,我希望能夠顯示在 Text 上。 因此在 class 下再建立一個內部儲存變數 _meme 與屬性 public int MeMe,然後覆寫 Draw 事件讓它可以把變數內容寫入 Text         //內部變數         private int _meme = 0;                 //屬性設定         public int MeMe         {             get { return _meme; }             set             {                

Xamarin:Android:Animator 控制器簡單使用法

原文說明: 看這裡 這篇主要介紹 Property animators 的常見用法,使用前要引用下面的 Name Space using Android.Animation 這個 animators 有三個子類別 ValueAnimator - 數值計數器,數字更新對象必須以加載是件方式執行 ObjectAnimator - 進階版 ValueAnimator 可以直接指定物件與更新對象 AnimatorSet - 動作集合器,可以把許多 ValueAnimator 和 ObjectAnimator 掛載一起執行,也可以指定執行順序 ValueAnimator 其實這個 ValueAnimator 控制器,說穿了就是一個線性計數器而已,跟繪圖一點關係都沒有,因為它的基本動作就是: 你給予 A 到 B 區間的數字,然後要它在一定時間內跑完,然後在跑數字的時候同時去更新某個變數。 下面有個基本範例,這個範例可以讓顯示的文字從0漸增到100,然後再從100漸減到0,每次的變化需要的時間剛好就1秒: //設定數字 0 到100 的變化 ValueAnimator valueAnimator = ValueAnimator .ofInt(0 , 100); //並且在 1000毫秒內執行完 valueAnimator.SetDuration(1000); //設定為無限次執行 valueAnimator.RepeatCount = ValueAnimator .Infinite; //循環方式為『反向計數』 valueAnimator.RepeatMode = ValueAnimatorRepeatMode .Reverse; //當數字發生變化時,更新TextView的文字顯示 valueAnimator.Update += delegate (object sender, ValueAnimator . AnimatorUpdateEventArgs e) {         //取得變化數值         var newValue = (int)e. Animation . AnimatedValue ;         var tv1 = FindViewById< TextVi

Xamarin : Android : Vibrate 使用裝置震動

圖片
在 Xamarin 下開發 Android 裝置需要振動程式碼如下 void button_vibrator_Click(object sender , EventArgs ea) {     // 建立振動服務     var vibrator = (Vibrator)GetSystemService(Android.Content.Context.VibratorService);     int iDuration = 500; // 500毫秒,振動持續時間       // 執行     vibrator.Vibrate(iDuration); } 這個 Vibrator 需要引用 using Android.OS; 這個服務屬於非同步機制,發出震動後不會等待振動時間完畢,而會繼續執行程式。 另外,還需要賦予適當的使用權限,可以在 專案屬性 調整 或是到 AndroidManifest.xml 裡增加 <uses-permission android:name="android.permission.VIBRATE" /> 建議使用專案屬性調整會比較安全,比較不會打錯字

Xamarin : Android : Lock Screen(View) Orientation 禁止螢幕(畫面)旋轉

在 Xamarin 開發 Android 的時候如果要禁止畫面旋轉 要在 Activity 的程式碼使用下面方式(黃色部分)宣告 [Activity(Label = "ZxingTest", MainLauncher = true, Icon = "@drawable/icon" , ScreenOrientation =Android.Content.PM.ScreenOrientation.Portrait) ] Android.Content.PM.ScreenOrientation 裡面有些列舉屬性,像是: Android.Content.PM.ScreenOrientation.Portrait (豎屏;直向畫面) Android.Content.PM.ScreenOrientation.Landscape (橫屏;橫向畫面) 而大部分網路上說修改 AndroidManifest.xml 裡面更改 <activity> 屬性增加 android:screenOrientation="portrait"等這類方式都是  Android Studio 的開發方式 不是 Ms Visual Studio 的開發方式,所以特別紀錄一下。 為什麼要禁止螢幕旋轉: 除了特定版面排版特性之外, 當每次旋轉螢幕時 Activity 和 View都會被重載 (Reload/Redraw) ,此時畫面上所有物件都會被 摧毀並釋放記憶體空間,由於這個特性,在畫面上的資料如果沒被儲存就會消失的一乾二淨。 當然可以利用 OnSaveInstanceState 和 OnRestoreInstanceState 事件覆寫來處理資料儲存和還原,但是如果不想做太複雜,就直接設定禁止畫面旋轉就可以了。

Xamarin : Android : Using ZXing.Net.Mobile Scan Barcode 掃描讀取條碼/QR碼

圖片
網路上大部份文章在講 Xamarin 開發都用上了 CPL類型的專案, 對於純粹使用 Android 專案使用者要理解真是有點不方便, 這裡是記錄了在 Xamarin 下的 Android 專案如何開發一個讀取 條碼或是 QR碼的功能 個功能是基於 ZXing.Net.Mobile 套件開發的,可以自行把這個套件應用你的專案中 1、假設你經開啟了一個 Android blank Project (空白專案) 2、使用 NuGet 管理員去新增 ZXing.Net.Mobile 套件,並把它安裝到專案中 3、然後到你要呼叫使用 ZXing 操作的 Activity 去新增引用(在這案例裡我是放在 MainActivity.cs 內) using ZXing; using ZXing.Mobile; 4、在顯示畫面(Main.axml)上設計兩個 TextView 和一個 Button,用來啟動掃描和接收掃描的結果 5、然後就是把操作 ZXing 的程式碼加入,在這案例中,我只簡單全部做在 MainActivity 的 OnCreate 內,實際應用上你可以依照需要放置。 using Android.App; using Android.Widget; using Android.OS; using ZXing; using ZXing.Mobile; namespace ZxingTest {     [Activity(Label = "ZxingTest", MainLauncher = true, Icon = "@drawable/icon")]     public class MainActivity : Activity     {         //建立操作物件指標         private TextView _barcodeFormat, _barcodeData;         protected override void OnCreate(Bundle bundle)         {             base.OnCreate(bundle);             // 設定顯示畫