Android enter key listener

2020-06-23 03:58发布

Can someone help me with a softkeyboard enter key listener?

I need a enter key listener like a button listener that would have a few editext listeners inside like this

enterkey.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) { 
        if(editext1.getText().toString().equalsIgnoreCase("test1")) {
            button3.performClick();
        }
        if(editext1.getText().toString().equalsIgnoreCase("test2")) {
            button4.performClick();
        }
    }
);

I also need something like below is this correct?

     if(editext1.getText().toString().equals.null)) {
            testwrong.setText("Wrong"); 

thanks to all that can help


I have now tried using this code but keep getting a null value when I hit enter? Can anyone suggest a solution to avoid this?

        editext.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (keyCode==KeyEvent.KEYCODE_ENTER) { 
                if("test1".equalsIgnoreCase(anstext.getText().toString())) {
                     but4.performClick();
                    }}
                else
                if("test2".equalsIgnoreCase(editext.getText().toString())) {
                 but5.performClick();
                }

            if("test5".equalsIgnoreCase(editext.getText().toString())) {
             but6.performClick();
            }

            if("test7".equalsIgnoreCase(editext.getText().toString())) {
             but7.performClick();
            }
            if (editext.getText().toString() != null){
              testwrong.seText("wrong");               }

        return true;




        } });

3条回答
冷血范
3楼-- · 2020-06-23 04:48

If you want to catch user press Enter register onKeyListener on your Edittext

  yourEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode==KeyEvent.KEYCODE_ENTER) { //Whenever you got user click enter. Get text in edittext and check it equal test1. If it's true do your code in listenerevent of button3
                    if("test1".equals(edt.getText().toString())) {
                        //paste your code in button3 listener here
                        }
                }

}
    )

This part is wrong.

if(editext1.getText().toString().equals.null)) { testwrong.setText("Wrong");

you should change to

if (editext1.getText().toString() != null && !editext1.getText().toString().isEmpty()) {
  // doSomething
}
查看更多
家丑人穷心不美
4楼-- · 2020-06-23 04:49

In your EditText you should specify keyboard action using imeOptions.

<EditText
       android:id="@+id/query"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:imeOptions="actionGo"
       android:inputType="text" />

And in your Activity's class:

 EditText editText = (EditText) findViewById(R.id.query);
 editText.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_GO) {

                        return true;
                    }
                    return false;
                }
            });
查看更多
登录 后发表回答