KeyEvent.KEYCODE_ENTER如何引发

时间:2011-05-19 06:56:38

标签: java android xml

我的java类文件看起来像

public class SignIn extends Activity {

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

/***Enter key event in phone********************************/
public boolean onKey(View v, int keyCode, KeyEvent event) {
    switch(keyCode) {
    case KeyEvent.KEYCODE_ENTER:
     Intent intent = new Intent(SignIn.this, List_Of_Songs.class);
     startActivity(intent);
            break;

    default:
            return false;
    }

    return true;
}

}

和xml

<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/signinemailtxt"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Email" android:textSize="20dp" android:layout_marginTop="65dp"
    android:layout_marginLeft="25dp" android:paddingRight="50dp" android:textColor="#000000"/>

<EditText android:id="@+id/signinemailid" android:hint="email@domain.com"
    android:layout_height="28dp" android:layout_width="170dp"
    android:layout_toRightOf="@+id/signinemailtxt"

     android:layout_marginTop="65dp"
    android:background="#ffffff" android:layout_below="@+id/signinlayout" />

<TextView android:id="@+id/signinpasswordtxt"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Password" android:textSize="20dp"
    android:layout_marginTop="20dp" android:layout_marginLeft="25dp"
    android:layout_below="@+id/signinemailtxt" android:paddingRight="15dp" android:textColor="#000000" />

<EditText android:id="@+id/signinpassword" android:hint="password"
    android:layout_height="28dp" android:layout_width="170dp"
    android:layout_toRightOf="@+id/signinpasswordtxt"
     android:layout_marginTop="20dp"
    android:background="#ffffff" android:layout_below="@+id/signinemailid" />

现在,当我按下电话中的enterkey时,它不会转向意图......焦点会随着edittext passsword而自动...该怎么办?

3 个答案:

答案 0 :(得分:2)

在java代码中添加以下行:

1)创建编辑文本的对象:

EditText editText = (EditText) findViewById(R.id.signinpassword);

2)向其添加一个onKeyListener:

editText.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                switch(keyCode) {
    case KeyEvent.KEYCODE_ENTER:
     Intent intent = new Intent(SignIn.this, List_Of_Songs.class);
     startActivity(intent);
            break;

    default:
            return false;
    }

    return true;

            }
        });

答案 1 :(得分:0)

发生这种情况是因为当usr命中物理键时会调用onkey方法你应该覆盖ime选项中存在的键,即软键盘

答案 2 :(得分:0)

一旦我从我的xml文件中删除android:imeOptions="actionDone",这对我有用。