按Enter键应该与某个特定按钮完全相同

时间:2015-08-06 05:33:36

标签: android onclicklistener

我正在开发一个Android应用程序,用户需要通过他的电话号码注册他的帐户。现在,当用户在EditText视图中输入了他的号码时,他需要按“提交”按钮创建他的帐户,此提交按钮位于EditText视图的正下方。

截至目前,在输入号码后,当用户按下手机键盘的“Enter”键时,他将被带到EditText View的下一行,但是,当用户按下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();
}});

2 个答案:

答案 0 :(得分:1)

您需要在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;
            }
        });

答案 1 :(得分:0)

尝试获取Enter按钮事件&将您的注册过程放入其中:

 @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
        Register();
    }
}

修改:

editText.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                Log.i(TAG,"Enter pressed");
            }    
            return false;
        }
    });