hey i have displayed a pop-up screen when i click on a menu item
now i want to close that pop-up screen when user presses escape key.but it does not work and remain stuck,till i click on a button on the pop=up screen.
how can i achieve that????
filter is my pop-up screen
my code is :::
protected boolean keyChar(char c, int status, int time)
{
boolean retVal = false;
if (c == Characters.ESCAPE)
{
close();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
//UiApplication.getUiApplication().popScreen(filter);
UiApplication.getUiApplication().
popScreen(UiApplication.getUiApplication().getActiveScreen());//(filter);
}
});
retVal = super.keyChar(c,status,time);
}
return retVal;
}
i need to override the keychar method in pop-up screen,search for escape and then close
code :
popupscreen1=new PopupScreen(myverticalfieldmanager)
{
protected boolean keyChar(char c, int status, int time)
{
if (c == Characters.ESCAPE)
close();
return super.keyChar(c, status, time);
}
};
Related
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.
I need to create a LabelField which can perform click action. I have overrided navigationClick(int status, int time). But when I click on that LabelField, MenuButton popup is appearing. How to disable it. The code I'm using is given below:
LabelField pdtitem = new LabelField(pdts.getProducts_name(),LabelField.FOCUSABLE) {
protected boolean navigationClick(int status, int time) {
Dialog.alert("hai");
UiApplication.getUiApplication().pushScreen(new desc());
return super.navigationClick(status, time);
}
};
Consume event in your navigationClick() method.
return true instead of return super.navigationClick(status, time);
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 want to run some Java code when the user clicks on this ToolbarButtonField in my BlackBerry app. I have the following code which is not working. Please tell me where I am wrong.
butHome = new ToolbarButtonField(new StringProvider("Home"));
butHome.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Clicked...");
}
});
You can use:
ToolbarButtonField#invoke
Performs an action when this
ToolbarButtonField is clicked on if
Command has been set. A click is
defined as the following sequence of
touch events: TouchEvent.DOWN,
TouchEvent.CLICK, TouchEvent.UNCLICK
and TouchEvent.UP.
You're going to have to use that in conjuction with the Command framework. If that's not desirable, override ToolbarButtonField#touchEvent for a TouchEvent.UNCLICK event to execute the desired code.
public boolean touchEvent(TouchEvent message) {
if ( message.geEvent() == TouchEvent.UNCLICK ) {
// do what I want.
}
}
Try this:
butHome = new ToolbarButtonField(new StringProvider("Home")) {
protected boolean navigationClick(int status, int time) {
System.out.println("Clicked...");
return true;
}
});
when i am pressing the back button a pop screen is displayed which shows three button save, discard and cancel button i don't want this screen to be popped up. is this possible.
Thanks in advance
The default behaviour of the back button is to save changes for dirty screens. Rewrite the onClose() method to overwrite the default behaviour.
public boolean onClose() {
int choice = Dialog.ask(Dialog.D_YES_NO, "¿Do you want to exit?", Dialog.YES);
if (choice == Dialog.YES) {
//write a close() routine to exit
close();
}
return true;
}
You return true because you managed the ESC button pressed event. Review the Screen class docs.
You can also change the default behaviour of the ESC button rewriting the keyChar method as follows:
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ESCAPE) {
onClose();
return true;
}
return super.keyChar(character, status, time);
}
close() should be somenthing like:
public void close() {
System.exit(0);
}
Override the onSavePrompt method. Then that screen will not come. Actually that popup screen will come only when something is changed on your screen. So it will ask you for the appropriate action.
protected boolean onSavePrompt() {
return true;
}
Skip the saving prompt with it
protected boolean onSavePrompt() {
return false;
}
Override onClose() method like this:
public boolean onClose() {
close();
return true;
}
you will not get that annoying alert message.