Problems with onKeyDown / onKeyListener (?)

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problems with onKeyDown / onKeyListener (?)

Postby timweicker » Thu Jun 11, 2009 2:15 pm

Hey there,


I have a simple problem:

I programmed an activity in which the user can press a button and send a text from an EditText box. But every time the user presses "Enter" while being in the EditText box, a new row appears in the EditText box. But I'd like to have that if the user presses "Enter" my program reacts as if he presses the button. How is that possible?

Thank you for your time!


Best regards,
Tim
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Top

Postby WarrenFaith » Thu Jun 11, 2009 3:28 pm

Ever tested this?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public boolean onKeyDown(int keyCode, KeyEvent ke) {
  4.  
  5.     if (keyCode == KeyEvent.KEYCODE_ENTER) {
  6.  
  7.         // do what you want...
  8.  
  9.     }
  10.  
  11.     return true;
  12.  
  13. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby timweicker » Thu Jun 11, 2009 5:20 pm

I'm very desperated right now, maybe someone can help me - This source code doesn't work right now, do you have any idea why?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Search extends Activity {
  3.  
  4.  
  5.  
  6.         static EditText city;
  7.  
  8.         static Cities[] result;
  9.  
  10.  
  11.  
  12.         public boolean onKeyDown(int keyCode, KeyEvent ke) {
  13.  
  14.             if (keyCode == KeyEvent.KEYCODE_ENTER) {
  15.  
  16.                 AlertDialog.Builder alert = new AlertDialog.Builder(this);
  17.  
  18.                 alert.setMessage("Error");
  19.  
  20.                 alert.setCancelable(true);
  21.  
  22.                 alert.create().show();
  23.  
  24.             }
  25.  
  26.             return true;
  27.  
  28.         }
  29.  
  30.        
  31.  
  32.         @Override
  33.  
  34.         public void onCreate(Bundle savedInstanceState) {
  35.  
  36.                 super.onCreate(savedInstanceState);
  37.  
  38.                 setContentView(R.layout.city);
  39.  
  40.  
  41.  
  42.                 city = (EditText) findViewById(R.id.city);
  43.  
  44.                 Button send = (Button) findViewById(R.id.send);
  45.  
  46.                
  47.  
  48.                 send.setOnClickListener(new View.OnClickListener() {
  49.  
  50.  
  51.  
  52.                         public void onClick(View v) {
  53.  
  54.  
  55.  
  56.                                 CitySearchFunction Suche = new CitySearchFunction();
  57.  
  58.                                 result = Acitivity.method((String) Search.city.getText()
  59.  
  60.                                                 .toString());
  61.  
  62.                                         try {
  63.  
  64.                                                 Intent intent = new Intent();
  65.  
  66.                                                 intent.setClass(Search.this, listen.class);
  67.  
  68.                                                 startActivity(intent);
  69.  
  70.                                                 finish();
  71.  
  72.                                         } catch (Exception e) {
  73.  
  74.                                                 System.out.println(e.getMessage());
  75.  
  76.                                         }
  77.  
  78.                         }
  79.  
  80.                 });
  81.  
  82.         }
  83.  
  84.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Postby WarrenFaith » Thu Jun 11, 2009 5:33 pm

This source code doesn't work right now

Sorry but what does this mean?

Is the source not compiling? Is the source code angry with you? :D

So what exactly does not work? What should this code do in your opinion?
Have you any exception in your LogCat?

We need more information to help you.
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby timweicker » Thu Jun 11, 2009 6:51 pm

It runs, but not in the way that I want it to :D

Everytime I press "Enter" when I finished typing into the EditText box, the onKeyDown-method doesn't react and the emulator starts a new row within the EditText box. So basically this new method doesn't do any difference and I don't understand why...

Tim
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Postby timweicker » Thu Jun 11, 2009 6:58 pm

It runs, but not in the way that I want it to :D

Everytime I press "Enter" when I finished typing into the EditText box, the onKeyDown-method doesn't react and the emulator starts a new row within the EditText box. So basically this new method doesn't do any difference and I don't understand why...

Tim
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Top

Postby tbi » Fri Jun 12, 2009 11:39 am

Excerpt from the Javadoc for the onKeyDown method:
"Called when a key was pressed down and not handled by any of the views inside of the activity. So, for example, key presses while the cursor is inside a TextView will not trigger the event (unless it is a navigation to another object) because TextView handles its own key presses. "

Try the following in your onCreate method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. city.setOnKeyListener(new OnKeyListener() {
  2.     @Override
  3.     public boolean onKeyDown(int keyCode, KeyEvent ke) {
  4.         if (keyCode == KeyEvent.KEYCODE_ENTER) {
  5.             AlertDialog.Builder alert = new AlertDialog.Builder(this);
  6.             alert.setMessage("Error");
  7.             alert.setCancelable(true);
  8.             alert.create().show();
  9.         }
  10.         return true;
  11.     }
  12. });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Greets,
/TBI

P.S.:
Why'd you want to show an error message anyway? If you don't want the TextView to be able to hold multiple lines (which would be my guess) you could simply add this xml attribute.
A la fin, il y aura seulement de la beauté.
tbi
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri May 29, 2009 8:22 am
Location: Marburg, Germany

Postby timweicker » Fri Jun 12, 2009 10:01 pm

Thank you!

That worked!
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests