2022年4月25日 星期一

App inventor: AIX擴充檔案寫法-檔案練習範本

https://ide.niotron.com 編譯建構AIX檔案的網站

https://www.youtube.com/watch?v=P6hfXQLtEh8 這影片有教怎麼使用上面編譯擴充AIX檔案的方法.

以下為範例,切記要自己import需要的包,否則會出錯。

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@DesignerComponent(
version = 1,
description = "",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")

@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")

public class TA extends AndroidNonvisibleComponent {

//Activity and Context
private Context context;
private Activity activity;

public TA(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}

@SimpleFunction(description = "Sample Function Generated by Niotron")
//建立某範圍數字不重複取值函式,參數1為起始值,參數2為結束值,參數3為要取得的數目數
public static List<Integer> uniqueNumber(int startNumber,int endNumber,int counts){
//創建HashMap為HashMap<Integer, Integer>
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
//返回hashMap鍵值的 set
Set<Integer> keySet = hashMap.keySet();
int randomCount=0;
//當hashMap清單的長度小於你設定取得的數量,就執行裡面的程式
while(hashMap.size()<counts){
//取得變數
randomCount=(int)(Math.random()*(endNumber-startNumber))+startNumber;
//透過for決定要跑幾次迴圈,迴圈跑的次數是(你設定取得的數量-目前已有的haspmap數量)
//,因為主鍵值相同會覆蓋,所以不用排除,如果不夠就重跑上一層的while,直到滿足需要的次數
for(int i=0;i<(counts-hashMap.size());i++){
//把每次的隨機取出值加入,後面的值應該就是隨便都可以,這裡設定的是1.....到你要的次數。
hashMap.put(randomCount, i+hashMap.size());
}
}
//建立number清單,並將Haspmap主鍵存入
List<Integer> number = new ArrayList<Integer>(keySet);
//將清單利用Collections.sort排序
Collections.sort(number);
return number;
}

@SimpleEvent(description = "Test Event Generated by Niotron")
public void TestEvent(){
EventDispatcher.dispatchEvent(this, "TestEvent");
}
}



其它:https://programtalk.com/java-more-examples/@com.google.appinventor.components.annotations.SimpleProperty(category%20=%20PropertyCategory.APPEARANCE)/
 

沒有留言:

張貼留言