From 8e8e7916fcdb61c724deb04d1d6f27848bd940be Mon Sep 17 00:00:00 2001 From: TFStudy <3202410880@qq.com> Date: Wed, 21 Apr 2021 10:54:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme=E5=8F=8Acopyright?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- .../java/com/goodiebag/pinview/Constant.java | 18 ++- .../java/com/goodiebag/pinview/MyValue.java | 44 ++++++ .../java/com/goodiebag/pinview/Pinview.java | 141 ++++++++++++------ .../com/goodiebag/pinview/TypedAttrUtils.java | 6 +- 5 files changed, 157 insertions(+), 56 deletions(-) create mode 100644 pinview/src/main/java/com/goodiebag/pinview/MyValue.java diff --git a/README.md b/README.md index 927df72..e302a2d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # Pinview -**本项目是基于开源项目Pinview进行OpenHarmony的移植和开发的,可以通过项目标签以及github地址()追踪到原安卓项目版本** +**本项目是基于开源项目Pinview进行openharmony的移植和开发的,可以通过项目标签以及github地址()追踪到原安卓项目版本** #### 项目介绍 - 项目名称:TextField工具类 -- 所属系列:OpenHarmony的第三方组件适配移植 +- 所属系列:openharmony的第三方组件适配移植 - 功能:实现连续的文本输入框、密码框 - 项目移植状态:TextField光标设置图片背景未实现 - 调用差异:无 diff --git a/pinview/src/main/java/com/goodiebag/pinview/Constant.java b/pinview/src/main/java/com/goodiebag/pinview/Constant.java index d2a8694..bca8679 100644 --- a/pinview/src/main/java/com/goodiebag/pinview/Constant.java +++ b/pinview/src/main/java/com/goodiebag/pinview/Constant.java @@ -1,10 +1,23 @@ - /* - * 常量类 + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.goodiebag.pinview; +/* + * 常量类 + */ public class Constant { /** * -1 @@ -186,5 +199,4 @@ public class Constant { * 151 */ public static final int COLOR_151 = 151; - } diff --git a/pinview/src/main/java/com/goodiebag/pinview/MyValue.java b/pinview/src/main/java/com/goodiebag/pinview/MyValue.java new file mode 100644 index 0000000..2a522b3 --- /dev/null +++ b/pinview/src/main/java/com/goodiebag/pinview/MyValue.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.goodiebag.pinview; + +import ohos.agp.components.TextField; + +import java.util.ArrayList; +import java.util.List; + +public class MyValue { + /** + * 所有子视图 + */ + public static List pinviews = new ArrayList<>(); + /** + * 所有输入框 + */ + public static List fields = new ArrayList<>(); + /** + * 位置 + */ + public static int size = 0; + /** + * 行数 + */ + public static int page = 0; + /** + * 存储临时输入框 + */ + public static TextField temp = null; +} diff --git a/pinview/src/main/java/com/goodiebag/pinview/Pinview.java b/pinview/src/main/java/com/goodiebag/pinview/Pinview.java index 98299c6..fd6dd5b 100644 --- a/pinview/src/main/java/com/goodiebag/pinview/Pinview.java +++ b/pinview/src/main/java/com/goodiebag/pinview/Pinview.java @@ -29,8 +29,7 @@ import ohos.agp.window.dialog.ToastDialog; import ohos.app.Context; import ohos.multimodalinput.event.KeyEvent; -import java.util.ArrayList; -import java.util.List; +import java.util.*; /** * This class implements a pinview for android. @@ -54,9 +53,6 @@ public class Pinview extends DirectionalLayout implements */ private int mPinLength = 4; private List editTextList = new ArrayList<>(); - private List pinViews = new ArrayList<>(); - private List fields = new ArrayList<>(); - private List textFields = new ArrayList<>(); private int mPinWidth = 100; private int mTextSize = 50; private int mPinHeight = 100; @@ -110,6 +106,47 @@ public class Pinview extends DirectionalLayout implements @Override public void onClick(Component component) { + if (MyValue.temp != null && MyValue.temp != component) { + if (MyValue.page == 4) { + selectFlag(MyValue.temp, 3); + } else { + selectFlag(MyValue.temp, 2); + } + MyValue.temp.clearFocus(); + } + MyValue.pinviews.clear(); + MyValue.fields.clear(); + for (int index = 0; index < ((DirectionalLayout) getComponentParent()).getChildCount(); index++) { + if (((DirectionalLayout) getComponentParent()).getComponentAt(index) instanceof Pinview) { + MyValue.pinviews.add((Pinview) ((DirectionalLayout) getComponentParent()).getComponentAt(index)); + } + } + + for (int pinIndex = 0; pinIndex < MyValue.pinviews.size(); pinIndex++) { + Pinview pinView = MyValue.pinviews.get(pinIndex); + for (int index = 0; index < pinView.getChildCount(); index++) { + MyValue.fields.add((TextField) pinView.getComponentAt(index)); + } + } + if (MyValue.fields.size() > 0) { + for (TextField field : MyValue.fields) { + MyValue.size++; + if (field == component) { + break; + } + } + } + if (MyValue.size >= 0 && MyValue.size <= mPinLength - 1) { + MyValue.page = 0; + } else if (MyValue.size > mPinLength - 1 && MyValue.size <= mPinLength * 2 - 1) { + MyValue.page = 1; + } else if (MyValue.size > mPinLength * 2 - 1 && MyValue.size <= mPinLength * 3 - 1) { + MyValue.page = 2; + } else if (MyValue.size > mPinLength * 2 - 1 && MyValue.size <= mPinLength * 4 - 1) { + MyValue.page = 3; + } + MyValue.size = 0; + new ToastDialog(mContext).setText(MyValue.page + "").show(); for (int i = 0; i < Integer.parseInt(component.getTag().toString()); i++) { TextField textField = editTextList.get(i); String text = textField.getText(); @@ -129,6 +166,7 @@ public class Pinview extends DirectionalLayout implements for (int i = 0; i < mPinLength; i++) { TextField textField = editTextList.get(i); if (textField.length() == 0) { + number++; textField.setFocusable(FOCUS_ENABLE); textField.setEnabled(true); textField.requestFocus(); @@ -141,6 +179,16 @@ public class Pinview extends DirectionalLayout implements return; } } + if (number == 0) { + TextField textField = editTextList.get(mPinLength - 1); + if (mFlag != 3) { + setPinBackgroundRes(textField, 1); + } else { + setPinBackgroundRes(textField, mFlag); + } + } else { + number = 0; + } } } if (count != 0) { @@ -474,58 +522,57 @@ public class Pinview extends DirectionalLayout implements } return false; } else if (keyEvent.isKeyDown() && keyEvent.getKeyCode() == KeyEvent.KEY_ENTER) { - pinViews.clear(); - fields.clear(); - textFields.clear(); - for (int index = 0; index < ((DirectionalLayout) getComponentParent()).getChildCount(); index++) { - if (((DirectionalLayout) getComponentParent()).getComponentAt(index) instanceof Pinview) { - pinViews.add((Pinview) ((DirectionalLayout) getComponentParent()).getComponentAt(index)); - } + if (MyValue.page == 0) { + enter(1); + MyValue.page = 1; + } else if (MyValue.page == 1) { + enter(2); + MyValue.page = 2; + } else if (MyValue.page == 2) { + enter(3); + MyValue.page = 3; + } else if (MyValue.page == 3) { + enter(4); + MyValue.page = 4; } + } + return false; + } - for (int pinIndex = 0; pinIndex < pinViews.size(); pinIndex++) { - Pinview pinView = pinViews.get(pinIndex); - for (int index = 0; index < pinView.getChildCount(); index++) { - textFields.add((TextField) pinView.getComponentAt(index)); - if (index == 0) { - TextField field = (TextField) pinView.getComponentAt(index); - if (field.length() > 0) { - number = pinIndex + 1; - new ToastDialog(mContext).setText("" + number).show(); - } - fields.add(field); - } - } - } - TextField field = fields.get(number); - int indexOf = textFields.indexOf(field); - if (field.length() > 0) { - for (int index = indexOf; index < textFields.size(); index++) { - if (index + 1 < textFields.size() && textFields.get(index + 1).length() <= 0) { - if (index + 1 >= textFields.size() - 1 - mPinLength) { - setPinBackgroundRes(textFields.get(index + 1), 3); - } else { - setPinBackgroundRes(textFields.get(index + 1), 1); - } - textFields.get(index + 1).setFocusable(FOCUS_ENABLE); - textFields.get(index + 1).setEnabled(true); - textFields.get(index + 1).requestFocus(); - return true; + private void enter(int page) { + Pinview pinview = MyValue.pinviews.get(page); + int childCount = pinview.getChildCount(); + for (int index = 0; index < childCount; index++) { + TextField field = (TextField) pinview.getComponentAt(index); + if (field.length() == 0) { + int focus = getIndexOfCurrentFocus(); + TextField textField = editTextList.get(focus); + if (focus == 0) { + if (page == 4) { + selectFlag(textField, 3); + } else { + selectFlag(textField, 2); } - } - } else { - if (number == fields.size() - 1) { - setPinBackgroundRes(field, 3); } else { - setPinBackgroundRes(field, 1); + if (page == 4) { + selectFlag(textField, 3); + } else { + selectFlag(textField, 0); + } } + MyValue.temp = field; field.setFocusable(FOCUS_ENABLE); field.setEnabled(true); field.requestFocus(); - number = 0; + textField.clearFocus(); + if (page == 4) { + selectFlag(field, 3); + } else { + selectFlag(field, 1); + } + break; } } - return false; } @Override diff --git a/pinview/src/main/java/com/goodiebag/pinview/TypedAttrUtils.java b/pinview/src/main/java/com/goodiebag/pinview/TypedAttrUtils.java index 956cef7..d8cb1db 100644 --- a/pinview/src/main/java/com/goodiebag/pinview/TypedAttrUtils.java +++ b/pinview/src/main/java/com/goodiebag/pinview/TypedAttrUtils.java @@ -1,18 +1,16 @@ /* - * Copyright (C) 2016 Jared Rummler - * + * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.goodiebag.pinview; -- Gitee