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. :)
Related
My requirement is to create 5 stars when i click bitmap changes,When "Unclicked" bitmap changes back to default.and if star 1 is not clicked then all 4 stars after must not be clickable, if star 2 is clicked then star 3 must be clickable and stars 4-5 must not be clickable, (and then backwards) if all 5 stars have been selected only star5 must be clickable, if star 5 and 4 are unclicked then star3must be clickable stars 2-1 must not be clickable, etc.
public class Starscreen extends MainScreen
{
protected static BitmapField Star1 = null;
protected static BitmapField Star2 = null;
protected static BitmapField Star4 = null;
protected static BitmapField Star5 = null;
protected static final Bitmap StarClicked = null;
protected static BitmapField Star3 = null;
BitmapField bitmapField1;
private Bitmap StarNotClicked;
public Starscreen(Secondscreen secondscreen)
{
setTitle("Star Screen");
LabelField RateDeal = new LabelField("Rating: ");
Mainlayout.add(RateDeal);
HorizontalFieldManager StarManager=new HorizontalFieldManager
(USE_ALL_WIDTH);
final Bitmap StarNotClicked = Bitmap.getBitmapResource("Star3.png");
final Bitmap StarClicked = Bitmap.getBitmapResource("Star4.png");
Star1 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
private Object StarClicked;
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap((Bitmap) StarClicked);
Star2.setBitmap(StarNotClicked);
Star3.setBitmap(StarNotClicked);
Star4.setBitmap(StarNotClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(1);
return true;
}
};
Star2 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
private Object StarClicked;
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap((Bitmap) StarClicked);
Star2.setBitmap((Bitmap) StarClicked);
Star3.setBitmap(StarNotClicked);
Star4.setBitmap(StarNotClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(2);
return true;
}
};
Star3 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
// private Object StarClicked;
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap((Bitmap) StarClicked);
Star2.setBitmap((Bitmap) StarClicked);
Star3.setBitmap((Bitmap) StarClicked);
Star4.setBitmap(StarNotClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(3);
return true;
}
};
Star4 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap(StarClicked);
Star2.setBitmap(StarClicked);
Star3.setBitmap(StarClicked);
Star4.setBitmap(StarClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(4);
return true;
}
};
Star5 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap(StarClicked);
Star2.setBitmap(StarClicked);
Star3.setBitmap(StarClicked);
Star4.setBitmap(StarClicked);
Star5.setBitmap(StarClicked);
AmountOfStarsSelected(5);
return true;
}
};
StarManager.add(Star1);
StarManager.add(Star2);
StarManager.add(Star3);
StarManager.add(Star4);
StarManager.add(Star5);
Mainlayout.add(StarManager);
add(Mainlayout);
}
}
By executing above code i am getting an error and it shows create method for AmountOfStarsSelected(); but i dont have any idea what condition should i use inside AmountOfStarsSelected(); please help me i am new to blackberry
I'd say make a separate class for this, extended from HorizontalFieldManager. Have that class create the 5 buttons and lay them out, and contain a local state variable int level. Create a public setLevel(int level) and getLevel() method in this class. Where level refers to the star currently selected.
In setLevel put the following code:
public void setLevel(int level)
{
if(Math.abs(this.level - level) != 1)
{
return; // this.level refers to your local state. return because there is no change, or the user didn't click 1 up or down.
}
if(level == 0)
{
Star1.setBitmap(StarClicked);
}
else
{
Star1.setBitmap(StarNotClicked);
}
if(level == 1)
{
Star2.setBitmap(StarClicked);
}
else
{
Star2.setBitmap(StarNotClicked);
}
if(level == 2)
{
Star3.setBitmap(StarClicked);
}
else
{
Star3.setBitmap(StarNotClicked);
}
if(level == 3)
{
Star4.setBitmap(StarClicked);
}
else
{
Star4.setBitmap(StarNotClicked);
}
if(level == 4)
{
Star5.setBitmap(StarClicked);
}
else
{
Star5.setBitmap(StarNotClicked);
}
this.level = level;
}
In each of your stars you should call this setLevel method
Star1 = new BitmapField(StarNotClicked, BitmapField.FOCUSABLE){
protected boolean navigationClick(int status, int time)
{
setLevel(0); // If these fields are the only thing in your horizontal field you can get away with using setLevel(getIndex());
return true;
}
};
Remember that in your constructor you should set level to -1, then call setLevel(0) so that the first star can be selected by default.
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;
}
}
}
If the length returned by input.getText() is greater than 13, the last character entered by the user should not appear on the edit field. If the 13th character is a ',' the program should allow 2 additional characters after the ','. That way, the maximum length of the edit field would be 16.
What would be an option to limit the text width of an EditField like this?
input = new BorderedEditField();
input.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(input.getText().length() < 13)
input.setText(pruebaTexto(input.getText()));
else
//do not add the new character to the EditField
}
});
public static String pruebaTexto(String r){
return r+"0";
}
I have coded a simple BorderedEditField class which extends EditField. The method, protected boolean keyChar(char key, int status, int time) of this class gets modified so that manipulation of EditField's default behavior is possible. If you found this example helpful, then you can improve the implementation.
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen {
public MyScreen() {
BorderedEditField ef = new BorderedEditField();
ef.setLabel("Label: ");
add(ef);
}
}
class BorderedEditField extends EditField {
private static final int MAX_LENGTH = 13;
private static final int MAX_LENGTH_EXCEPTION = 16;
private static final char SPECIAL_CHAR = ',';
protected boolean keyChar(char key, int status, int time) {
// Need to add more rules here according to your need.
if (key == Characters.DELETE || key == Characters.BACKSPACE) {
return super.keyChar(key, status, time);
}
int curTextLength = getText().length();
if (curTextLength < MAX_LENGTH) {
return super.keyChar(key, status, time);
}
if (curTextLength == MAX_LENGTH) {
char spChar = getText().charAt(MAX_LENGTH - 1);
return (spChar == SPECIAL_CHAR) ? super.keyChar(key, status, time) : false;
}
if (curTextLength > MAX_LENGTH && curTextLength < MAX_LENGTH_EXCEPTION) {
return super.keyChar(key, status, time);
} else {
return false;
}
}
}
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).
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);
}
};