commit f3e35a2a0153d4412517a9bffd867d593383b680 Author: Dacer Date: Sat Oct 18 22:55:03 2014 +0800 init diff --git a/AutoCompleteEditText.java b/AutoCompleteEditText.java new file mode 100644 index 0000000..e1a9525 --- /dev/null +++ b/AutoCompleteEditText.java @@ -0,0 +1,105 @@ +package im.dacer; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.WindowManager; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; + +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + +/** + * Created by Dacer on 5/23/14. + * Show a drop-down list after input a symbol. + */ +public class AutoCompleteEditText extends AutoCompleteTextView { + + private ArrayAdapter adapter; + private String startAtSymbol = "#"; + + public AutoCompleteEditText(Context context){ + this(context, null); + } + public AutoCompleteEditText(Context context, AttributeSet attrs){ + super(context,attrs); + init(); + } + public AutoCompleteEditText(Context context, AttributeSet attrs, int defStyle){ + super(context, attrs, defStyle); + init(); + } + + private void init(){ + adapter = new ArrayAdapter(getContext(), + android.R.layout.simple_dropdown_item_1line, new ArrayList()); + setAdapter(adapter); + setDropDownWidth(WindowManager.LayoutParams.WRAP_CONTENT); + } + + /** + * + * @param symbol default is # + */ + public void setStartAtSymbol(String symbol){ + startAtSymbol = symbol; + } + + /** + * + * @param dataList The data must start with the symbol + */ + public void setAutoCompleteList(String[] dataList){ + adapter = new ArrayAdapter(getContext(), + android.R.layout.simple_dropdown_item_1line, dataList); + setAdapter(adapter); + } + + @Override + public boolean enoughToFilter() { + if(getText() != null){ + return getText().length() != 0; + } + return true; + } + + @Override + protected void performFiltering(CharSequence text, int keyCode){ + String beforeCursor = getText().toString().substring(0, getSelectionStart()); + Pattern pattern = Pattern.compile(getRegularExpression()); + Matcher matcher = pattern.matcher(beforeCursor); + if (matcher.find()) { + text = matcher.group(0);; + } + super.performFiltering(text, keyCode); + } + + @Override + protected void replaceText(CharSequence text){ + String beforeCursor = getText().toString().substring(0, getSelectionStart()); + String afterCursor = getText().toString().substring(getSelectionStart()); + + Pattern pattern = Pattern.compile("#\\S*"); + Matcher matcher = pattern.matcher(beforeCursor); + StringBuffer sb = new StringBuffer(); + int matcherStart = 0; + while (matcher.find()) { + int curPos = getSelectionStart(); + if(curPos > matcher.start() && + curPos <= matcher.end()){ + matcherStart = matcher.start(); + matcher.appendReplacement(sb, text.toString()+" "); + } + } + matcher.appendTail(sb); + setText(sb.toString()+afterCursor); + setSelection(matcherStart + text.length()+1); + } + + + private String getRegularExpression(){ + return startAtSymbol+"\\S*\\z"; + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4820773 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2014 Ding Wenhao + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3eee516 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# AutoCompleteEditText + +A simple EditText that can show a drop-down list after input '#' symbol. + +![AutoCompleteEditText](https://raw.github.com/dacer/AndroidCharts/master/preview.gif) +It was used in [Pomotodo](https://play.google.com/store/apps/details?id=com.pomotodo) + +## Usage + +```xml + +``` + +```java +AutoCompleteEditText editText = (AutoCompleteEditText) findViewById(R.id.edit_text); +editText.setStartAtSymbol("#"); +editText.setAutoCompleteList(list); +``` + +## License + +The MIT License (MIT) + +Copyright (c) 2014 Ding Wenhao + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +## Contributing + +Please fork this repository and contribute back using +[pull requests](https://github.com/github/android/pulls). + +Any contributions, large or small, major features, bug fixes, additional +language translations, unit/integration tests are welcomed \ No newline at end of file diff --git a/preview.gif b/preview.gif new file mode 100644 index 0000000..5494e08 Binary files /dev/null and b/preview.gif differ