Is there a way to switch on and switch off the num lock (alt + aA) keys programmatically in BlackBerry. There is a method setMode() in KeyPad class would that help?
Keypad.setMode(mode) - internal method for keyboard mode indicator update (ex 0 - none, 1 - numeric, 2 - alphabets).
You can use something like
class NLEditField extends EditField {
boolean mNumlockOn = false;
protected boolean keyChar(char key, int status, int time) {
if (mNumlockOn)
key = Keypad.getAltedChar(key);
return super.keyChar(key, status, time);
}
}
By using the net.rim.device.api.ui.component.BasicEditField, or subclasses, or any widget that allows you to set a net.rim.device.api.ui.text.TextFilter you can specify complex input semantics that will interpret the key presses in context of the type of input you desire: INTEGER, NUMERIC, UPPERCASE, EMAIL, URL, etc.
Related
Hi I am using a text watcher on edit text
I want to restrict the number after certain length
suppose i want restrict length for 3
It should not allow 1234
but it should allow 123.(except dot(.) it should not allow any other character)
How can i do with TextWatcher.
Also tried to restrict all key entries except dot but not working
insideEdit.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 46) // dot key code is 46
{
return false;
}else {
return true;
}
}
});
Use
android:maxLength="4"
inside <EditText tag
I am making a custom android keyboard. I want to make it sure that when user press enter key proper EditorAction took place. Therefore I want to know is there any way to check if edittext is single line or not? Or is there any way to check min and max lines of editext?
In your onStartInput(EditorInfo info) and onStartInputView(EditorInfo info) callbacks EditorInfo is passed in.
To check if it's a multiline you can do:
if (0 != (info.inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE)) {
//This is multiline text field
}
See TYPE_TEXT_FLAG_MULTI_LINE
You can implement it by setting a key listener and and checking getMaxLines() and getMinLines() properties. Note that it is applied for API > 15!
EditText editText = new EditText(this);
editText.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent)
{
if (view instanceof EditText && ((EditText) view).getMaxLines() == 1 && ((EditText) view).getMinLines() == 1)
{
// this is a single line EditText view
}
return false;
}
});
I am working on ListView section, in this, the user can search the content by name and directly move at the first element of List via pressing a keyboard button. Like, if you press button B from (right vertical manager) it will scroll the list and move focus to first record of B.
The code is working fine in simulator but it's not working on Touch device - I have BB 9380 Curve.
here is the code for :
LabelField a = new LabelField("A" , FOCUSABLE)
{
protected void paint(Graphics graphics)
{
graphics.setColor(0xC4C4C4);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
//fieldChangeNotify(1);
injectKey(Characters.LATIN_CAPITAL_LETTER_A);
injectKey(Characters.LATIN_CAPITAL_LETTER_A);
return true;
}
};
private void injectKey(char key)
{
try
{
searchList.setFocus();
KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
inject.post();
/*inject.post();*/
} catch (Exception e) {
Log.d("In injectKey :: :: :: "+e.toString());
MessageScreen.msgDialog("In Inject Key "+e.toString());
}
}
Alternate Solution
I would recommend a different strategy for this. Instead of trying to simulate key press events, I would define one method that handles a keypress of a certain letter, or a touch click on that same letter's LabelField.
Source: blackberry.com
So, you can have code that handles key presses by using
protected boolean keyChar( char character, int status, int time )
{
// you might only want to do this for the FIRST letter entered,
// but it sounds like you already have the keypress handling
// the way you want it ...
if( CharacterUtilities.isLetter(character) )
{
selectLetter(character);
return true;
}
return super.keyChar( character, status, time );
}
and then also handle touch events:
LabelField a = new LabelField("A" , FOCUSABLE)
{
protected void paint(Graphics graphics)
{
graphics.setColor(0xC4C4C4);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
char letter = getText().charAt(0);
selectLetter(letter);
return true;
}
};
then, simply define a method that takes in one character, and scrolls to the start of that part of the list:
private void selectLetter(char letter);
Key Injection
If you really, really want to simulate key presses, though, you might try changing the code so that it injects two events: key down, and then key up (you're currently injecting two key down events). This might be causing problems.
injectKey(Characters.LATIN_CAPITAL_LETTER_A, true);
injectKey(Characters.LATIN_CAPITAL_LETTER_A, false);
with
private void injectKey(char key, boolean down)
{
try
{
searchList.setFocus();
int event = down ? KeyEvent.KEY_DOWN : KeyEvent.KEY_UP;
KeyEvent inject = new KeyEvent(event, key, 0);
inject.post();
} catch (Exception e) { /** code removed for clarity **/
}
}
Additional Note
For UIs, I like to trigger events on the key up, or unclick events. I think this makes a better experience for the user. So, you could replace keyChar() with keyUp() and navigationClick() with navigationUnclick() if you want to do this.
How to get selected row from blackberry objectlistfield, when user clicks on list item?
getSelectedIndex()
You will also have to set the setChangeListener() and implement the corresponding methods like fieldChanged() and keyDown()
have you read the documentation before asking ? Do you have a more specific question ?
public boolean navigationClick(int status, int time) {
Field focus = list.getLeafFieldWithFocus();
Dialog.alert("Focus String :: " + focus.getIndex());
if (focus instanceof ListField) {
ListField listField = (ListField)focus;
Dialog.alert("Selected Index"+listField.getSelectedIndex());
Dialog.alert("Selected List Value"+listField.getCallback().get(listField,
listField.getSelectedIndex()).toString());
}
return true;
}
I am trying to detect when the shift key (either side) is held down by the user (without pressing any other keys), but I can't figure out to do this. This is the only thing I found to detect pressing a shift key:
protected boolean keyStatus(int keycode, int time)
{
System.out.println("down");
boolean retVal = false;
int key = Keypad.key(keycode);
if( key == Keypad.KEY_SHIFT_LEFT )
{
// do something
retVal = true;
}
else if( key == Keypad.KEY_SHIFT_RIGHT )
{
// do something
retVal = true;
}
return retVal;
}
Shift doesn't trigger keyDown and keyUp, which would have been ideal. What am I missing?
Does holding down the shift key trigger multiple keypresses? If so, you could write a function to detect a certain number of keypresses within a given amount of time.