Bir necha so'z miqdorini aniqrog'i uchun EditText-ni cheklash

EditText uzunligini so'zlarning ma'lum soniga (100 so'z) cheklash uchun qvaay qilib cheklashimiz mumkin.

Hatto ayrim matnlardan nusxa ko'chirish mumkin bo'lsa-da, uni joylashtirishda faqat 100 so'zni qabul qilish kerak. Buni belgilar soni bilan chegaralash mumkinligini bilaman ( maxLength )

Men kodni quyida sinab ko'rdim, lekin u 100 so'z bilan cheklanmaydi va katta matnni ko'chirishga ruxsat beradi.

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

                int wordsLength = countWords(charSequence.toString());// words.length;
               //count == 0 means a new word is going to start
                if (count == 0 && wordsLength >= MAX_WORD_LIMIT) {
                    int charLength = mDescription.getText().length();
                    setCharLimit(mDescription, charLength > 0 ? charLength - 2 : charLength);
                } else {
                    removeFilter(mDescription);
                }

va

private InputFilter filter;

    private void setCharLimit(EditText et, int max) {
        filter = new InputFilter.LengthFilter(max);
        et.setFilters(new InputFilter[]{filter});
    }

    private void removeFilter(EditText et) {
        if (filter != null) {
            et.setFilters(new InputFilter[0]);
            filter = null;
        }
    }
1
maxLength bilan qanday xatolik bor?
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
yep. javob berish ...........
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
OK .. savolim bor :)
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
tinglovchilarda o'zgarmaydigan raqamlardan foydalaning
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
@Redman: Buni takrorlash emas, men uni so'zlar bilan nazorat qilishim kerak
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
@KiranBennyJoseph: bu belgi sonini cheklaydi
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
@KiranBennyJoseph: Tashakkur :) har qanday fikr?
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
Nima uchun past ovoz berilganini bilmayman !!!
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
Buning o'rniga maxLength ni foydalaning.
qo'shib qo'ydi muallif M. Ashish, manba

6 javoblar

Buni quyidagicha amalga oshirishingiz mumkin:

android:maxLength="50"

EditText-da.

3
qo'shib qo'ydi

TextChanged o'rniga afterTextChanged (Editable s) kodini @Ram yozing.
qo'shib qo'ydi muallif Rahul Sharma, manba
Iltimos, savolni to'liq o'qing. Buni bilaman, bu belgi soni bilan chegaralanishi kerak, men uni so'zlar soni bilan nazorat qilishim kerak
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
    edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

            //To fix word count
            String yourText= editText.getText().toString().replace(String.valueOf((char) 160), " ");
            if (yourText.split("\\s+").length > MAX_WORD_LIMIT ) {

                int space = 0;
                int length = 0;
                for (int i = 0; i < yourText.length(); i++) {
                    if (yourText.charAt(i) == ' ') {
                        space++;
                        if (space >= MAX_WORD_LIMIT) {
                            length = i;
                            break;
                        }

                    }
                }
                if (length > 1) {
                    editText.getText().delete(length, yourSelf.length());//deleting last space
                    setCharLimit(editText, length - 1); //or limit edit text
                }
            } else {
                removeFilter(editText);
            }


        }

            @Override
            public void afterTextChanged(Editable s) {

        });
          private InputFilter filter;

          private void setCharLimit(EditText et, int max) {
            filter = new InputFilter.LengthFilter(max);
            et.setFilters(new InputFilter[] { filter });
            }

          private void removeFilter(EditText et) {
            if (filter != null) {
              et.setFilters(new InputFilter[0]);
             filter = null;
             }
           }

here 160 is the non breaking space

1
qo'shib qo'ydi
@KiranBennyJoseph Bu sodir bo'ladi. Ba'zan odamlar tasodifan qo'shimcha bo'shlik qo'yishadi. Bundan tashqari, bo'sh bo'lmasligi uchun avvalgi belgini tekshiring.
qo'shib qo'ydi muallif theblitz, manba
Agar ketma-ket ikkita blank mavjud bo'lsa, bu ishlamaydi.
qo'shib qo'ydi muallif theblitz, manba
:). OK, men buni bilaman
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
@Ram buni sinov qilmayman. Buning o'rniga Iam yozyapman
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
OK, men o'zgaraman
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
OK men qo'ydim teng ()
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
@theblitz nima uchun so'zlar orasida ikki blankani joylashtiramiz
qo'shib qo'ydi muallif Kiran Benny Joseph, manba
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
Bu shunchaki xabar berilgandir. Sizning vaqtingiz uchun minnatdorman, Agar siz setFocusable false ni tanlasangiz siz kiritgan matnni tahrirlash yoki o'chira olmaysiz
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
Buning o'rniga, charSequence.toString() funktsiyasini ishlatishingiz kerak. split ("\\ s +") uzunligi> 99
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
Agarda bunday noto'g'ri gap bo'lsa ...
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
Va @theblitz sharhiga qo'shilaman
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
@KiranBennyJoseph s.equals ('') tekshiruvi hech qachon haqiqiy emas, s har doim kiritgan barcha matnlarni o'z ichiga oladi.
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
Yo :) OK, tekshirish sizga xabar beradi.
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
CharSequence bilan to'g'ridan to'g'ri '==' solishtira olmaysiz ...
qo'shib qo'ydi muallif Ram Prakash Bhat, manba

Siz uchun uning ishini umid qilamiz

edittext.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) {
          //Check your edittext length here
           if (edittext.getText().toString().length() > 100)
              edittext.setText(edittext.getText().toString().substring(0, 100);
        }
    });
1
qo'shib qo'ydi
qo'shib qo'ydi muallif user2025187, manba
lengtht() funktsiyasi umumiy sonni bildiradi
qo'shib qo'ydi muallif Ram Prakash Bhat, manba
@Ram, men e'lon qildim, iltimos, uni tekshirib chiqing.
qo'shib qo'ydi muallif shahid17june, manba

Muammoingizni hal qilish uchun android: maxLength xususiyatini tahrir qiling.

1
qo'shib qo'ydi

Splitni funksiyalar indekslari bilan ishlatishingiz mumkin. Uzoq muddatli mag'lubiyat uchun uzr so'rayman, umid qilamanki. Faqat ba'zi parametrlarni chiqaring.

edittext.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 (edittext.getText().toString().split(" ").size() > 100)
                  edittext.setText(edittext.getText().toString().substring(0, edittext.getText().toString().indexOf(edittext.getText().toString().split(" ").get(100))));
            }
        });
0
qo'shib qo'ydi

Agar siz so'zlar sonini topmoqchi bo'lsangiz, Java mantiqidan foydalanishingiz mumkin.

String text = "More than 100 word sentence";
String[] txtArr = text.split(" ");

Endi dastlabki 100 ta indeksli so'zni tanlang.

Bu xaker. Ehtimol, u erda eng maqbul yo'llar bor.

0
qo'shib qo'ydi