1

編集テキストに5行を入れたいので、ユーザーはEnter4回だけ押すことができます。このように試しましたが、それでも機能しません。

myEtidtext.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    // if enter is pressed start calculating
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
        // get EditText text
        String text = ((EditText) v).getText().toString();
        // find how many rows it cointains
        int editTextRowCount = text.split("\\n").length;
        // user has input more than limited - lets do something
        // about that
        if (editTextRowCount >= 5) {
            // find the last break
            int lastBreakIndex = text.lastIndexOf("\n");
            // compose new text
            String newText = text.substring(0, lastBreakIndex);
            // add new text - delete old one and append new one
            // (append because I want the cursor to be at the end)
            ((EditText) v).setText("");
            ((EditText) v).append(newText);
        }
    }
    return false;
}
});
4

2 に答える 2

0

編集テキストボックスの文字長制限を動的に設定するためのリンク-ソースコードも利用可能です: http ://www.mobisoftinfotech.com/blog/android/android-edittext-setfilters-example-numeric-text-field-patterns-および-length-restriction/#comment-174948 フィルターと長さの制限も利用可能

于 2013-02-03T13:08:08.150 に答える
0

その中の文字数EditTextを(たとえば300文字)に設定できます

et_description.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (s.toString().length() > 300) {
                    et_description.setText(s.toString().subSequence(0, 300));
                }
            }
        });
于 2013-02-03T14:20:40.097 に答える