I need to get the text of the keys at the event of key press in Blackberry. This happens when the user presses a key from the the keypad in order to type text. How is that possible?
you can get the pressed key text by overriding keyChar like this
public boolean keyChar(char key, int status, int time)
{
if (key == Characters.ESCAPE)
{
int result = Dialog.ask(Dialog.D_YES_NO,"Are you sure you want to exit?");
if (result == Dialog.YES) {
closePopup();
}
return(true);
}
else
if (key == Characters.ENTER)
{
processLocation();
return(true);
}
else
{
//the pressed key is key
return(super.keyChar(key,status,time));
}
}
This helps you:
protected boolean keyChar(char ch, int status, int time)
{
if(ch == Characters.ESCAPE || ch == Characters.ENTER)
{
//Nothing to do;
}
else
{
pressedKey=pressedKey+ch;
}
return super.keyChar(ch, status, time);
}
Then you can get the values in pressedKey(it is a String variable you have to declare it first).
Related
I referred this link to listen the android EditText finish typing event. But from this reference i am getting the Enter key pressed event only. I need to listen an event when user closes keybord after finishing the typing and also when user presses Next. is there any way to listen theses events in android.
finally i got the answer for Next/Done/search AND enter key press like this
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event !=null){
if(actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_NEXT ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!event.isShiftPressed()) {
Log.e("", "FINISHED Typing : "+v.getText().toString());
return true; // consume.
}
}
}
else{
if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE){
Log.e("", "Next/Done/Search Pressed");
return true;
}
}
return false;
}
also for the Keypad dismiss i customized the editText and override the method onKeyPreIme into that. Whenever i need the edittext , i am using this edittext. this solved my issue.
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getAction()!=KeyEvent.ACTION_DOWN){
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
dispatchKeyEvent(event);
//do whatever you want to do here, when keypad dismiss on EditText
return false;
}
}
return super.onKeyPreIme(keyCode, event);
}
I have two editText fields, with following TextWatchers in their own addTextChangedListeners. Input is read and handled correctly but the number I enter is simply not shown on the display in the second field (editText2) (even though I wait (sleep() in afterTextChanged()) a while before proceeding with setting the values of both fields to null end setting focus to the first field.
What happens is on entering a number in the first field: number is diplayed in the field and focus is moved to the second field. What happens on entering a number in the second field: cursor (blinking vertical line) is frozen, no number is shown, after two seconds: cursor is moved to first field and both fields all empty. All of this is meant to happen except that the number entered in the second field should show and then the system should be frozen showing that number for a while before setting to null and moving on to the first field.
What is wrong and how to solve?
public class Spel extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spel);
editText1 = (EditText) findViewById(R.id.editText1);
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (before==0) { // alleen doen als editText1 leeg was
String v = s.toString();
if (v.equals("0") || v.equals("1") || v.equals("2") || v.equals("3") || v.equals("4") || v.equals("5") || v.equals("6") || v.equals("7") || v.equals("8") || v.equals("9")) {
editText2.requestFocus();
int baanWorpScore = Integer.parseInt(v);
banenWorpScore[0] = baanWorpScore;
}
else {
// blijf wachten op goede invoer
editText1.setText(null);
}
}
}
});
editText2 = (EditText) findViewById(R.id.editText2);
editText2.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (before==0) {
String v = s.toString();
if (v.equals("0") || v.equals("1") || v.equals("2") || v.equals("3") || v.equals("4") || v.equals("5") || v.equals("6") || v.equals("7") || v.equals("8") || v.equals("9")) {
editText1.requestFocus();
editText1.setText(null);
editText2.setText(null);
int baanWorpScore = Integer.parseInt(v);
banenWorpScore[1] = baanWorpScore;
}
else {
// blijf wachten op goede invoer
editText2.setText(null);
}
}
});
}
Found following solution, putting the delayed operations in a runnable, passed to method postDelayed of a (new) handler. This does exactly what I wished. Refer here for a post that helped me find where to look for the solution.
(I actually don't know if method removeCallbacks must actually be called)
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
Runnable mFilterTask = new Runnable() {
#Override
public void run() {
editText1.setText(null);
editText2.setText(null);
editText1.requestFocus();
}
};
Handler mHandler = new Handler();
if (before==0) {
String v = s.toString();
if (v.equals("0") || v.equals("1") || v.equals("2") || v.equals("3") || v.equals("4") || v.equals("5") || v.equals("6") || v.equals("7") || v.equals("8") || v.equals("9")) {
mHandler.removeCallbacks(mFilterTask);
mHandler.postDelayed(mFilterTask, 2000);
int baanWorpScore = Integer.parseInt(v);
banenWorpScore[1] = baanWorpScore;
}
}
}
in my application i have a Custom text box with BasicEditField.FILTER_NUMERIC. When the user enter the value in the field the comma should be added to the Currency format .
EX:1,234,567,8.... like this.
In my code i tried like this.
protected boolean keyUp(int keycode, int time) {
String entireText = getText();
if (!entireText.equals(new String(""))) {
double val = Double.parseDouble(entireText);
String txt = Utile.formatNumber(val, 3, ",");// this will give the //comma separation format
setText(txt);// set the value in the text box
}
return super.keyUp(keycode, time);
}
it will give the correct number format... when i set the value in the text box it will through the IllegalArgumentException. I know BasicEditField.FILTER_NUMERIC will not allow the charector like comma(,)..
How can i achieve this?
I tried this way and it works fine...
public class MyTextfilter extends TextFilter {
private static TextFilter _tf = TextFilter.get(TextFilter.REAL_NUMERIC);
public char convert(char character, int status) {
char c = 0;
c = _tf.convert(character, status);
if (c != 0) {
return c;
}
return 0;
}
public boolean validate(char character) {
if (character == Characters.COMMA) {
return true;
}
boolean b = _tf.validate(character);
if (b) {
return true;
}
return false;
}
}
and call like this
editField.setFilter(new MyTextfilter());
When i use this method, its throws illegal state expression on blackberry simulator.
protected boolean keyChar(char c, int status, int time)
{
if (c == Keypad.KEY_ESCAPE)
{
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
return super.keyChar(c, status, time);
}
if i give the return type true, No error display on simulator.
when i use the super.keyChar(c,status,time). it throw illegal state expression.
But in device no error display. Only on blackberry simulator. wht is the problem on it.
try this:
protected boolean keyChar(char c, int status, int time)
{
if (c == Keypad.KEY_ESCAPE)
{
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
});
return true;
}
return super.keyChar(c, status, time);
}
protected boolean keyChar(char c, int status, int time)
{
if (c == Characters.ESCAPE)
{
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
return true;
}
try this one. This will work.
And you can solve your problem by returning true from your if block and return super.keyChar(c, status, time); from else part. :)
I have added a BasicEditField to a GridFieldManager. When I test it, it allows input values like 11.11.11. How can I make my BasicEditField accept only correct double numbers, like 101.1 or 123.123. That is, allow only one decimal point.
gfm = new GridFieldManager(1, 2, 0);
gfm.add(new LabelField(" Enter value : "));
bef = new BasicEditField(BasicEditField.NO_NEWLINE|BasicEditField.FILTER_REAL_NUMERIC);
bef.setFilter(TextFilter.get(NumericTextFilter.REAL_NUMERIC));
bef.setFilter(TextFilter.get(TextFilter.REAL_NUMERIC));
bef.setText("1");
bef.setMaxSize(8);
gfm.add(bef);
add(gfm);
i had tried everything that i can. but the problem is yet in my app. can anyone give me a proper way to design a input field tha accepts decimal numbers?
Please add all the objects into the mainScreen with add(field);.
and then trying to get value of that fields.
now in your code put
String s = bef.getText();
Dialog.alert(s);
after
add(gfm);
and
To accept number like 1.1111.
then add
BasicEditField.FILTER_REAL_NUMERIC
in BasicEditFieldConstructor.
Now i think you got your solution.
finally i got the solution for a forum(forgot to copy the link)..
here it is...
inside my class i put the variables...
private int maxIntDigits = -1;
private int maxFractDigits = -1;
private String old;
i had added a BasicEditField, bef..
bef = new BasicEditField("","1");
bef.setMaxSize(8);
bef.setChangeListener(this);
add(bef);
And then in its fieldChanged().
public void fieldChanged(Field field, int context)
{
if(field==bef)
{
String str = bef.getText();
if(str.equals(""))
{
old = "";
//return;
}
if(str.indexOf('.') == str.lastIndexOf('.'))
{
if(str.indexOf('-') >= 0)
{
bef.setText(old);
}
if(validateIntPart(str) && validateFractPart(str))
{
old = str;
//return;
}
else
{
bef.setText(old);
}
}
else
{
bef.setText(old);
//return;
}
}
}
and then two functions in it...
private boolean validateIntPart(String str) {
if(maxIntDigits == -1) {
return true; //no limit has been set
}
int p = str.indexOf('.');
if(p == -1) {
p = str.length();
}
int digits = str.substring(0, p).length();
if(digits > maxIntDigits) {
return false;
} else {
return true;
}
}
private boolean validateFractPart(String str) {
if(maxFractDigits == -1) {
return true; //no limit has been set
}
int p = str.indexOf('.');
if(p == -1) {
return true; //if no '.' found then the fract part can't be too big
}
int digits = str.substring(p + 1, str.length()).length();
if(digits > maxFractDigits) {
return false;
} else {
return true;
}
}