|
|
When EditText widget edittext, has method OnKeyListener.onKey(..) detect a carriage return, a toast is shown and hideKeyboard() is called. Method hideKeyboard() makes use of a class object for InputMethodManager.
package com.brainyideas.dev.poc.fste;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
public class TextEdit extends Activity {
EditText edittext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
void init() {
edittext = (EditText) findViewById(R.id.edit_text);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(TextEdit.this, edittext.getText(), Toast.LENGTH_SHORT).show();
hideKeyboard();
return true;
}
return false;
}
});
}
void hideKeyboard() {
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/field_name"
/>
<EditText
android:id="@+id/edit_text"
android:inputType="text|textCapSentences|textAutoCorrect|textMultiLine"
android:windowSoftInputMode="stateVisible|adjustPan"
android:hint="@string/edit_hint"
android:lines="5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>