Hello,

In my small application I have 2 EditTexts and a Button. If something
is entered in EditText1 and confirmed by "Enter", the focus should be
set to EditText-Field2. Therefore I use an OnKeyListener on the first
EditText. The method requestFocus is called in onKey on the second
EditText.

But there is problem. After something is entered in EditText1, the
focus changes to the Button.

Why does that happen?

Here is the layout and the actitivity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

package de.mih.myandroidtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;

public class MyAndroidTestActivity extends Activity {

    EditText _t1;
    EditText _t2;

    Button _b1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        _t1 = (EditText) findViewById(R.id.editText1);
        _t1.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_ENTER:
                    _t1.setEnabled(false);
                    _t2.requestFocus();
                }
                return false;
            }
        });

        _t2 = (EditText) findViewById(R.id.editText2);

        _b1 = (Button) findViewById(R.id.button1);
        _b1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                _t1.setEnabled(true);
                _t1.setText("");
                _t2.setEnabled(true);
                _t2.setText("");

                _t1.requestFocus();
            }
        });
    }
}

Thanks a lot

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to