Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to add tag after space input. #19

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand All @@ -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
Expand Down
80 changes: 56 additions & 24 deletions library/src/main/java/me/originqiu/library/EditTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -54,6 +53,8 @@ public class EditTag extends FrameLayout

private int deleteModeBgRes;

private boolean shouldAddTagAfterSpace;

private Drawable defaultTagBg;

private boolean isEditableStatus = true;
Expand Down Expand Up @@ -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();
}
Expand All @@ -128,6 +130,7 @@ private void addInputTagView() {
private void setupListener() {
editText.setOnEditorActionListener(this);
editText.setOnKeyListener(this);
editText.addTextChangedListener(this);
}

@Override
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -347,4 +375,8 @@ public void removeTag(String... tagValue) {
}
}
}

public void setShouldAddTagAfterSpace(boolean shouldAddTagAfterSpace) {
this.shouldAddTagAfterSpace = shouldAddTagAfterSpace;
}
}
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<attr name="tag_layout" format="reference"></attr>
<attr name="input_layout" format="reference"></attr>
<attr name="delete_mode_bg" format="reference"></attr>
<attr name="add_tag_after_space_input" format="boolean"></attr>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand Down