添加TermuxButton按钮控件类
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Thu Apr 30 02:41:13 GMT 2026
|
||||
#Thu Apr 30 03:47:19 GMT 2026
|
||||
stageCount=26
|
||||
libraryProject=
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.25
|
||||
buildCount=54
|
||||
buildCount=60
|
||||
baseBetaVersion=15.11.26
|
||||
|
||||
@@ -17,10 +17,19 @@ public class TermuxButtonModel extends BaseBean {
|
||||
String exeCommand;
|
||||
String workDir;
|
||||
|
||||
// 新增属性
|
||||
boolean isCommit;
|
||||
String commitTitle;
|
||||
String commitInfo;
|
||||
|
||||
public TermuxButtonModel() {
|
||||
this.buttonName = "";
|
||||
this.exeCommand = "";
|
||||
this.workDir = "";
|
||||
// 新增属性默认初始化
|
||||
this.isCommit = false;
|
||||
this.commitTitle = "";
|
||||
this.commitInfo = "";
|
||||
}
|
||||
|
||||
public void setButtonName(String buttonName) {
|
||||
@@ -47,9 +56,35 @@ public class TermuxButtonModel extends BaseBean {
|
||||
return workDir;
|
||||
}
|
||||
|
||||
// ========== 新增属性 Get & Set ==========
|
||||
public boolean isCommit() {
|
||||
return isCommit;
|
||||
}
|
||||
|
||||
public void setCommit(boolean commit) {
|
||||
isCommit = commit;
|
||||
}
|
||||
|
||||
public String getCommitTitle() {
|
||||
return commitTitle;
|
||||
}
|
||||
|
||||
public void setCommitTitle(String commitTitle) {
|
||||
this.commitTitle = commitTitle;
|
||||
}
|
||||
|
||||
public String getCommitInfo() {
|
||||
return commitInfo;
|
||||
}
|
||||
|
||||
public void setCommitInfo(String commitInfo) {
|
||||
this.commitInfo = commitInfo;
|
||||
}
|
||||
|
||||
// 修复原来错误的返回类名
|
||||
@Override
|
||||
public String getName() {
|
||||
return UserInfoModel.class.getName();
|
||||
return TermuxButtonModel.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,17 +93,32 @@ public class TermuxButtonModel extends BaseBean {
|
||||
jsonWriter.name("buttonName").value(getButtonName());
|
||||
jsonWriter.name("exeCommand").value(getExeCommand());
|
||||
jsonWriter.name("workDir").value(getWorkDir());
|
||||
|
||||
// 新增字段写入JSON
|
||||
jsonWriter.name("isCommit").value(isCommit());
|
||||
jsonWriter.name("commitTitle").value(getCommitTitle());
|
||||
jsonWriter.name("commitInfo").value(getCommitInfo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
|
||||
if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
|
||||
if (super.initObjectsFromJsonReader(jsonReader, name)) {
|
||||
return true;
|
||||
} else {
|
||||
if (name.equals("buttonName")) {
|
||||
setButtonName(jsonReader.nextString());
|
||||
} else if (name.equals("exeCommand")) {
|
||||
setExeCommand(jsonReader.nextString());
|
||||
} else if (name.equals("workDir")) {
|
||||
setWorkDir(jsonReader.nextString());
|
||||
}
|
||||
// 新增字段解析读取
|
||||
else if (name.equals("isCommit")) {
|
||||
setCommit(jsonReader.nextBoolean());
|
||||
} else if (name.equals("commitTitle")) {
|
||||
setCommitTitle(jsonReader.nextString());
|
||||
} else if (name.equals("commitInfo")) {
|
||||
setCommitInfo(jsonReader.nextString());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@@ -85,8 +135,8 @@ public class TermuxButtonModel extends BaseBean {
|
||||
jsonReader.skipValue();
|
||||
}
|
||||
}
|
||||
// 结束 JSON 对象
|
||||
jsonReader.endObject();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package cc.winboll.studio.winboll.views;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.Button;
|
||||
import cc.winboll.studio.winboll.models.TermuxButtonModel;
|
||||
|
||||
/**
|
||||
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2026/04/30 10:57
|
||||
*/
|
||||
public class TermuxButton extends Button {
|
||||
|
||||
public static final String TAG = "TermuxButton";
|
||||
|
||||
// 绑定实体Model
|
||||
private TermuxButtonModel buttonModel;
|
||||
|
||||
// 原生基础构造
|
||||
public TermuxButton(Context context) {
|
||||
super(context);
|
||||
initView(null, null);
|
||||
}
|
||||
|
||||
// XML布局引用构造 解析属性
|
||||
public TermuxButton(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initView(attrs, null);
|
||||
}
|
||||
|
||||
public TermuxButton(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
initView(attrs, null);
|
||||
}
|
||||
|
||||
public TermuxButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
initView(attrs, null);
|
||||
}
|
||||
|
||||
// 代码动态创建 直接传入Model构造
|
||||
public TermuxButton(Context context, TermuxButtonModel model) {
|
||||
super(context);
|
||||
initView(null, model);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一初始化
|
||||
*/
|
||||
private void initView(AttributeSet attrs, TermuxButtonModel model) {
|
||||
this.buttonModel = model;
|
||||
|
||||
// 基础按钮默认配置
|
||||
setClickable(true);
|
||||
setFocusable(true);
|
||||
|
||||
// 解析XML自定义属性
|
||||
if (attrs != null) {
|
||||
parseXmlCustomAttr(attrs);
|
||||
}
|
||||
|
||||
// 用model的buttonName同步按钮文字
|
||||
refreshButtonText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析XML属性
|
||||
* 改用原生 android:text 给 buttonName 赋值
|
||||
*/
|
||||
private void parseXmlCustomAttr(AttributeSet attrs) {
|
||||
if (buttonModel == null) {
|
||||
buttonModel = new TermuxButtonModel();
|
||||
}
|
||||
|
||||
// 核心:读取原生 android:text 作为 buttonName
|
||||
String androidText = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "text");
|
||||
|
||||
// 读取其他自定义属性
|
||||
String cmd = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "exeCommand");
|
||||
String dir = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "workDir");
|
||||
String isCommitStr = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "isCommit");
|
||||
String cTitle = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "commitTitle");
|
||||
String cInfo = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "commitInfo");
|
||||
|
||||
// 把 android:text 赋值给 model 的 buttonName
|
||||
if (androidText != null) {
|
||||
buttonModel.setButtonName(androidText);
|
||||
}
|
||||
|
||||
// 其余属性正常赋值
|
||||
if (cmd != null) buttonModel.setExeCommand(cmd);
|
||||
if (dir != null) buttonModel.setWorkDir(dir);
|
||||
if (isCommitStr != null) buttonModel.setCommit(Boolean.parseBoolean(isCommitStr));
|
||||
if (cTitle != null) buttonModel.setCommitTitle(cTitle);
|
||||
if (cInfo != null) buttonModel.setCommitInfo(cInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一同步:buttonName 同步到按钮 android:text
|
||||
*/
|
||||
private void refreshButtonText() {
|
||||
if (buttonModel != null) {
|
||||
setText(buttonModel.getButtonName());
|
||||
}
|
||||
}
|
||||
|
||||
// Model Getter & Setter
|
||||
public TermuxButtonModel getButtonModel() {
|
||||
return buttonModel;
|
||||
}
|
||||
|
||||
public void setButtonModel(TermuxButtonModel buttonModel) {
|
||||
this.buttonModel = buttonModel;
|
||||
// 赋值model自动刷新按钮文字
|
||||
refreshButtonText();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,37 +27,61 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<HorizontalScrollView
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<HorizontalScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1.0">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<Button
|
||||
<cc.winboll.studio.winboll.views.TermuxButton
|
||||
android:id="@+id/btn_termux"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Termux"
|
||||
android:textSize="18sp"
|
||||
android:padding="16dp"
|
||||
android:backgroundTint="@android:color/holo_blue_dark"/>
|
||||
android:backgroundTint="@android:color/holo_blue_dark"
|
||||
app:exeCommand="cd ~"
|
||||
app:workDir="~"
|
||||
app:isCommit="true"
|
||||
app:commitTitle="打开 Termux"
|
||||
app:commitInfo="打开 Termux 应用"/>
|
||||
|
||||
<Button
|
||||
<cc.winboll.studio.winboll.views.TermuxButton
|
||||
android:id="@+id/btn_termuxworkspaces"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="TermuxWorkSpaces"
|
||||
android:textSize="18sp"
|
||||
android:padding="16dp"
|
||||
android:backgroundTint="@android:color/holo_blue_dark"/>
|
||||
android:backgroundTint="@android:color/holo_blue_dark"
|
||||
app:exeCommand="cd ~"
|
||||
app:workDir="~"
|
||||
app:isCommit="true"
|
||||
app:commitTitle="打开 TermuxWorkSpaces"
|
||||
app:commitInfo="打开 Termux 应用,进入 TermuxWorkSpaces 目录。"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</HorizontalScrollView>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="+"
|
||||
android:id="@+id/btn_addtermuxbutton"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
||||
@@ -5,5 +5,14 @@
|
||||
<attr name="toolbarTitleColor" format="color" />
|
||||
<attr name="toolbarBackgroundColor" format="color" />
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="TermuxButton">
|
||||
<attr name="buttonName" format="string" />
|
||||
<attr name="exeCommand" format="string" />
|
||||
<attr name="workDir" format="string" />
|
||||
<attr name="isCommit" format="boolean" />
|
||||
<attr name="commitTitle" format="string" />
|
||||
<attr name="commitInfo" format="string" />
|
||||
</declare-styleable>
|
||||
</resources>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user