I am using Intellij IDEA 11, Android 4.1 and using the example in http://www.mkyong.com/android/android-textbox-example/ to display toast text after a user enters a text in the edit text then presses enter. The problem is that the enter key always creates a new line, the keyboard is still there and the toast is not displayed. I’m using emulator ARM with skin WVGA800 to test this if that makes any difference.
I tried diffrent things including:
edittext.requestFocus();
edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true); and
edittext.setMaxLines(1);
but it is still not working.
Here is the xml code:
<?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"
>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
Here is the java code:
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Toast;
public class T_C_Activity extends Activity {
/**
* Called when the activity is first created.
*/
private EditText edittext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addKeyListener();
}
public void addKeyListener() {
// get edittext component
edittext = (EditText) findViewById(R.id.editText);
//edittext.setFocusable(true);
//edittext.setFocusableInTouchMode(true);
//edittext.setMaxLines(1);
// add a keylistener to keep track user input
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and "enter" is pressed
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
edittext.requestFocus();
// display a floating message
Toast.makeText(T_C_Activity.this,
edittext.getText(), Toast.LENGTH_LONG).show();
return true;
} else if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_9)) {
edittext.requestFocus();
// display a floating message
Toast.makeText(T_C_Activity.this,
"Number 9 is pressed!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
}
Thanks in advance.
Take a look at the imeOptions section of the API documentation.
For example, you could do something like this:
Then, You can listen for editor events like this: