2

Would someone kindly suggest why the code in the OnKey() method is not executing. Please note that I am attempting to trigger this my hitting the "Search" button on the android key board.

Here is the main code:

package com.onkeyexample1; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; public class OnKeyExample1Activity extends Activity { EditText editText; OnKeyListener onKeyListener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.editText1); editText.setOnKeyListener(onKeyListener); editText.addTextChangedListener(inputTextWatcher); onKeyListener = new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { System.out.println("Clicked"); return true; } }; } 

I tried adding the block of code below as a suggested fix but it didn't help:

 private TextWatcher inputTextWatcher = new TextWatcher() { public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { // Log.d(TAG, s.charAt(count-1) + " character to send");; } }; } 

Here is my layout xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:imeOptions="actionSearch" android:layout_width="fill_parent" android:id="@+id/editText1" android:layout_height="wrap_content"> <requestFocus></requestFocus> </EditText> </LinearLayout> 

Any suggestions are greatly appreciated!

Here is the first implementation of Ted's suggestion:

public class OnKeyExample1Activity extends Activity { EditText editText; OnKeyListener onKeyListener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.editText1); editText.setOnKeyListener(onKeyListener = new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { System.out.println("Clicked"); return true; } }); } } 

Here's the fix to the double trigger problem mentioned in my comment:

editText.setOnKeyListener(onKeyListener = new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() != KeyEvent.ACTION_DOWN) { System.out.println("Argh"); return false; } return true; } }); } 
0

1 Answer 1

2

You need to assign a value to onKeyListener before you call editText.setOnKeyListener(onKeyListener);. Just move the assignment up ahead of the method call and it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Ted, thank you for your suggestion it worked only now it seems that my code in onKey() is being executed twice. Can think of any reason for this? Again much thanks for your help.
Ted, fixed the double trigger problem (See my code above). Thanks again for you help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.