Mega Code Archive

 
Categories / Android / UI
 

EditText focus event listener

package app.test; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Test extends Activity {   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     Button btn1 = (Button) findViewById(R.id.btn1);     btn1.setOnClickListener(btnListener);     Button btn2 = (Button) findViewById(R.id.btn2);     btn2.setOnClickListener(btnListener);     EditText txt1 = (EditText) findViewById(R.id.txt1);     txt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {       @Override       public void onFocusChange(View v, boolean hasFocus) {         Toast.makeText(getBaseContext(),             ((EditText) v).getId() + " has focus - " + hasFocus,             Toast.LENGTH_LONG).show();       }     });   }   private OnClickListener btnListener = new OnClickListener() {     public void onClick(View v) {       Toast.makeText(getBaseContext(),           ((Button) v).getText() + " was clicked", Toast.LENGTH_LONG)           .show();     }   };   @Override   public boolean onKeyDown(int keyCode, KeyEvent event) {     switch (keyCode) {     case KeyEvent.KEYCODE_DPAD_CENTER:       Toast.makeText(getBaseContext(), "Center was clicked",           Toast.LENGTH_LONG).show();       break;     case KeyEvent.KEYCODE_DPAD_LEFT:       Toast.makeText(getBaseContext(), "Left arrow was clicked",           Toast.LENGTH_LONG).show();       break;     case KeyEvent.KEYCODE_DPAD_RIGHT:       Toast.makeText(getBaseContext(), "Right arrow was clicked",           Toast.LENGTH_LONG).show();       break;     case KeyEvent.KEYCODE_DPAD_UP:       Toast.makeText(getBaseContext(), "Up arrow was clicked",           Toast.LENGTH_LONG).show();       break;     case KeyEvent.KEYCODE_DPAD_DOWN:       Toast.makeText(getBaseContext(), "Down arrow was clicked",           Toast.LENGTH_LONG).show();       break;     }     return false;   } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout     android:id="@+id/widget28"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     xmlns:android="http://schemas.android.com/apk/res/android"         >     <TextView                 android:layout_width="214dp"         android:layout_height="wrap_content"         android:text="Your Name"                 />     <EditText         android:id="@+id/txt1"         android:layout_width="214dp"         android:layout_height="wrap_content"                 />        <Button         android:id="@+id/btn1"         android:layout_width="106dp"         android:layout_height="wrap_content"         android:text="OK"         />     <Button         android:id="@+id/btn2"         android:layout_width="106dp"         android:layout_height="wrap_content"         android:text="Cancel"         /> </LinearLayout>