2
<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 看起来像:

在此处输入图像描述

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 事件后,类型是继续,但文本不显示在编辑文本中

4

1 回答 1

0

以下代码片段将帮助您为我处理输入文件管理器,它的工作原理就像一个魅力

  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}); 
于 2013-01-23T08:03:31.477 回答