have to press 2 times for onKeyDown to effect

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

have to press 2 times for onKeyDown to effect

Postby ShareTung » Sun Sep 06, 2009 3:30 pm

I try to simplify LunarLander by not using message handling. My purpose is just to set the textview to be invisible.
but I have to press 2 times for onKeyDown to occur. Can you please tell me what wrong in my code.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestHindMsg extends Activity {
  2.  
  3.         TestHindView TestV;
  4.  
  5.     @Override
  6.  
  7.     public void onCreate(Bundle savedInstanceState) {
  8.  
  9.         super.onCreate(savedInstanceState);
  10.  
  11.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  12.  
  13.         setContentView(R.layout.main);
  14.  
  15.         TestV = (TestHindView)findViewById(R.id.hview);
  16.  
  17.         TestV.setTextView((TextView) findViewById(R.id.text));
  18.  
  19.         Log.w(this.getClass().getName(), "onCreate Completed");
  20.  
  21.     }
  22.  
  23. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestHindView extends SurfaceView{
  2.  
  3.         private TextView mStatusText;
  4.  
  5.         public TestHindView(Context context, AttributeSet attrs) {
  6.  
  7.                 super(context, attrs);
  8.  
  9.                 Log.w(this.toString(), "View Created");
  10.  
  11.                 setFocusable(true);
  12.  
  13.         }
  14.  
  15.     public void setTextView(TextView textView) {
  16.  
  17.         mStatusText = textView;
  18.  
  19.     }
  20.  
  21.         @Override
  22.  
  23.         public boolean onKeyDown(int keyCode, KeyEvent msg) {
  24.  
  25.                 Log.w(this.toString(), "onKeyDown detected");
  26.  
  27.                 mStatusText.setVisibility(View.INVISIBLE);
  28.  
  29.                 this.invalidate();
  30.  
  31.                 return false;
  32.  
  33.         }
  34.  
  35. }
  36.  
  37.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.         android:layout_width="fill_parent"
  4.  
  5.         android:layout_height="fill_parent" >
  6.  
  7.         <com.TestHindMsg.TestHindView
  8.  
  9.                 android:id="@+id/hview"
  10.  
  11.                 android:layout_width="fill_parent"
  12.  
  13.                 android:layout_height="fill_parent" />
  14.  
  15.         <RelativeLayout
  16.  
  17.                 android:layout_width="fill_parent"
  18.  
  19.                 android:layout_height="fill_parent">
  20.  
  21.                 <TextView
  22.  
  23.                         android:id="@+id/text"
  24.  
  25.                         android:text="XXXXXXXXXXXX"
  26.  
  27.                         android:layout_width="wrap_content"
  28.  
  29.                         android:layout_height="wrap_content" />        
  30.  
  31.         </RelativeLayout>
  32.  
  33. </FrameLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

Thank you very much.
ShareTung
Freshman
Freshman
 
Posts: 4
Joined: Sun Sep 06, 2009 2:32 pm

Top

Postby Frink » Mon Sep 07, 2009 9:01 am

If your having trouble understanding or using the Handler, I'd suggest learning that rather than removing it. It will make sure things stay synchronized - which could very well be the reason your having trouble.

I can't test this at the minute, but is there a reason your onKeyDown returns false (have you tried returning true?)
User avatar
Frink
Developer
Developer
 
Posts: 46
Joined: Sat Aug 15, 2009 7:42 pm

Postby ShareTung » Mon Sep 07, 2009 10:56 am

Yes, I tried returning true, but it work the same.
Just try to change onKeyDown to see if it work properly.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     @Override
  3.  
  4.     public boolean onKeyDown(int keyCode, KeyEvent msg) {
  5.  
  6.         if(flag) {
  7.  
  8.                 Log.w(this.toString(), "onKeyDown detected 1");
  9.  
  10.             mStatusText.setVisibility(View.INVISIBLE);
  11.  
  12.             mStatusText.setText("111111111111111");
  13.  
  14.             flag = false;
  15.  
  16.         }
  17.  
  18.         else {
  19.  
  20.             Log.w(this.toString(), "onKeyDown detected 2");
  21.  
  22.             mStatusText.setVisibility(View.VISIBLE);
  23.  
  24.             mStatusText.setText("222222222222222");
  25.  
  26.             flag = true;
  27.  
  28.         }
  29.  
  30.         return true;
  31.  
  32.     }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Everything is working fine except the first times I press button it show "No keyboard for id 0" and "Using default keymap: /~~~~" (default log) without "onKeyDown detected " log
but the secode press it show log "onKeyDown detected 1" as it should show.

I think it is just tricky problem but I don't know how to solve it.

Thank you for your help.
ShareTung
Freshman
Freshman
 
Posts: 4
Joined: Sun Sep 06, 2009 2:32 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests