我正在开发一个Android应用程序,其中用户需要通过他的电话号码注册他的帐户。现在,当用户在EditText视图中输入他的号码时,他需要按下Submit按钮来创建他的帐户,这个Submit按钮位于EditText视图的正下方。
到目前为止,输入数字后,当用户按下手机键盘的“ Enter”键时,他将被带到EditText视图的下一行,但是,我希望当用户按下Enter按钮时,他的帐户应该是创建。因此,基本上,我希望Enter按钮的功能与Submit按钮的功能完全相同。
在这方面的任何帮助或指导将不胜感激。
我的提交按钮的代码:
final ImageButton bSubmit= (ImageButton) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
EditText eMobileNo = (EditText) findViewById(R.id.eMobileNo);
mobile = eMobileNo.getText().toString().replaceAll("[^\\d]", "");;
Log.i("MOBILE","MOBILE: "+mobile);
Toast.makeText(RegisterMe.this,"Your account has been successfully created",Toast.LENGTH_SHORT).show();
postData();
ConfirmToken();
finish();
}});
最佳答案
您需要在Edittext标记中修改一些代码xml
android:singleLine="true"
android:imeOptions="actionSend"
然后在您的活动中编写
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
// submit code here
EditText eMobileNo = (EditText) findViewById(R.id.eMobileNo);
mobile = eMobileNo.getText().toString().replaceAll("[^\\d]", "");;
Log.i("MOBILE","MOBILE: "+mobile);
Toast.makeText(RegisterMe.this,"Your account has been successfully created",Toast.LENGTH_SHORT).show();
postData();
ConfirmToken();
finish();
return true;
}
return false;
}
});