2010-11-13 8 views
5

hi dove utilizzare onKey e onKeyUp/Down evento in Android.dove e come utilizzare onKey, onkeyDown, onKeyErrore evento in Android?

ad es. ho una vista testuale. quando l'utente preme un tasto qualsiasi voglio visualizzare quel carattere in textview, In questo caso viene utilizzato l'evento (sopra).

PLEASE explain with EXAMPLE 

Oppure dare qualche altro esempio che ottiene l'evento chiave e stampa in edittext o in un altro.

Grazie in anticipo ...

+3

Per guadagnare la reputazione, e fai in modo che gli altri rispondano alla tua domanda, accetta le buone risposte delle tue domande. –

risposta

3

i pls si riferiscono il seguente codice

public class Demo extends Activity 
{ 

    /** 
    * Variables & Objects Declaration 
    * 
    */ 


    EditText et; 

    private static Context CONTEXT; 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.main); 
     et =(EditText)findViewById(R.id.header_text02); 
     }// end of OnCreate 

    @Override 
    public boolean onKeyDown(View arg0, Editable arg1, int arg2, KeyEvent arg3) { 
     // TODO Auto-generated method stub 
     Log.v("I am ","KeyDown"); 
      switch (keyCode) { 
        case KeyEvent.KEYCODE_A: 
        { 
         //your Action code 
         et.setText("A"); 
         return true; 
        } 
         case KeyEvent.KEYCODE_B: 
        { 
         //your Action code 
         et.setText("B"); 
         return true; 
        } 
        // similarly write for others too 
     } 



     return true; 
    }// End of onKeyDown 



    @Override 
    public boolean onKeyUp(View arg0, Editable arg1, int arg2, KeyEvent arg3) { 
     // TODO Auto-generated method stub 
     Log.v("I am ","KeyUp"); 
      et.setText("KeyUp"); 
     return true; 
    }// End of onKeyUp 



} 
+0

@shankar puoi aiutarmi a mostrare un messaggio di avviso sulla pressione del tasto home. – Kishore

12

Se cercate questo EditText, è meglio utilizzare questi

editText.addTextChangedListener(new TextWatcher() { 
       public void afterTextChanged(Editable s) { 
        Log.v("TAG", "afterTextChanged"); 
       } 

       public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
        Log.v("TAG", "beforeTextChanged"); 
       } 

       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        Log.v("TAG", "onTextChanged"); 
       } 
      }); 
+0

migliore soluzione per me! –

Problemi correlati