How to create input textbox in Android and capture ENTER key event
In this example we will learn the following
- Create input textbox (EditText) using XML layout resource
- Retrieve the value entered in the textbox
- Ability to capture EditText ENTER key event
Source for the Screen Layout - main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"
android:layout_marginBottom="10dp" />
<TextView android:id="@+id/someText1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dp"
android:text="Text Box1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText android:id="@+id/editText1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10"
android:inputType="textMultiLine" android:layout_marginBottom="10dp">
<requestFocus />
</EditText>
<TextView android:id="@+id/someText2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dp"
android:text="Text Box2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText android:id="@+id/editText2" android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10"
android:inputType="textMultiLine" android:layout_marginBottom="10dp">
</EditText>
<TextView android:id="@+id/responseText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Source for the Activity - AndroidEnterKeyActivity.java
package com.as400samplecode;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidEnterKeyActivity extends Activity implements OnKeyListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setOnKeyListener(this);
EditText editText2 = (EditText) findViewById(R.id.editText2);
editText2.setOnKeyListener(this);
}
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
TextView responseText = (TextView) findViewById(R.id.responseText);
EditText myEditText = (EditText) view;
if (keyCode == EditorInfo.IME_ACTION_SEARCH ||
keyCode == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (!event.isShiftPressed()) {
Log.v("AndroidEnterKeyActivity","Enter Key Pressed!");
switch (view.getId()) {
case R.id.editText1:
responseText
.setText("Just pressed the ENTER key, " +
"focus was on Text Box1. " +
"You typed:\n" + myEditText.getText());
break;
case R.id.editText2:
responseText
.setText("Just pressed the ENTER key, " +
"focus was on Text Box2. " +
"You typed:\n" + myEditText.getText());
break;
}
return true;
}
}
return false; // pass on to other listeners.
}
}
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.