diff --git a/README.md b/README.md
index 476a60d..9b48387 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,7 @@
android:id="@+id/edit_tag_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
+ app:add_tag_after_space_input="true"
app:tag_layout="@layout/view_sample_tag"
app:delete_mode_bg="#FF4081"
app:input_layout="@layout/view_sample_input_tag"/>
@@ -35,6 +36,7 @@
* Get tag list: ```getTagList()```
* Set input tag enable ```setEditable(true)```
* Add a tag ```addTag(String tagContent)```
+* Enable add tag after entering space ```setShouldAddTagAfterSpace(Boolean shouldAddTagAfterSpace)```
* ⚠ When you custom input_layout layout, please use ```MEditText``` replaced ```EditText```, in order to avoid some of the soft keyboard can not receive the delete action event
### Todo
diff --git a/library/src/main/java/me/originqiu/library/EditTag.java b/library/src/main/java/me/originqiu/library/EditTag.java
index aafb087..5a326f9 100644
--- a/library/src/main/java/me/originqiu/library/EditTag.java
+++ b/library/src/main/java/me/originqiu/library/EditTag.java
@@ -20,10 +20,10 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
-import android.support.v7.widget.AppCompatTextView;
+import android.text.Editable;
import android.text.TextUtils;
+import android.text.TextWatcher;
import android.util.AttributeSet;
-import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@@ -35,14 +35,13 @@
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.List;
/**
* Created by OriginQiu on 4/7/16.
*/
public class EditTag extends FrameLayout
- implements View.OnClickListener, TextView.OnEditorActionListener, View.OnKeyListener {
+ implements View.OnClickListener, TextView.OnEditorActionListener, View.OnKeyListener, TextWatcher {
private FlowLayout flowLayout;
@@ -54,6 +53,8 @@ public class EditTag extends FrameLayout
private int deleteModeBgRes;
+ private boolean shouldAddTagAfterSpace;
+
private Drawable defaultTagBg;
private boolean isEditableStatus = true;
@@ -103,6 +104,7 @@ public EditTag(Context context, AttributeSet attrs, int defStyleAttr) {
R.layout.view_default_input_tag);
deleteModeBgRes =
mTypedArray.getResourceId(R.styleable.EditTag_delete_mode_bg, R.color.colorAccent);
+ shouldAddTagAfterSpace = mTypedArray.getBoolean(R.styleable.EditTag_add_tag_after_space_input, false);
mTypedArray.recycle();
setupView();
}
@@ -128,6 +130,7 @@ private void addInputTagView() {
private void setupListener() {
editText.setOnEditorActionListener(this);
editText.setOnKeyListener(this);
+ editText.addTextChangedListener(this);
}
@Override
@@ -166,26 +169,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean isHandle = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
- String tagContent = editText.getText().toString();
- if (TextUtils.isEmpty(tagContent)) {
- // do nothing, or you can tip "can'nt add empty tag"
- } else {
- if (tagAddCallBack == null || (tagAddCallBack != null
- && tagAddCallBack.onTagAdd(tagContent))) {
- TextView tagTextView = createTag(flowLayout, tagContent);
- if (defaultTagBg == null) {
- defaultTagBg = tagTextView.getBackground();
- }
- tagTextView.setOnClickListener(EditTag.this);
- flowLayout.addView(tagTextView, flowLayout.getChildCount() - 1);
- tagValueList.add(tagContent);
- // reset action status
- editText.getText().clear();
- editText.performClick();
- isDelAction = false;
- isHandle = true;
- }
- }
+ isHandle = handleTextToTag();
}
return isHandle;
}
@@ -216,6 +200,50 @@ public void onClick(View view) {
}
}
+ @Override
+ public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
+ // Do nothing
+ }
+
+ @Override
+ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
+ // Do nothing
+ }
+
+ @Override
+ public void afterTextChanged(Editable editable) {
+ String inputText = editable.toString();
+ if (shouldAddTagAfterSpace && inputText.contains(" ") && inputText.trim().length() > 0) {
+ handleTextToTag();
+ }
+ }
+
+ private boolean handleTextToTag() {
+ String tagContent = editText.getText().toString();
+ if (TextUtils.isEmpty(tagContent)) {
+ // do nothing, or you can tip "can'nt add empty tag"
+ return false;
+ } else {
+ tagContent = tagContent.trim();
+ if (tagAddCallBack == null || (tagAddCallBack != null
+ && tagAddCallBack.onTagAdd(tagContent))) {
+ TextView tagTextView = createTag(flowLayout, tagContent);
+ if (defaultTagBg == null) {
+ defaultTagBg = tagTextView.getBackground();
+ }
+ tagTextView.setOnClickListener(EditTag.this);
+ flowLayout.addView(tagTextView, flowLayout.getChildCount() - 1);
+ tagValueList.add(tagContent);
+ // reset action status
+ editText.getText().clear();
+ editText.performClick();
+ isDelAction = false;
+ return true;
+ }
+ }
+ return false;
+ }
+
private void removeSelectedTag() {
int size = tagValueList.size();
if (size > 0 && lastSelectTagView != null) {
@@ -347,4 +375,8 @@ public void removeTag(String... tagValue) {
}
}
}
+
+ public void setShouldAddTagAfterSpace(boolean shouldAddTagAfterSpace) {
+ this.shouldAddTagAfterSpace = shouldAddTagAfterSpace;
+ }
}
diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml
index 8e5c4fd..1a01d74 100644
--- a/library/src/main/res/values/attrs.xml
+++ b/library/src/main/res/values/attrs.xml
@@ -4,5 +4,6 @@
+
\ No newline at end of file
diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml
index 534eba5..cd5ca57 100644
--- a/sample/src/main/res/layout/activity_main.xml
+++ b/sample/src/main/res/layout/activity_main.xml
@@ -20,6 +20,7 @@
android:id="@+id/edit_tag_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
+ app:add_tag_after_space_input="true"
app:input_layout="@layout/view_sample_input_tag"
app:tag_layout="@layout/view_sample_tag" />