如何将InputFilter用于Android中EditText中的最大字符数?

时间:2013-01-23 07:58:58

标签: android android-edittext

<RelativeLayout
    android:id="@+id/tab1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dip" >

    <EditText
        android:id="@+id/txtMarketName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true"
        android:textColor="@android:color/black"
        android:textSize="17sp" />

    <ImageView
        android:id="@+id/imgDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip"
        android:focusableInTouchMode="true"
        android:scaleType="fitXY"
        android:src="@drawable/edit_clr_tab" />
</RelativeLayout>

xml看起来像:

enter image description here

Java代码:

public class EditTextLocker {
private EditText editText;

private int charactersLimit;

public EditTextLocker(EditText editText) {

    this.editText = editText;

    editText.setOnKeyListener(editTextOnKeyListener);
}

private OnKeyListener editTextOnKeyListener = new OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_DEL) {
            startStopEditing(false);
        }

        return false;
    }
};

private TextWatcher editTextWatcherForCharacterLimits = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {            


        if (!editText.getText().toString().equalsIgnoreCase("")) {

            int editTextLength = editText.getText().toString().trim()
                    .length();

            if (editTextLength >= charactersLimit) {

                startStopEditing(true);

            }

        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

public void limitCharacters(final int limit) {

    this.charactersLimit = limit;
    editText.addTextChangedListener(editTextWatcherForCharacterLimits);
}

public void unlockEditText() {

    startStopEditing(false);
}

public void startStopEditing(boolean isLock) {

    System.out.println("Start Stop Edit ==============> : " + isLock);

    if (isLock) {

        editText.setFilters(new InputFilter[] { new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start,
                    int end, Spanned dest, int dstart, int dend) {

                return len < 1 ? dest.subSequence(dstart, dend)
                        : "";
            }
        } });

    } else {

        editText.setFilters(new InputFilter[] { new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start,
                    int end, Spanned dest, int dstart, int dend) {
                return null;
            }
        } });
    }
}
}

主要活动:

public class MainActivity extends Activity {
EditText editText1;
ImageView imageView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText1 = (EditText) findViewById(R.id.txtMarketName);

    imageView1 = (ImageView) findViewById(R.id.imgDelete);

    final EditTextLocker locker1 = new EditTextLocker(editText1);
    locker1.limitCharacters(60);

     editText1.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if (!hasFocus) {
                imageView1.setVisibility(View.INVISIBLE);
            } else {
                imageView1.setVisibility(View.VISIBLE);
            }
        }
    });

    imageView1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            editText1.setText("");
        }
    });
}
}

问题是停止出现在60个字符后,但它仍然继续输入,除了文本没有出现,但是,它确实出现在“缓冲区”(意思是建议事物)

另一个问题是,在调用imageview1 onClick事件后,键入是继续但文本不会显示在编辑文本中

1 个答案:

答案 0 :(得分:0)

以下代码片段将帮助您处理inPut Filer 对我而言,它就像一个魅力

  InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter});