Activity key event : Activity « UI « Android






Activity key event

     
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>

   
    
    
    
    
  








Related examples in the same category

1.Backup Activity
2.Notification Activity
3.Timing Activity
4.Set content view from xml for Activity
5.More than one Activity
6.Find user control by using findViewById
7.A Simple Form
8.Link form with POJO
9.Life cycle
10.Comparing Android UI Elements to Swing UI Elements
11.add android:background = "#FFFF0000"
12.Rotation demo
13.Set activity screen Orientation
14.Check activity result
15.Activity lifecycle event
16.Allows the activity to manage the Cursor's lifecyle based on the activity’s lifecycle---
17.Activity configuration changed event
18.Check Activity result and onActivityResult
19.Phone Call Activity
20.Using Media Store Activity
21.External Storage Activity
22.Making Activity Go Full-Screen
23.Surface View Test Activity
24.Widget Preview Activity
25.This class provides a basic demonstration of how to write an Android activity.
26.This demonstrates the basic code needed to write a Screen activity
27.Example of removing yourself from the history stack after forwarding to another activity.
28.Fancy Blur Activity
29.Example of receiving a result from another activity.
30.Demonstrates required behavior of saving and restoring dynamic activity state
31.Securer Activity
32.This activity is an example of a simple settings screen that has default values.
33.Bind Click Action Activity
34.get ActivityManager
35.forward to another Activity
36.Request window features before setContentView
37.No code required here to attach the listener
38.Save data to Preference
39.Save instance state
40.onRetainNonConfigurationInstance
41.Create a user interface in code.
42.Redirect
43.Demonstrates how the various soft input modes impact window resizing.
44.extends Activity implements OnClickListener