diff --git a/doric/src/main/java/pub/doric/DoricRegistry.java b/doric/src/main/java/pub/doric/DoricRegistry.java index 75f087e5..6172e9dd 100644 --- a/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric/src/main/java/pub/doric/DoricRegistry.java @@ -30,6 +30,7 @@ import pub.doric.plugin.StoragePlugin; import pub.doric.refresh.RefreshableNode; import pub.doric.shader.HLayoutNode; import pub.doric.shader.ImageNode; +import pub.doric.shader.InputNode; import pub.doric.shader.ScrollerNode; import pub.doric.shader.flowlayout.FlowLayoutItemNode; import pub.doric.shader.flowlayout.FlowLayoutNode; @@ -106,6 +107,7 @@ public class DoricRegistry { this.registerViewNode(RefreshableNode.class); this.registerViewNode(FlowLayoutNode.class); this.registerViewNode(FlowLayoutItemNode.class); + this.registerViewNode(InputNode.class); initRegistry(this); } diff --git a/doric/src/main/java/pub/doric/shader/InputNode.java b/doric/src/main/java/pub/doric/shader/InputNode.java new file mode 100644 index 00000000..6c924b44 --- /dev/null +++ b/doric/src/main/java/pub/doric/shader/InputNode.java @@ -0,0 +1,153 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * 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 pub.doric.shader; + +import android.text.Editable; +import android.text.InputType; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.View; +import android.widget.EditText; + +import com.github.pengfeizhou.jscore.JSObject; +import com.github.pengfeizhou.jscore.JSValue; + +import pub.doric.DoricContext; +import pub.doric.extension.bridge.DoricMethod; +import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.extension.bridge.DoricPromise; + +/** + * @Description: pub.doric.shader + * @Author: pengfei.zhou + * @CreateDate: 2019-12-06 + */ +@DoricPlugin(name = "Input") +public class InputNode extends ViewNode implements TextWatcher, View.OnFocusChangeListener { + private String onTextChangeId; + private String onFocusChangeId; + + public InputNode(DoricContext doricContext) { + super(doricContext); + } + + @Override + protected EditText build() { + EditText editText = new EditText(getContext()); + editText.addTextChangedListener(this); + editText.setOnFocusChangeListener(this); + return editText; + } + + @Override + protected void blend(EditText view, String name, JSValue prop) { + switch (name) { + case "text": + view.setText(prop.asString().toString()); + break; + case "textSize": + view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, prop.asNumber().toFloat()); + break; + case "textColor": + view.setTextColor(prop.asNumber().toInt()); + break; + case "textAlignment": + view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL); + break; + case "hintText": + view.setHint(prop.asString().toString()); + break; + case "hintTextColor": + view.setHintTextColor(prop.asNumber().toInt()); + break; + case "multiline": + if (prop.asBoolean().value()) { + view.setInputType(view.getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE); + } else { + view.setInputType(view.getInputType() & ~InputType.TYPE_TEXT_FLAG_MULTI_LINE); + } + break; + case "onTextChange": + if (prop.isString()) { + onTextChangeId = prop.asString().value(); + } else { + onTextChangeId = null; + } + break; + case "onFocusChange": + if (prop.isString()) { + onFocusChangeId = prop.asString().value(); + } else { + onFocusChangeId = null; + } + break; + default: + super.blend(view, name, prop); + break; + } + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + if (!TextUtils.isEmpty(onTextChangeId)) { + callJSResponse(onTextChangeId, s.toString()); + } + } + + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (!TextUtils.isEmpty(onFocusChangeId)) { + callJSResponse(onFocusChangeId, hasFocus); + } + } + + @DoricMethod + public String getText() { + return mView.getText().toString(); + } + + @DoricMethod + public void setSelection(JSObject jsObject, DoricPromise doricPromise) { + int start = jsObject.getProperty("start").asNumber().toInt(); + int end = jsObject.getProperty("end").asNumber().toInt(); + mView.setSelection(start, end); + doricPromise.resolve(); + } + + @DoricMethod + public void requestFocus(DoricPromise promise) { + mView.requestFocus(); + promise.resolve(); + } + + @DoricMethod + public void releaseFocus(DoricPromise promise) { + mView.clearFocus(); + promise.resolve(); + } +} diff --git a/doric/src/main/java/pub/doric/shader/TextNode.java b/doric/src/main/java/pub/doric/shader/TextNode.java index 78f95906..87c5faea 100644 --- a/doric/src/main/java/pub/doric/shader/TextNode.java +++ b/doric/src/main/java/pub/doric/shader/TextNode.java @@ -57,7 +57,8 @@ public class TextNode extends ViewNode { case "textAlignment": view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL); break; - case "numberOfLines": + case "maxLines": + view.setMaxLines(prop.asNumber().toInt()); break; default: super.blend(view, name, prop);