郵箱聯想
佈局
<LinearLayout
android:orientation="vertical"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="match_parent"
android:layout_height="66dp" />
<test.com.mp3.MailBoxAssociateView
android:id="@+id/imageviewedittextone"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:hint="地址"
android:paddingLeft="5dp"
android:popupBackground="@android:color/darker_gray"
android:singleLine="true"
android:textColor="@android:color/black"
android:textColorHint="@android:color/holo_blue_bright"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/autoComplete" />
</LinearLayout>
自定義 MailBoxAssociateView
package test.com.mp3;
import android.content.Context;
import android.util.AttributeSet;
/**
* Created by T_baby on 17/12/25.
* @後進行提示
*/
public class MailBoxAssociateView extends android.support.v7.widget.AppCompatMultiAutoCompleteTextView {
public MailBoxAssociateView(Context context) {
super(context);
}
public MailBoxAssociateView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MailBoxAssociateView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean enoughToFilter() {
// 如果字元中包含'@'並且不在第一位,則滿足條件
return getText().toString().contains("@") && getText().toString().indexOf("@") > 0;
}
}
類 MailBoxAssociate tokenizer
package test.com.mp3;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView;
/**
* Created by T_baby on 17/12/25.
*/
public class MailBoxAssociateTokenizer implements MultiAutoCompleteTextView.Tokenizer {
@Override
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == '@') {
return i;
} else {
i++;
}
}
return len;
}
@Override
public int findTokenStart(CharSequence text, int cursor) {
int index = text.toString().indexOf("@");
if (index < 0) {
index = text.length();
}
if (index >= findTokenEnd(text, cursor)) {
index = 0;
}
return index;
}
@Override
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
if (i > 0 && text.charAt(i - 1) == '@') {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text);
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
return sp;
} else {
return text;
}
}
}
}
類 Autocompale
package test.com.mp3;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by T_baby on 17/12/25.
* 自動提示
*/
public class AutoComplete extends AutoCompleteTextView {
//輸入字元進行聯想,@163.com、@126.com、@qq.com、@sina.com、@yeah.net、@yahoo.com、@gmail.com等常用郵箱
private static final String[] emailSuffix = {"@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com",
"@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net", "@vip.qq.com", "@vip.sina.com"};
public AutoComplete(Context context) {
super(context);
init(context);
}
public AutoComplete(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
final MyAdatper adapter = new MyAdatper(context);
setAdapter(adapter);
addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
adapter.mList.clear();
if (input.length() > 0) {
for (int i = 0; i < emailSuffix.length; ++i) {
adapter.mList.add(input + emailSuffix[i]);
}
}
adapter.notifyDataSetChanged();
showDropDown();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// default=2 當輸入一個字元的時候就開始檢測
setThreshold(1);
}
class MyAdatper extends BaseAdapter implements Filterable {
List<String> mList;
private Context mContext;
private MyFilter mFilter;
public MyAdatper(Context context) {
mContext = context;
mList = new ArrayList<String>();
}
@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}
@Override
public Object getItem(int position) {
return mList == null ? null : mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
TextView tv = new TextView(mContext);
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
convertView = tv;
}
TextView txt = (TextView) convertView;
txt.setText(mList.get(position));
return txt;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new MyFilter();
}
return mFilter;
}
private class MyFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (mList == null) {
mList = new ArrayList<String>();
}
results.values = mList;
results.count = mList.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
}
MainActivity
MailBoxAssociateView view;
private String[] email_sufixs = new String[]{"@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com",
"@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net", "@vip.qq.com", "@vip.sina.com"};
在onCreateview中
view = findViewById(R.id.imageviewedittextone); MailBoxAssociateTokenizer mailBoxAssociateTokenizer = new MailBoxAssociateTokenizer(); ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, email_sufixs); view.setAdapter(adapter); view.setTokenizer(mailBoxAssociateTokenizer);
相關文章
- 電子郵箱是qq郵箱嗎 電子郵箱和qq郵箱的區別聯絡介紹
- 自動註冊gmail郵箱構想AI
- win10自帶郵件怎麼新增qq郵箱_win10郵箱如何匯入qq郵箱Win10
- win10 email配置qq郵箱如何操作_win10郵箱怎麼新增qq郵箱Win10AI
- gitlab 郵箱配置Gitlab
- 外貿郵箱為什麼申請企業郵箱?
- golang傳送郵件(qq郵箱)Golang
- win10系統下郵箱怎麼新增qq郵箱Win10
- win10自帶郵箱無法登入QQ郵箱Win10
- 如何申請企業郵箱?企業郵箱選購指南
- 工作郵箱怎麼註冊?企業郵箱有哪些功能?
- win10郵件怎麼用qq郵箱 win10郵件如何使用qq郵箱Win10
- qq郵箱怎麼發檔案給別的郵箱 qq郵箱如何將文件傳送給別人
- 企業郵箱如何註冊登陸,企業郵箱怎麼收費?企業郵箱哪個更好?
- 郵箱格式驗證
- js驗證郵箱JS
- Mac電子郵箱Mac
- google smtp 郵箱配置Go
- 關於郵箱爆破
- 外貿郵箱註冊:谷歌企業郵箱註冊詳細教程谷歌
- 怎麼登陸163郵箱?163的郵箱有哪些實用技巧?
- 收費郵箱有哪些?收費郵箱有哪些真正的價值?
- HTML input email 郵箱域HTMLAI
- HTML input email郵箱域HTMLAI
- Laravel 郵箱服務使用Laravel
- JavaScript郵箱格式驗證JavaScript
- Laravel 配置郵箱引數Laravel
- 郵箱傳送模組
- 郵箱偽造詳解
- centos配置郵箱服務CentOS
- P9150 郵箱題
- Outlook 2021 for Mac outlook郵箱Mac
- python 如何偽裝郵箱Python
- spring boot配置QQ郵箱傳送郵件Spring Boot
- 谷歌郵箱,配置傳送郵件密碼谷歌密碼
- 你到底有幾個郵箱?碼雲賬號增加多郵箱支援!
- 個人郵箱與企業郵箱的區別有哪些?如何選擇?
- 把企業郵箱換成Zoho Mail企業郵箱之前的疑問AI