Tuesday, June 25, 2013

Android OnKeyListener Example

0 comments
In this post you will learn how to implement Android OnKeyListener with EditText

android onkeylistener javasrilankansupport.com

EditTextKeyListener

EditTextKeyListener class implement the Android OnKeyListener interface





package com.javasrilankansupport.androidkeylistner;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;

public class EditTextKeyListener extends Activity implements OnKeyListener {

 private EditText userName;
 private EditText password;

 private String mUserName;
 private String mPassword;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_keylistner);

  userName = (EditText) findViewById(R.id.txt_name);
  password = (EditText) findViewById(R.id.password);

  // set onkeyListner
  userName.setOnKeyListener(this);
  password.setOnKeyListener(this);

 }

 @Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {

  switch (v.getId()) {

  case R.id.txt_name:
   // keyup and "ENTER"
   if ((event.getAction() == KeyEvent.ACTION_DOWN)
     && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    mUserName = userName.getText().toString();
    if (mUserName.equals("")) {
     Toast.makeText(this, "Enter user name...",
       Toast.LENGTH_SHORT).show();

    } else
     password.requestFocus();
   }
   break;

  case R.id.password:
   // keydown and "ENTER"
   if ((event.getAction() == KeyEvent.ACTION_DOWN)
     && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    mPassword = password.getText().toString();
    if (mPassword.equals(""))
     Toast.makeText(this, "Enter password...",
       Toast.LENGTH_SHORT).show();

    else
     // show Toast Long time
     Toast.makeText(
       this,
       "User name : " + mUserName + "  Password : "
         + mPassword, Toast.LENGTH_LONG).show();
   }
   break;

  }
  return false;
 }

}

activity_keylistener.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditTextKeyListener" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:ems="10"
        android:inputType="textPersonName" 
        android:hint="@string/username"
       >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/txt_name"
        android:layout_below="@+id/txt_name"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="textPassword"
        android:hint="@string/psass" />

</RelativeLayout>


Download Android KeyListener project

0 comments:

Post a Comment