我试图猜测游戏的单词类型。我创建了一个编辑文本字段供用户输入单词。用户按下虚拟键盘上的返回键后,如何获取数据。以前我曾尝试通过创建按钮来获取数据,一旦用户单击按钮,就会检索数据。但这似乎是解决这个问题的迂回方式。
答案 0 :(得分:0)
你可以这样做
public class AndroidEnterKeyActivity extends Activity implements
OnKeyListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setOnKeyListener(this);
EditText editText2 = (EditText) findViewById(R.id.editText2);
editText2.setOnKeyListener(this);
}
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
TextView responseText = (TextView) findViewById(R.id.responseText);
EditText myEditText = (EditText) view;
if (keyCode == EditorInfo.IME_ACTION_SEARCH ||
keyCode == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (!event.isShiftPressed()) {
Log.v("AndroidEnterKeyActivity","Enter Key Pressed!");
switch (view.getId()) {
case R.id.editText1:
responseText
.setText("Just pressed the ENTER key, " +
"focus was on Text Box1. " +
"You typed:\n" + myEditText.getText());
break;
case R.id.editText2:
responseText
.setText("Just pressed the ENTER key, " +
"focus was on Text Box2. " +
"You typed:\n" + myEditText.getText());
break;
}
return true;
}
}
return false; // pass on to other listeners.
}
}
答案 1 :(得分:0)
将setOnEditorActionListener()
用于上一个编辑文字
布局xml代码
<EditText
android:id="@+id/signinscr_userName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/userName"
android:imeOptions="actionNext" />
<EditText
android:id="@+id/signinscr_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:imeOptions="actionDone"
android:inputType="textPassword" />
.java
文件的代码
EditText userNameField = (EditText)
findViewById(R.id.signinscr_userName);
EditText passwordField = (EditText)
findViewById(R.id.signinscr_password);
passwordField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent
arg2) {
//Do your operation here.
return false;
}
});