gpt4 book ai didi

java - KeyEvent.KEYCODE_ENTER如何引发

转载 作者:行者123 更新时间:2023-12-02 08:17:36 27 4
gpt4 key购买 nike

我的 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" />

现在,当我在手机中按 Enter 键时,它不会转到 Intent ...焦点始终与 edittext 密码保持一致...该怎么办?

最佳答案

在 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;

}
});

关于java - KeyEvent.KEYCODE_ENTER如何引发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6054918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com