Focus auf EditText setzen

G

Grossi

Gast
Hallo zusammen!

Ich habe ein Problem, von dem ich hoffe, dass ihr mir weiterhelfen könnt.

In einer Activity sind sechs Textfelder (EditText) definiert.
Sechzehn Buttons dienen dazu, etwas in diese Felder einzugeben (die virtuelle Tastatur habe ich abgeschaltet durch (myEditText.setInputType(InputType.TYPE_NULL)).

Nun möchte ich Android dazu bewegen zum nächsten EditText zu springen, wenn ein Butto gedrückt wurde.
Dazu habe ich eine Methode onButtonClick eingerichtet:

Code:
[COLOR=darkred]public[/COLOR] [COLOR=darkred]void[/COLOR] onButtonClick(View v) {
[COLOR=darkviolet]String[/COLOR] wert = [COLOR=red]""[/COLOR];
[COLOR=darkred]switch[/COLOR] (v.getId()) {
       [COLOR=darkred]case[/COLOR] R.id.reihe1_X: { wert = [COLOR=red]"X"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe1_10: { wert = [COLOR=red]"10"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe1_9: { wert = [COLOR=red]"9"[/COLOR]; [COLOR=darkred]break[/COLOR]; } 
       [COLOR=darkred]case[/COLOR] R.id.reihe1_8: { wert = [COLOR=red]"8"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe2s_7: { wert = [COLOR=red]"7"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe2n_7: { wert = [COLOR=red]"7"[/COLOR]; [COLOR=darkred]break[/COLOR]; } 
       [COLOR=darkred]case[/COLOR] R.id.reihe2s_6: { wert = [COLOR=red]"6"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe2s_M: { wert = [COLOR=red]"M"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe2n_6: { wert = [COLOR=red]"6"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe2n_5: { wert = [COLOR=red]"5"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe2n_4: { wert = [COLOR=red]"4"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe3_3: { wert = [COLOR=red]"3"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe3_2: { wert = [COLOR=red]"2"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe3_1: { wert = [COLOR=red]"1"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe3_M: { wert = [COLOR=red]"M"[/COLOR]; [COLOR=darkred]break[/COLOR]; }
       [COLOR=darkred]case[/COLOR] R.id.reihe4_Clear: { wert = [COLOR=red]""[/COLOR]; [COLOR=darkred]break[/COLOR]; }
}

[COLOR=darkred]if[/COLOR] (findViewById(R.id.wert1).hasFocus()) {
    ((EditText) findViewById(R.id.wert1)).setText(wert);
    ((EditText) findViewById(R.id.wert2)).requestFocus(); }

[COLOR=darkred]if[/COLOR] (findViewById(R.id.wert2).hasFocus()) {
   ((EditText) findViewById(R.id.wert2)).setText(wert);
   ((EditText) findViewById(R.id.wert3)).requestFocus(); }

[COLOR=darkred]if[/COLOR] (findViewById(R.id.wert3).hasFocus()) {
   ((EditText) findViewById(R.id.wert3)).setText(wert);
   ((EditText) findViewById(R.id.wert4)).requestFocus(); }

[COLOR=darkred]if[/COLOR] (findViewById(R.id.wert4).hasFocus()) { 
   ((EditText) findViewById(R.id.wert4)).setText(wert);
   ((EditText) findViewById(R.id.wert5)).requestFocus(); }

[COLOR=darkred]if[/COLOR] (findViewById(R.id.wert5).hasFocus()) {
   ((EditText) findViewById(R.id.wert5)).setText(wert);
   ((EditText) findViewById(R.id.wert6)).requestFocus(); }

[COLOR=darkred]if[/COLOR] (findViewById(R.id.wert6).hasFocus()) {
   ((EditText) findViewById(R.id.wert6)).setText(wert);
   ((EditText) findViewById(R.id.wert6)).requestFocus(); }
}
Das funktioniert aber so nicht. Wenn ich ein Wert in ein Feld (per Button) eingebe, erscheint dieser Wert zeitgleich in den übrigen 5 Felder und der Fokus wechselt nicht. Was mache ich falsch? Hat jemand eine Idee?

Vielen Dank im Voraus!
Grossi

P.S.: Wenn ich das requestFocus() weglasse, wird der Wert in das richtige Feld eingetragen.
Ich muss dann nur per Hand zwischen den Feldern wechseln.
 
Ich würde jetzt mal, ohne groß drüber bescheid zu wissen oder sich das genauer anzusehen, sagen ... du testest nur mit textfeld 1? --> 1. if ist true da focus .... geht durch und aktiviert das 2. --> 2. if ist true ... etc. eventuell einfach mal in jedes if ein return oder nicht if sondern else if nehmen.

MfG nijoX
 
  • Danke
Reaktionen: Grossi
nijoX schrieb:
Ich würde jetzt mal, ohne groß drüber bescheid zu wissen oder sich das genauer anzusehen, sagen ... du testest nur mit textfeld 1? --> 1. if ist true da focus .... geht durch und aktiviert das 2. --> 2. if ist true ... etc. eventuell einfach mal in jedes if ein return oder nicht if sondern else if nehmen.

MfG nijoX

Hallo nijoX,

genau das war's. Jetzt klappt's!

Vielen DanK!
 

Ähnliche Themen

A
Antworten
1
Aufrufe
637
swa00
swa00
M
  • MikelKatzengreis
Antworten
5
Aufrufe
128
swa00
swa00
B
Antworten
4
Aufrufe
491
bb321
B
Zurück
Oben Unten