diff --git a/demo/src/main/java/me/gujun/android/taggroup/demo/TagEditorActivity.java b/demo/src/main/java/me/gujun/android/taggroup/demo/TagEditorActivity.java index 27d2ea1..b841b6c 100644 --- a/demo/src/main/java/me/gujun/android/taggroup/demo/TagEditorActivity.java +++ b/demo/src/main/java/me/gujun/android/taggroup/demo/TagEditorActivity.java @@ -4,6 +4,8 @@ import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; import me.gujun.android.taggroup.TagGroup; import me.gujun.android.taggroup.demo.db.TagsManager; @@ -23,6 +25,20 @@ protected void onCreate(Bundle savedInstanceState) { mTagGroup = (TagGroup) findViewById(R.id.tag_group); mTagGroup.setTags(tags); + mTagGroup.setOnTagTextEntryListener(new TagGroup.OnTagTextEntryListener() { + @Override + public void onTextEntry(AutoCompleteTextView tagView, String text) { + if (text.equals("A")) { + tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Au", "Bu", "Cy"})); + } else if (text.equals("Au")) { + tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Aus", "Bul", "Cyp"})); + } else if (text.equals("Aus")) { + tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Aust", "Bulg", "Cypr"})); + } else if (text.equals("Aust")) { + tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Australia", "Bulgaria", "Cyprus"})); + } + } + }); } @Override diff --git a/library/build.gradle b/library/build.gradle index 87bfd43..8206344 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -13,6 +13,6 @@ android { } } // Used to push in maven -apply from: '../maven-push.gradle' +//apply from: '../maven-push.gradle' dependencies { } \ No newline at end of file diff --git a/library/src/main/java/me/gujun/android/taggroup/TagGroup.java b/library/src/main/java/me/gujun/android/taggroup/TagGroup.java index 5f2ae99..b155192 100644 --- a/library/src/main/java/me/gujun/android/taggroup/TagGroup.java +++ b/library/src/main/java/me/gujun/android/taggroup/TagGroup.java @@ -13,6 +13,7 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.Editable; +import android.text.InputType; import android.text.TextUtils; import android.text.TextWatcher; import android.text.method.ArrowKeyMovementMethod; @@ -26,6 +27,7 @@ import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; +import android.widget.AutoCompleteTextView; import android.widget.TextView; import java.util.ArrayList; @@ -134,6 +136,9 @@ public class TagGroup extends ViewGroup { /** Listener used to handle tag click event. */ private InternalTagClickListener mInternalTagClickListener = new InternalTagClickListener(); + /** Listener used to handle tag text entry event. */ + public OnTagTextEntryListener onTagTextEntryListener; + public TagGroup(Context context) { this(context, null); } @@ -457,6 +462,15 @@ public void setOnTagChangeListener(OnTagChangeListener l) { mOnTagChangeListener = l; } + /** + * Register a callback to be invoked when a tag text is entered. + * + * @param l the callback that will be used to inform text entry event + */ + public void setOnTagTextEntryListener(OnTagTextEntryListener l) { + this.onTagTextEntryListener = l; + } + /** * @see #appendInputTag(String) */ @@ -553,6 +567,10 @@ public interface OnTagClickListener { void onTagClick(String tag); } + public interface OnTagTextEntryListener { + void onTextEntry(AutoCompleteTextView tagView, String text); + } + /** * Per-child layout information for layouts. */ @@ -648,7 +666,7 @@ public void onClick(View v) { /** * The tag view which has two states can be either NORMAL or INPUT. */ - class TagView extends TextView { + class TagView extends AutoCompleteTextView { public static final int STATE_NORMAL = 1; public static final int STATE_INPUT = 2; @@ -717,6 +735,8 @@ public TagView(Context context, final int state, CharSequence text) { setGravity(Gravity.CENTER); setText(text); setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); + setRawInputType(InputType.TYPE_CLASS_TEXT); + setImeOptions(EditorInfo.IME_ACTION_GO); mState = state; @@ -737,6 +757,21 @@ public boolean onLongClick(View v) { if (state == STATE_INPUT) { requestFocus(); + addTextChangedListener(new TextWatcher() { + @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 (onTagTextEntryListener != null) onTagTextEntryListener.onTextEntry(TagView.this, s.toString()); + } + }); + // Handle the ENTER key down. setOnEditorActionListener(new OnEditorActionListener() { @Override