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

添加编辑模式下监听输入文字改变接口 #49

Open
wants to merge 1 commit into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import me.gujun.android.taggroup.TagGroup;
import me.gujun.android.taggroup.demo.db.TagsManager;
Expand All @@ -23,6 +24,12 @@ protected void onCreate(Bundle savedInstanceState) {

mTagGroup = (TagGroup) findViewById(R.id.tag_group);
mTagGroup.setTags(tags);
mTagGroup.setOnInputTextChangeListener(new TagGroup.OnInputTextChangeListener() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Toast.makeText(TagEditorActivity.this, s, Toast.LENGTH_SHORT).show();
}
});
}

@Override
Expand Down
209 changes: 165 additions & 44 deletions library/src/main/java/me/gujun/android/taggroup/TagGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
Expand Down Expand Up @@ -68,70 +69,119 @@ public class TagGroup extends ViewGroup {
private final float default_horizontal_padding;
private final float default_vertical_padding;

/** Indicates whether this TagGroup is set up to APPEND mode or DISPLAY mode. Default is false. */
/**
* Indicates whether this TagGroup is set up to APPEND mode or DISPLAY mode. Default is false.
*/
private boolean isAppendMode;

/** The text to be displayed when the text of the INPUT tag is empty. */
/**
* The text to be displayed when the text of the INPUT tag is empty.
*/
private CharSequence inputHint;

/** The tag outline border color. */
/**
* The tag outline border color.
*/
private int borderColor;

/** The tag text color. */
/**
* The tag text color.
*/
private int textColor;

/** The tag background color. */
/**
* The tag background color.
*/
private int backgroundColor;

/** The dash outline border color. */
/**
* The dash outline border color.
*/
private int dashBorderColor;

/** The input tag hint text color. */
/**
* The input tag hint text color.
*/
private int inputHintColor;

/** The input tag type text color. */
/**
* The input tag type text color.
*/
private int inputTextColor;

/** The checked tag outline border color. */
/**
* The checked tag outline border color.
*/
private int checkedBorderColor;

/** The check text color */
/**
* The check text color
*/
private int checkedTextColor;

/** The checked marker color. */
/**
* The checked marker color.
*/
private int checkedMarkerColor;

/** The checked tag background color. */
/**
* The checked tag background color.
*/
private int checkedBackgroundColor;

/** The tag background color, when the tag is being pressed. */
/**
* The tag background color, when the tag is being pressed.
*/
private int pressedBackgroundColor;

/** The tag outline border stroke width, default is 0.5dp. */
/**
* The tag outline border stroke width, default is 0.5dp.
*/
private float borderStrokeWidth;

/** The tag text size, default is 13sp. */
/**
* The tag text size, default is 13sp.
*/
private float textSize;

/** The horizontal tag spacing, default is 8.0dp. */
/**
* The horizontal tag spacing, default is 8.0dp.
*/
private int horizontalSpacing;

/** The vertical tag spacing, default is 4.0dp. */
/**
* The vertical tag spacing, default is 4.0dp.
*/
private int verticalSpacing;

/** The horizontal tag padding, default is 12.0dp. */
/**
* The horizontal tag padding, default is 12.0dp.
*/
private int horizontalPadding;

/** The vertical tag padding, default is 3.0dp. */
/**
* The vertical tag padding, default is 3.0dp.
*/
private int verticalPadding;

/** Listener used to dispatch tag change event. */
/**
* Listener used to dispatch tag change event.
*/
private OnInputTextChangeListener mOnInputTextChangeListener;

/**
* Listener used to dispatch tag change event.
*/
private OnTagChangeListener mOnTagChangeListener;

/** Listener used to dispatch tag click event. */
/**
* Listener used to dispatch tag click event.
*/
private OnTagClickListener mOnTagClickListener;

/** Listener used to handle tag click event. */
/**
* Listener used to handle tag click event.
*/
private InternalTagClickListener mInternalTagClickListener = new InternalTagClickListener();

public TagGroup(Context context) {
Expand Down Expand Up @@ -197,12 +247,16 @@ public void onClick(View v) {
public void submitTag() {
final TagView inputTag = getInputTag();
if (inputTag != null && inputTag.isInputAvailable()) {
inputTag.endInput();

if (mOnTagChangeListener != null) {
mOnTagChangeListener.onAppend(TagGroup.this, inputTag.getText().toString());
if (mOnTagChangeListener.onAppend(TagGroup.this, inputTag.getText().toString())) {
inputTag.endInput();
appendInputTag();
}
} else {
inputTag.endInput();
appendInputTag();
}
appendInputTag();

}
}

Expand Down Expand Up @@ -515,13 +569,24 @@ public void setOnTagClickListener(OnTagClickListener l) {
mOnTagClickListener = l;
}

/**
* Register a callback to be invoked when the input tex has been changed.
*
* @param l the callback that will run.
*/
public void setOnInputTextChangeListener(OnInputTextChangeListener l) {
this.mOnInputTextChangeListener = l;
}

protected void deleteTag(TagView tagView) {
removeView(tagView);
if (mOnTagChangeListener != null) {
mOnTagChangeListener.onDelete(TagGroup.this, tagView.getText().toString());
}
}

//CHANGE

/**
* Interface definition for a callback to be invoked when a tag group is changed.
*/
Expand All @@ -531,7 +596,7 @@ public interface OnTagChangeListener {
*
* @param tag the appended tag.
*/
void onAppend(TagGroup tagGroup, String tag);
boolean onAppend(TagGroup tagGroup, String tag);

/**
* Called when a tag has been deleted from the the group.
Expand All @@ -553,6 +618,21 @@ public interface OnTagClickListener {
void onTagClick(String tag);
}

/**
* Interface definition for a callback to be invoked when a tag is clicked.
*/
public interface OnInputTextChangeListener {
/**
* Called when the input text has been changed
* @param s
* @param start
* @param before
* @param count
*/
void onTextChanged(CharSequence s, int start, int before, int count);
}


/**
* Per-child layout information for layouts.
*/
Expand Down Expand Up @@ -652,19 +732,29 @@ class TagView extends TextView {
public static final int STATE_NORMAL = 1;
public static final int STATE_INPUT = 2;

/** The offset to the text. */
/**
* The offset to the text.
*/
private static final int CHECKED_MARKER_OFFSET = 3;

/** The stroke width of the checked marker */
/**
* The stroke width of the checked marker
*/
private static final int CHECKED_MARKER_STROKE_WIDTH = 4;

/** The current state. */
/**
* The current state.
*/
private int mState;

/** Indicates the tag if checked. */
/**
* Indicates the tag if checked.
*/
private boolean isChecked = false;

/** Indicates the tag if pressed. */
/**
* Indicates the tag if pressed.
*/
private boolean isPressed = false;

private Paint mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Expand All @@ -673,28 +763,44 @@ class TagView extends TextView {

private Paint mCheckedMarkerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

/** The rect for the tag's left corner drawing. */
/**
* The rect for the tag's left corner drawing.
*/
private RectF mLeftCornerRectF = new RectF();

/** The rect for the tag's right corner drawing. */
/**
* The rect for the tag's right corner drawing.
*/
private RectF mRightCornerRectF = new RectF();

/** The rect for the tag's horizontal blank fill area. */
/**
* The rect for the tag's horizontal blank fill area.
*/
private RectF mHorizontalBlankFillRectF = new RectF();

/** The rect for the tag's vertical blank fill area. */
/**
* The rect for the tag's vertical blank fill area.
*/
private RectF mVerticalBlankFillRectF = new RectF();

/** The rect for the checked mark draw bound. */
/**
* The rect for the checked mark draw bound.
*/
private RectF mCheckedMarkerBound = new RectF();

/** Used to detect the touch event. */
/**
* Used to detect the touch event.
*/
private Rect mOutRect = new Rect();

/** The path for draw the tag's outline border. */
/**
* The path for draw the tag's outline border.
*/
private Path mBorderPath = new Path();

/** The path effect provide draw the dash border. */
/**
* The path effect provide draw the dash border.
*/
private PathEffect mPathEffect = new DashPathEffect(new float[]{10, 5}, 0);

{
Expand All @@ -707,7 +813,7 @@ class TagView extends TextView {
}


public TagView(Context context, final int state, CharSequence text) {
public TagView(final Context context, final int state, CharSequence text) {
super(context);
setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
setLayoutParams(new TagGroup.LayoutParams(
Expand Down Expand Up @@ -747,11 +853,17 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (isInputAvailable()) {
// If the input content is available, end the input and dispatch
// the event, then append a new INPUT state tag.
endInput();

if (mOnTagChangeListener != null) {
mOnTagChangeListener.onAppend(TagGroup.this, getText().toString());
if (mOnTagChangeListener.onAppend(TagGroup.this, getText().toString())) {
endInput();
appendInputTag();
}
} else {
endInput();
appendInputTag();
}
appendInputTag();

}
return true;
}
Expand Down Expand Up @@ -801,10 +913,18 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mOnInputTextChangeListener!=null){
mOnInputTextChangeListener.onTextChanged(s, start, before, count);
}
}

@Override
public void afterTextChanged(Editable s) {
//workaround for keyboards that don't work well with the enter key event
if (s.toString().contains("\n")) {
setText(s.toString().replace("\n", ""));
submitTag();
}
}
});
}
Expand Down Expand Up @@ -1022,4 +1142,5 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) {
}
}
}

}