前言: auto.js的ui可以在將代碼打包的時候方便用戶的操作,可以設計一個舒服的界面來執行手機腳本。
知識梳理篇:
ui的按鈕shi
// 開頭必須要加,否則腳本不會以ui模式運行
"ui";
// 這裏是用xml來編寫的界面
ui.layout(
// vertical 表示的是垂直佈局
<vertical>
<button text="第一個按鈕"/>
<button text="第二個按鈕"/>
</vertical>
);
// 開頭必須要加,否則腳本不會以ui模式運行
"ui";
// 這裏是用xml來編寫的界面
ui.layout(
// horizontal 表示的是佈局
<horizontal>
<button text="第一個按鈕"/>
<button text="第二個按鈕"/>
</horizontal>
);
// 開頭必須要加,否則腳本不會以ui模式運行
"ui";
// 這裏是用xml來編寫的界面
ui.layout(
// bg可以表示界面的背景
<vertical bg="#ff0000">
// textSize可以表示字體的大小
<button text="第一個按鈕" textSize="20sp"/>
<button text="第二個按鈕"/>
</vertical>
);
控件和佈局都屬於視圖
w是width的縮寫,*表示儘量填滿父佈局,auto表示根據View的內容自動調整
"ui";
ui.layout(
<horizontal>
<button w="auto" text="自適應寬度"/>
<button w="*" text="填滿父佈局"/>
</horizontal>
);
"ui";
ui.layout(
<horizontal>
<button w="200" text="寬度200dp"/>
<button w="100" text="寬度100dp"/>
</horizontal>
);
h爲height的縮寫,使用情形和w一樣
獲取簡單的屬性,並且調用
"ui";
ui.layout(
<frame>
<button id="ok" text="哇哈哈"/>
</frame>
);
// 通過ui.ok獲取到按鈕的控件
toast(ui.ok.getText());
gravity表示額的是"重力",用於決定View的內容相對View的位置(ravity="right|bottom"的View他的內容會在右下角)
可以設置爲
- left 靠左
- right 靠右
- top 靠頂部
- bottom 靠底部
- center 居中
- center_vertical 垂直居中
- center_horizontal 水平居中
"ui";
ui.layout(
<frame>
<button gravity="right" w="*" h="auto" text="你看我的文字是靠哪兒的"/>
</frame>
);
"ui";
ui.layout(
<frame w="*" h="*">
<button layout_gravity="center" w="auto" h="auto" text="居中的按鈕"/>
<button layout_gravity="right|bottom" w="auto" h="auto" text="右下角的按鈕"/>
</frame>
);
margin方法:
- margin爲View和其他View的間距,他包括四個值:
- marginLeft 左外邊距
- marginRight 右外邊距
- marginTop 上外邊距
- marginBottom 下外邊距
margin="marginAll" 指定各個外邊距都是該值。例如margin="10"表示左右上下邊距都是10dp。
margin="marginLeft marginTop marginRight marginBottom" 分別指定各個外邊距。例如margin="10 20 30 40"表示左邊距爲10dp, 上邊距爲20dp, 右邊距爲30dp, 下邊距爲40dp
margin="marginHorizontal marginVertical" 指定水平外邊距和垂直外邊距。例如margin="10 20"表示左右邊距爲10dp, 上下邊距爲20dp。
"ui";
ui.layout(
<horizontal>
<button margin="30" text="距離四周30"/>
<button text="普通的按鈕"/>
</horizontal>
);
"ui";
ui.layout(
<horizontal>
<button marginLeft="50" text="我是特殊的按鈕"/>
<button text="普通的按鈕"/>
</horizontal>
);
bg表示背景:
- bg="#00ff00"設置背景爲綠色(RGB格式顏色),bg="file:///sdcard/1.png"設置背景爲圖片
- alpha表示的View的透明度,foreground表示的是View的前景,minHeigh表示的是View的最小高度
- visbility表示的是View的可見性,決定View是否可以顯示出來。
- rotation,View的旋轉角度
文本控件的一些屬性:
- text文本控件,例如:text="一段文字"
- textColor,例如:<text text="紅色字體" textColor="red"/>
- textSize,設置字體的大小,<text text="超大字體" textSize="40sp"/>
- textStyle,可以設置字體的樣式,bold 加粗字體,italic 斜體,normal 正常字體(可以使用|將他們結合)
設置文本控件的行數:
"ui";
ui.layout(
<vertical>
<text id="myText" line="3"/>
</vertical>
);
// 通過\n進行換行
ui.myText.setText("第一行\n第二行\n第三行\n第四行");
按鈕控件button:
- Widget.AppCompat.Button.Colored 帶顏色的按鈕
- Widget.AppCompat.Button.Borderless 無邊框按鈕
- Widget.AppCompat.Button.Borderless.Colored 帶顏色的無邊框按鈕
"ui";
ui.layout(
<vertical>
<button style="Widget.AppCompat.Button.Colored" text="我是漂亮的按鈕"/>
</vertical>
);
輸入控件:input(有下面這個例子就差不多足夠了)
"ui";
ui.layout(
<vertical padding="16">
<text textSize="16sp" textColor="red" text="請輸入使用者姓名"/>
<input id="name"/>
<button id="ok" text="確定"/>
</vertical>
);
// 點擊確定按鈕以後要執行的動作
ui.ok.click(function(){
// 通過getText()獲取輸入的內容
var name = ui.name.getText();
toast(name + "您好,歡迎使用本軟件");
})
// hint爲提示效果
"ui";
ui.layout(
<vertical>
<input hint="請輸入您的姓名"/>
</vertical>
);
ps:textColorHint指定輸入提示的字體顏色。textSizeHint指定輸入提示的字體大小。
指定輸入框可以輸入的文本類型。可選的值爲以下值及其用"|"的組合:
- date 用於輸入日期。
- datetime 用於輸入日期和時間。
- none 沒有內容類型。此輸入框不可編輯。
- number 僅可輸入數字。
- numberDecimal 可以與number和它的其他選項組合,以允許輸入十進制數(包括小數)。
- numberPassword 僅可輸入數字密碼。
- numberSigned 可以與number和它的其他選項組合,以允許輸入有符號的數。
- phone 用於輸入一個電話號碼。
- text 只是普通文本。
- textAutoComplete 可以與text和它的其他選項結合, 以指定此字段將做自己的自動完成, 並適當地與輸入法交互。
- textAutoCorrect 可以與text和它的其他選項結合, 以請求自動文本輸入糾錯。
- textCapCharacters 可以與text和它的其他選項結合, 以請求大寫所有字符。
- textCapSentences 可以與text和它的其他選項結合, 以請求大寫每個句子裏面的第一個字符。
- textCapWords 可以與text和它的其他選項結合, 以請求大寫每個單詞裏面的第一個字符。
- textEmailAddress 用於輸入一個電子郵件地址。
- textEmailSubject 用於輸入電子郵件的主題。
- textImeMultiLine 可以與text和它的其他選項結合,以指示雖然常規文本視圖不應爲多行, 但如果可以, 則IME應提供多行支持。
- textLongMessage 用於輸入長消息的內容。
- textMultiLine 可以與text和它的其他選項結合, 以便在該字段中允許多行文本。如果未設置此標誌, 則文本字段將被限制爲單行。
- textNoSuggestions 可以與text及它的其他選項結合, 以指示輸入法不應顯示任何基於字典的單詞建議。
- textPassword 用於輸入密碼。
- textPersonName 用於輸入人名。
- textPhonetic 用於輸入拼音發音的文本, 如聯繫人條目中的拼音名稱字段。
- textPostalAddress 用於輸入郵寄地址。
- textShortMessage 用於輸入短的消息內容。
- textUri 用於輸入一個URI。
- textVisiblePassword 用於輸入可見的密碼。
- textWebEditText 用於輸入在web表單中的文本。
- textWebEmailAddress 用於在web表單裏輸入一個電子郵件地址。
- textWebPassword 用於在web表單裏輸入一個密碼。
- time 用於輸入時間。
例如:
<input inputType="number|numberDeciml"/>
輸入密碼的提示框例如:
<input password="true"/>
圖片控件:img
可以是本地的,也可以是網絡的(網絡的可能會存在一些bug,這裏我測試的幾個網絡圖片,有一些使不可以用的)。
"ui";
ui.layout(
<frame>
<img src="file:///sdcard/黑軟/美圖保存/1.png"/>
</frame>
);
控件高度(可以控制控件的高度,詳情可以看auto.js的文檔)
"ui";
ui.layout(
<vertical h="100dp" weightSum="5">
<text layout_weight="1" text="控件1" bg="#ff0000"/>
<text layout_weight="2" text="控件2" bg="#00ff00"/>
<text layout_weight="1" text="控件3" bg="#0000ff"/>
</vertical>
);