Theory
'Gan' લખો, બાકીનું જુઓ
FestConnect નું છેલ્લું ખાનું છે 'College'. એ સેંકડો છે, અને students પાસે આખું નામ લખાવવાથી typos નોતરાય છે. વધુ સારું: એ 'Gan' લખે એટલે dropdown 'Ganpat University' સૂચવે, એના પર દબાવો, પતી ગયું. અને એ કોઈ પણ ખાનું લખતા હોય ત્યારે તમને જીવતી અક્ષરોની ગણતરી કે તરતનું validation જોઈતું હોઈ શકે.
બે ટુકડા આ કરે છે: AutoCompleteTextView (સૂચનોના dropdown વાળું લખાણનું ખાનું) અને TextWatcher (દરેક કળ પર ચાલતો code). બંને સાથે મળીને લખાણનું input હોશિયાર બનાવે છે.
Theory
તમારું મન વાંચતું શોધનું ખાનું
એવી શોધની પટ્ટી વિચારો જે બે અક્ષર પછી સંભવિત પૂર્તિઓની ટૂંકી યાદી ઉતારે છે, અને તમે ભાગ્યે જ પૂરું લખો છો. એ છે AutoCompleteTextView: એ તમે અત્યાર સુધી લખેલા સામે યાદી ગાળે છે અને મળતાં સૂચવે છે. અને કલ્પો કે તમારા ખભા પરથી જોતો કોઈ મદદનીશ તમે લખો તેમ અક્ષરો ગણે છે, એ છે TextWatcher, જે દરેક કળ પર તરત પ્રતિભાવ આપે છે.
Theory
સૂચનો અને કળનાં events
AutoCompleteTextView:
- સૂચનો ArrayAdapter થી ભરો (Spinner ની જેમ).
- `setThreshold(n)`: n અક્ષર લખાય પછી સૂચનો દેખાડો (મૂળભૂત 2).
- વાપરનાર હજી મુક્ત લખાણ લખી શકે છે; સૂચનો ફક્ત મદદ કરે છે.
TextWatcher (કોઈ પણ EditText સાથે addTextChangedListener થી જોડો), ત્રણ callbacks:
beforeTextChanged(...),onTextChanged(...),afterTextChanged(...).
એમને જીવતી ગણતરી, validation, કે વાપરનાર લખે તેમ બટન ચાલુ કરવા વાપરો.
Practical
College નું autocomplete વત્તા જીવતી ગણતરી
// AutoCompleteTextView: suggest colleges after 1 char
AutoCompleteTextView college = findViewById(R.id.college);
String[] list = {"Ganpat University", "MSU Baroda", "GTU"};
college.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, list));
college.setThreshold(1); // suggest after 1 character
// TextWatcher on the name field: live character count
EditText name = findViewById(R.id.name);
TextView counter = findViewById(R.id.counter);
name.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s,int a,int b,int c){}
public void onTextChanged(CharSequence s,int a,int b,int c){
counter.setText(s.length() + " chars"); // runs each keystroke
}
public void afterTextChanged(Editable s){}
});Think first
Autocomplete સામે Spinner
AutoCompleteTextView અને Spinner બંને વાપરનારને યાદીમાંથી college પસંદ કરવા દે છે. લાંબી યાદી માટે autocomplete પસંદ કરવાનું એક કારણ આપો, અને એવી એક વસ્તુ કહો જે autocomplete ચાલવા દે છે અને Spinner નહીં.
Show the answer
લાંબી યાદી (સેંકડો colleges) માટે autocomplete વધુ ઝડપી છે: વાપરનાર થોડા અક્ષર લખીને તરત ગાળે છે, વિરાટ Spinner ના dropdown માં scroll કરવાને બદલે. અને autocomplete મુક્ત લખાણ ચાલવા દે છે: જેની college યાદીમાં ન હોય એ student એને લખી શકે છે, જ્યારે Spinner એને પહેલેથી નક્કી કરેલા વિકલ્પો પૂરતો મર્યાદિત રાખે છે. એટલે autocomplete = લખો તેમ ગાળવું વત્તા નવું દાખલ કરવાની છૂટ; Spinner = ચુસ્તપણે નિશ્ચિત સમૂહમાંથી પસંદગી.
Quiz
વાપરનાર નામના ખાનામાં લખે એ દરેક વખતે તમને અક્ષરોની ગણતરી સુધારવી છે. કઈ પ્રયુક્તિ તમારો code દરેક કળ પર ચલાવે છે?
- addTextChangedListener થી જોડાયેલું TextWatcher
- Spinner નું setOnItemSelectedListener
- onCreate માં દેખાડાયેલું Toast
- બટનનું onClick નું handler
Show the answer
addTextChangedListener થી જોડાયેલું TextWatcher
TextWatcher (addTextChangedListener થી ઉમેરાયેલું) લખાણના દરેક ફેરફાર પર પોતાનાં callbacks ચલાવે છે, એટલે વાપરનાર લખે તેમ એ જીવતી ગણતરી સુધારી શકે છે (A). Spinner નું item પસંદ થવાનું listener dropdown ની પસંદગીને પ્રતિભાવ આપે છે, લખવાને નહીં (B). onCreate માંનું Toast શરૂઆતમાં એક જ વાર ચાલે છે (C), અને onClick ફક્ત બટન દબાય ત્યારે ચાલે છે, દરેક કળ પર નહીં (D). લખવા પરના જીવતા પ્રતિભાવ માટે TextWatcher એ ઓજાર છે.
Watch out
Autocomplete અને TextWatcher ના ફાંદા
1. AutoCompleteTextView પર adapter નહીં: કોઈ સૂચનો દેખાતાં નથી. Spinner ની જેમ એને ArrayAdapter જોઈએ.
2. Threshold ની ગૂંચવણ: setThreshold(1) એક અક્ષર પછી સૂચનો દેખાડે છે; મૂળભૂત 2 છે. જો સૂચનો 'દેખાતાં નથી', તો કદાચ threshold લખાયેલા અક્ષરો કરતાં વધારે છે.
3. onTextChanged માં ભારે કામ: એ દરેક કળ પર ચાલે છે, એટલે ત્યાં ધીમી ક્રિયાઓ ટાળો; callback ને હળવું રાખો.
Formula
FestConnect Mobile પૂરું થયું
બાંધકામ પર નજર નાખો: Unit 1 એ Android સમજાવ્યું અને ઓજારો ગોઠવ્યાં, Unit 2 એ app બનાવી (Activity, layout, manifest), Unit 3 એ એ files જેમાં લખાય છે એ XML શીખવ્યું, અને Units 4 થી 5 એ નોંધણીનો પડદો widget પર widget ભેગો કર્યો, એટલે કે Toast સાથે EditText અને Button, ToggleButton, CheckBoxes, RadioButtons, AlertDialog સાથે Spinner, અને TextWatcher સાથે AutoCompleteTextView. પરીક્ષામાં, દરેક widget નો સાચો ઉપયોગ કહી શકવો (એક સામે અનેક, ક્ષણભંગુર સામે અટકાવતું, dropdown સામે autocomplete) એ કોઈ પણ એક પદ્ધતિના નામ કરતાં વધુ કિંમતી છે.
Summary
Key takeaways
- AutoCompleteTextView એ લખાણનું ખાનું છે જે તમે લખો તેમ adapter માંથી મળતાં સૂચવે છે; setThreshold(n) નક્કી કરે છે કે કેટલા અક્ષર પછી.
- એ યાદી ગાળે છે પણ મુક્ત લખાણ પણ ચાલવા દે છે, જે લાંબી યાદીઓ માટે સારું છે (ત્યાં Spinner કરતાં વધુ સારું).
- addTextChangedListener થી જોડાયેલું TextWatcher દરેક કળ પર code ચલાવે છે.
- એનાં ત્રણ callbacks છે beforeTextChanged, onTextChanged, afterTextChanged; એમને હળવાં રાખો.
- જીવતી ગણતરી, validation, કે વાપરનાર લખે તેમ બટન ચાલુ કરવા TextWatcher વાપરો.
- Memory hook: તમારું મન વાંચતું શોધનું ખાનું, અને દરેક કળ ગણતો મદદનીશ.