How to get new edit Text on Enter Key pressed event in Dialogbox using Android programing? Can you provide sample code for that? - android-edittext

I had tried through below code but not getting key event.
editText1.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(keyCode==KeyEvent.KEYCODE_ENTER))
{
editText1.clearFocus();
editText2.requestFocus();
return true;
}
return false;
}
});

use event.getKeyCode() instead of just keyCode
editText1.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(event.getKeyCode()==KeyEvent.KEYCODE_ENTER))
{
editText1.clearFocus();
editText2.requestFocus();
return true;
}
return false;
}
});

I would use Textwatcher instead:
try this
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if (s.length()>0 && s.subSequence(s.length()-1, s.length()).toString().equalsIgnoreCase("\n"))
{
editText1.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
editText1.clearFocus();
editText2.requestFocus();
}
}
});

Related

I'm using **Libgdx**, input events don't work when using **Multi-OS Engine**

It works fine in Android and Desktop. But when running on iOS, it doesn't respond any input event.
I'm using gdxVersion 1.9.5, multi-os engine 1.2.3
I wrote a test class extends input adapter and override touchDown and touchUp method. But it doesn't respond any of the input events which work fine in Android.
public class TestScreen extends ScreenAdapter implements InputProcessor {
private CardGame game;
private OrthographicCamera guiCam;
public TestScreen(CardGame game){
this.game = game;
guiCam = new OrthographicCamera(Consts.WORLD_WIDTH, Consts.WORLD_HEIGHT);
Gdx.input.setInputProcessor(this);
}
public void draw () {
GL20 gl = Gdx.gl;
gl.glClearColor(1, 0, 0, 1);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
guiCam.update();
}
public void render (float delta) {
draw();
}
#Override
public boolean keyDown(int keycode) {
System.out.println("key down");
return true;
}
#Override
public boolean keyUp(int keycode) {
System.out.println("key up");
return true;
}
#Override
public boolean keyTyped(char character) {
System.out.println("key typed");
return true;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
System.out.println("touch down");
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
System.out.println("touch up");
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
System.out.println("touch dragged");
return true;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
System.out.println("mouse moved");
return true;
}
#Override
public boolean scrolled(int amount) {
System.out.println("scrolled");
return true;
}
}

How to know instantly if a user presses space in edittext android

following are the methods i use to get the key event, which are working fine on emulator but doesn't seem to work on device.
following are the methods i use to get the key event, which are working fine on emulator but doesn't seem to work on device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_SPACE) {
Log.e("onkeydown", "onkeydown");
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
Log.e("dispatch", "dispatch");
}
return super.dispatchKeyEvent(event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
Log.e("onkeyup", "onkeyup");
}
return super.onKeyUp(keyCode, event);
}
Try this..
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int key, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN && key == KeyEvent.KEYCODE_ENTER) {
//write code when key press
return true;
}
return false;
}
});

I cannot access the KeyPressed function for when i press the ESCAPE key

The first problem is that the addKeyListener does is redlined wherever I place it. I've looked at various different examples online but it seems that Im missing something.
here is my code:
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
public class BBMIDLET extends javax.microedition.midlet.MIDlet implements KeyListener
public void startApp() {
Display.init(this);
addKeyListener(new TestKeyPadListener());
}
public class TestKeyPadListener implements KeyListener {
public boolean keyChar(char key, int status, int time) {
System.out.println("key: " + key);
return false;
}
public boolean keyDown(int keycode, int time) {
System.out.println("keycode: " + keycode);
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
System.out.println("Hi");
return false;
}
return true;
}
public boolean keyUp(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyRepeat(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean keyStatus(int keycode, int time) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
Thanks in advance
addKeyListener(KeyListener l) is not a method of javax.microedition.midlet.MIDlet or net.rim.device.api.system.KeyListener and you did not declare it anywhere else in your BBMIDLET class so it is undefined.

Adding Editfield to Popup screen

I have created my own custom popup screen to which now I am trying to add a editfield , everything seems to be fine but the problem is that I am not able to write anything in the edit field
class sveetIt extends PopupScreen implements FieldChangeListener, DialogClosedListener {
Hashtable pitemData;
ButtonField sveetNowlabelField;
ButtonField sveetLaterlabelField;
WatingScreen watingScreen;
long scheduledTime;
Dialog updateDialog;
public sveetIt() {
super(new MyCustimGridFieldManager());
LabelField messageLabelField = new LabelField("Enter your Sveet Message:",Field.FIELD_HCENTER) {
protected void paint(Graphics graphics) {
graphics.setColor(Color.YELLOWGREEN);
super.paint(graphics);
}
};
EditField sveetTexteditField= new EditField(null, "Sveet Message", 50, EditField.FIELD_HCENTER
| EditField.FIELD_VCENTER
| EditField.NON_SPELLCHECKABLE | EditField.NO_NEWLINE);
VerticalFieldManager buttonVFManager = new VerticalFieldManager(VerticalFieldManager.FIELD_HCENTER);
HorizontalFieldManager hfManager = new HorizontalFieldManager();
sveetNowlabelField = new ButtonField("Sveet Now");
sveetLaterlabelField = new ButtonField("Sveet Later");
sveetNowlabelField.setChangeListener(this);
sveetLaterlabelField.setChangeListener(this);
add(messageLabelField);
add(sveetTexteditField);
hfManager.add(sveetNowlabelField);
hfManager.add(sveetLaterlabelField);
buttonVFManager.add(hfManager);
add(buttonVFManager);
}
public boolean isEditable() {
return true;
}
protected boolean keyChar(char c, int status, int time) {
boolean retVal = false;
if (c == Characters.ESCAPE) {
Ui.getUiEngine().popScreen(this);
retVal = super.keyChar(c, status, time);
}
return retVal;
}
public void fieldChanged(Field field, int context) {
if (sveetNowlabelField == field) {
//Here directly starts uploading file
beginUpload();
} else if (sveetLaterlabelField == field) {
// first picks up time when to upload
scheduledTime = getScheduleTime();
if(scheduledTime!=1L) {
//now begins uploading file
beginUpload();
}
}}
class SubscribingThread extends StoppableThread {
int network = 50;
public void run() {
}
}
public void beginUpload() {
try {
watingScreen = new WatingScreen("Uploading Sveet...");
/*
* UiApplication.getUiApplication().invokeAndWait( new Runnable() {
* public void run() { Ui.getUiEngine().pushScreen(watingScreen); }
* });
*/
BaseScreen.pushScreen(watingScreen);
Thread thread = new Thread(new Runnable() {
public void run() {
uploadToServer();
}
});
thread.start();
// uploadToServer();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
private long getScheduleTime() {
scheduledTime = 0;
final DateTimePicker datePick = DateTimePicker.createInstance();
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Calendar currentCalendar = datePick.getDateTime();
datePick.setMinimumDate(currentCalendar);
datePick.doModal();
Calendar cal = datePick.getDateTime();
if (cal.after(currentCalendar)) {
Date date = cal.getTime();
Dialog.alert("You selected " + date.toString());
scheduledTime = date.getTime();
} else {
Dialog.alert("Invalid date selection");
scheduledTime = 1L;
}
}
});
System.out.println("date in MilliSeconds is:" + scheduledTime);
return scheduledTime;
}
public void uploadToServer() {
} public void dialogClosed(Dialog arg0, int arg1) {
}
}
class MyCustimGridFieldManager extends VerticalFieldManager {
public MyCustimGridFieldManager() {
super(VERTICAL_SCROLL | USE_ALL_WIDTH | FIELD_HCENTER);
}
protected void paint(Graphics gr) {
super.paint(gr);
}
}
try this:
protected boolean keyChar(char c, int status, int time) {
if (c == Characters.ESCAPE) {
Ui.getUiEngine().popScreen(this);
}
return super.keyChar(c, status, time);
}
Try adding Field.EDITABLE to your style for the EditField.

BlackBerry Bitmap listener

I have a code similar to the one below, painting over the mapfields this mIcon several times.
How can I add a click listener to this bitmap ? I am using bb 5.0
public Bitmap mIcon;
mIcon = Bitmap.getBitmapResource("pcture1.png");
protected void paint(Graphics g) {
super.paint(g);
mDest = new XYRect(....);
g.drawBitmap(mDest, mIcon, 0, 0);
}
Override BitmapField and modify the isFocusable(), navigationClick(), keyChar(), and trackwheelClick() methods.
public class ImageButtonField extends BitmapField
{
public ImageButtonField(Bitmap image)
{
super(image);
}
public boolean isFocusable()
{
return true;
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time)
{
if(Characters.ENTER == character || Characters.SPACE == character)
{
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}

Resources