This commit is contained in:
Dacer
2014-10-18 22:55:03 +08:00
commit f3e35a2a01
4 changed files with 178 additions and 0 deletions

105
AutoCompleteEditText.java Normal file
View File

@@ -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<String> 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<String>(getContext(),
android.R.layout.simple_dropdown_item_1line, new ArrayList<String>());
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<String>(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";
}
}

20
LICENSE Normal file
View File

@@ -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.

53
README.md Normal file
View File

@@ -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
<im.dacer.AutoCompleteEditText
android:id="@+id/edit_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
```
```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

BIN
preview.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 601 KiB