1、使用AutoComplete作为输入框,输入两个字及以上弹出提示。
2、当输入超出15个字,显示用户输入超出最大长度。
3、提示弹出邮箱。
界面代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:hint="@string/hint"
android:inputType="number"
android:id="@+id/userId"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/close"
android:visibility="gone"
android:src="@drawable/close"
android:layout_alignRight="@id/userId"
android:layout_alignTop="@id/userId"
android:layout_alignBottom="@id/userId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/msg"
android:visibility="invisible"
android:layout_below="@id/userId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!---imeOptions 改变键盘上回车键样式-->
<EditText
android:background="@drawable/input_area"
android:imeOptions="actionSend"
android:password="true"
android:layout_below="@id/msg"
android:id="@+id/pwd"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_below="@id/pwd"
android:layout_marginTop="10dp"
android:id="@+id/commit"
android:text="@string/commit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Activity代码
public class EditTextActivity extends Activity implements View.OnClickListener {
private AutoCompleteTextView mUserIdInput;
private View closeBtn;
private static final int USERID_MAX_LENGTH = 15;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edittext);
findViewById(R.id.commit).setOnClickListener(this);
findViewById(R.id.close).setOnClickListener(this);
closeBtn = findViewById(R.id.close);
mUserIdInput = (AutoCompleteTextView) findViewById(R.id.userId);
mUserIdInput.requestFocus();
// mUserIdInput.setSelection(3);
// mUserIdInput.setSelection(3, 5);
final TextView msg = (TextView) findViewById(R.id.msg);
mUserIdInput.setAdapter(new MyAdapter());
mUserIdInput.setThreshold(2);//设置输入几个字弹出提示
mUserIdInput.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 (s.length() > USERID_MAX_LENGTH) {
msg.setText(R.string.userIdError);
msg.setVisibility(View.VISIBLE);
} else {
msg.setVisibility(View.INVISIBLE);
}
closeBtn.setVisibility(s.length() > 0 ? View.VISIBLE : View.GONE);
}
});
//弹出输入框,打开输入法
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager inputManager = (InputMethodManager) mUserIdInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(mUserIdInput, 0);
}
}, 500);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.close:
mUserIdInput.setText(null);
break;
case R.id.commit:
break;
}
}
class MyAdapter extends BaseAdapter implements Filterable {
private String[] mArrays = getResources().getStringArray(R.array.userIdSuffix);
private ArrayList<String> mData = new ArrayList<>();
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) {
textView = new TextView(EditTextActivity.this);
} else {
textView = (TextView) convertView;
}
textView.setText(getItem(position).toString());
return textView;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
List<String> newData = new ArrayList<>();
if (constraint != null && !constraint.toString().contains("@")) {
for (String item : mArrays) {
newData.add(constraint + item);
}
}
result.values = newData;
result.count = newData.size();
return result;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mData = (ArrayList) results.values;
notifyDataSetChanged();
}
};
}
}
}
array.xml
<resources>
<string-array name="userIdSuffix">
<item>\@163.com</item>
<item>\@gmail.com</item>
<item>\@qq.com</item>
<item>\@sina.com</item>
</string-array>
</resources>