requestFocus Probleme

  • Antworten:0
Oliver Schwarz
  • Forum-Beiträge: 1

29.02.2012, 09:52:09 via Website

Hallo,

ich habe eine kleine App geschrieben, in der im oberen Bereich 2 EditTextfelder angezeigt werden und unten ein "OK-Button". Wenn man in das 1. EditText-Feld etwas eingibt und per Enter bestätigt, soll das 1. Feld deaktiviert werden und der Focus zum 2. EditText wechseln.

Ich habe dazu einen OnKeyListener am EditText 1 registriert. Per requestFocus soll das 2. Feld den Focus bekommen. Allerdings springt der Focus immer zum Button.

Das verstehe ich nicht. Warum ist das so?

1package de.mih.myandroidtest;
2
3import android.app.Activity;
4import android.os.Bundle;
5import android.view.KeyEvent;
6import android.view.View;
7import android.view.View.OnClickListener;
8import android.view.View.OnKeyListener;
9import android.widget.Button;
10import android.widget.EditText;
11
12public class MyAndroidTestActivity extends Activity {
13
14 EditText _t1;
15 EditText _t2;
16
17 Button _b1;
18
19 /** Called when the activity is first created. */
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.main);
24
25 _t1 = (EditText) findViewById(R.id.editText1);
26 _t1.setOnKeyListener(new OnKeyListener() {
27
28 public boolean onKey(View v, int keyCode, KeyEvent event) {
29 switch (keyCode) {
30 case KeyEvent.KEYCODE_ENTER:
31 _t1.setEnabled(false);
32 _t2.requestFocus();
33 }
34 return false;
35 }
36 });
37
38 _t2 = (EditText) findViewById(R.id.editText2);
39
40 _b1 = (Button) findViewById(R.id.button1);
41 _b1.setOnClickListener(new OnClickListener() {
42
43 public void onClick(View v) {
44 _t1.setEnabled(true);
45 _t1.setText("");
46 _t2.setEnabled(true);
47 _t2.setText("");
48
49 _t1.requestFocus();
50 }
51 });
52 }
53}

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <LinearLayout
8 android:id="@+id/linearLayout1"
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11 android:layout_weight="1"
12 android:orientation="vertical" >
13
14 <EditText
15 android:id="@+id/editText1"
16 android:layout_width="match_parent"
17 android:layout_height="wrap_content"
18 android:inputType="number" >
19
20 <requestFocus />
21 </EditText>
22
23 <EditText
24 android:id="@+id/editText2"
25 android:layout_width="match_parent"
26 android:layout_height="wrap_content"
27 android:inputType="number" />
28 </LinearLayout>
29
30 <Button
31 android:id="@+id/button1"
32 android:layout_width="fill_parent"
33 android:layout_height="wrap_content"
34 android:text="Button" />
35
36</LinearLayout>

Antworten