1、给EditText加上文字选中功能,比如微博的插入话题功能。点击“插入话题”按钮的时候,“#请插入话题名称#”在两个#号中间的内容处于选中状态,用户一点击即消失。代码如下:
- Java代码
- text.setText("#请插入话题名称#");
- Editable editable = text.getText();
- Selection.setSelection(editable, 1, editable.length() - 1);
2、如果想默认进入一个Activity时,唯一的一个edittext先不要获得焦点。在EditText前面加上一个没有大小的Layout:
- XML/HTML代码
- <LinearLayout
- android:focusable="true" android:focusableInTouchMode="true"
- android:layout_width="0px" android:layout_height="0px"/>
3、输入文字的时候,如果想限制字数,并提示用户,可用以下方法:
- Java代码
- text.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- textCount = textCount + count - before;
- if (textCount <= 140) {
- writeWordDes.setText("可输入字数:" + (140 - textCount));
- writeWordDes.setTextColor(getResources().getColor(R.color.solid_black));
- } else {
- writeWordDes.setText("超出字数:" + (textCount - 140));
- writeWordDes.setTextColor(getResources().getColor(R.color.solid_red));
- }
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
- }
4、让EditText不可输入,比如超过一定字数后,不让用户再输入文字:
- Java代码
- text.setFilters(new InputFilter[] {
- new InputFilter() {
- public CharSequence filter(CharSequence source, int start,
- int end, Spanned dest, int dstart, int dend) {
- return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
- }
- }
- });