I am developing one project using LWUIT, Midlet mobile Application. when I press number keys a dialog box will open. when i press the keys #,0,* Dialog should be close.
I am using Dialog.dispose() method to close dialog. But it is not working. Below is my Code. Can anyone tell me what is the problem in my code?
public class javaForm extends Component implements ActionListener
{
Dialog d=new Dialog();
public void keyPressed(int key){
System.out.println("Key pressed :"+key);
switch(key)
{
case 48:
d.show(130,20,30,30,true);
break;
case 49:
d.show(130,20,30,30,true);
break;
case 50:
d.show(130,20,30,30,true);
break;
case 51:
d.show(130,20,30,30,true);
break;
case 52:
d.show(130,20,30,30,true);
break;
case 53:
d.show(130,20,30,30,true);
break;
case 54:
d.show(130,20,30,30,true);
break;
case 55:
d.show(130,20,30,30,true);
break;
case 57:
d.show(130,20,30,30,true);
break;
case 56:
d.show(130,20,30,30,true);
break;
case 42:
d.dispose();
break;
case 35:
d.dispose();
break;
default:
d.dispose();
break;
}
}
public void actionPerformed(ActionEvent ae)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
Actually javaForm is a java Program developed using LWUIT and am calling this javaForm inside of MIDLET which is javaForm1. I Included all the Necessary packages.
Why don´t you use Form.addGamekeyListener()?
Put the gameKeyListener in yout Form (extends ActionListener in the Form)and later in the actionPerformed(ActionEvent ae) capture the key with ae.getKeyEvent and close the Dialog.
Map the GameKeys with Canvas. For example: Canvas.FIRE.
Related
I want to play a sound while user save note in database here i'm using Media player to play sound and i have save the sounds file in raw folder.. Here is my Code
mediaPlayer = MediaPlayer.create(this,R.raw.save_effects);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.note_save:
mediaPlayer.start();
if (isEdit) {
updateNote(id,editText1.getText().toString(),editText2.getText().toString(),getDateTime(),Colors);
}else {
saveNote(editText1.getText().toString(),editText2.getText().toString(),getDateTime(),Colors);
break;
}
case R.id.color:
mediaPlayer.start();
openColorPicker();
break;
case android.R.id.home:
finish();
break;
}
return true;
}
Here i'm getting the problem while clicking on save button not playing the sound but selecting color it play sounds please some one help me out of this problem..
This is a Swtich Case program. I am not sure why this happens in this program. Can anyone explain why this happening?
class SwitchCase{
public static void main(String args[]){
switch(3*4+2){
case 12:
System.out.println("Hello");
case 14:
System.out.println("Hello World");
case 16:
System.out.println("Hello World Aqib");
case 18:
System.out.println("Hello World Mohd");
default:
System.out.println("Hahah....");
}
}
}
Output:
Hello World
Hello World Aqib
Hello World Mohd
Hahah....
After each case you need a break;
case 12:
System.out.println("Hello");
break;
.....
I am doing a sample Project where I am using Navigation Drawer with Fragments.When I click in row item1 it opens Dialog Fragment1 .In Dialog Fragment1 I have a button .
My requirement is I want to open same Dialog which is triggred from navigation row item1 when button in Fragment is clicked ...
I am using Following code to make Dialog in Activity
public void showRegisterDialog() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_register);
dialog.show();
}
and following code to open Dialog in Fragment
private void LoadFragmentView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new Fragment2();
showRegisterDialog();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
default:
break;
}
I need some guidelines,thank you..
I did some research and got the solution.
You need to use callback method in fragment.
For this purpose,you need to use the following codes:
your_button_on_fragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((YourActivityName)getActivity()).showRegisterDialog();
}
});
I am developing one project using LWUIT, Midlet mobile Application. when I press number keys a dialog box will open. when i press the keys #,0,* Dialog should be close.
I am using Dialog.dispose() method to close dialog. But it is not working. Below is my Code. Can anyone tell me what is the problem in my code?
public class javaForm extends Component implements ActionListener
{
Dialog d=new Dialog();
public void keyPressed(int key){
System.out.println("Key pressed :"+key);
switch(key)
{
case 48:
d.show(130,20,30,30,true);
break;
case 49:
d.show(130,20,30,30,true);
break;
case 50:
d.show(130,20,30,30,true);
break;
case 51:
d.show(130,20,30,30,true);
break;
case 52:
d.show(130,20,30,30,true);
break;
case 53:
d.show(130,20,30,30,true);
break;
case 54:
d.show(130,20,30,30,true);
break;
case 55:
d.show(130,20,30,30,true);
break;
case 57:
d.show(130,20,30,30,true);
break;
case 56:
d.show(130,20,30,30,true);
break;
case 42:
d.dispose();
break;
case 35:
d.dispose();
break;
default:
d.dispose();
break;
}
}
public void actionPerformed(ActionEvent ae)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
Actually javaForm is a java Program developed using LWUIT and am calling this javaForm inside of MIDLET which is javaForm1. I Included all the Necessary packages.
Why don´t you use Form.addGamekeyListener()?
Put the gameKeyListener in yout Form (extends ActionListener in the Form)and later in the actionPerformed(ActionEvent ae) capture the key with ae.getKeyEvent and close the Dialog.
Map the GameKeys with Canvas. For example: Canvas.FIRE.
Note: I'm using MonoTouch.
I get the following exception when I click on the tbName (textfield, see its delegate below) for the second time. The first time the modal view comes up without a problem, and I dismiss it (code also provided below). But the second time I click on the tbName (Textfield by the way), I get this exception:
"Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Attemping to being a modal transition from to while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear to know the current transition has completed"
The thing is though, it doesn't matter how long I wait before clicking, so I figure there's a fundamental I'm missing
From the NewEnvelopeViewController class:
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
tbName.TouchDown += delegate(object sender, EventArgs e) {
// Set everything up for the editting of the envelope's name
_dataViewController.NumericPad = false;
_dataViewController.FieldType = NewEnvelopeDataViewController.Fields.Name;
_dataViewController.EnvelopeToEdit = _envelope;
this.PresentModalViewController(_dataViewController, true);
};
From the NewEnvelopeDataViewController class:
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
if (NumericPad) {
tbValueArea.KeyboardType = UIKeyboardType.DecimalPad;
}
else {
tbValueArea.KeyboardType = UIKeyboardType.Default;
}
switch (FieldType) {
case Fields.Name:
NavigationItem.Title = "Enter the name";
tbValueArea.Text = EnvelopeToEdit.Name;
tbValueArea.KeyboardType = UIKeyboardType.Default;
break;
case Fields.Budget:
NavigationItem.Title = "Set your budget";
tbValueArea.Text = EnvelopeToEdit.Budget.ToString();
tbValueArea.KeyboardType = UIKeyboardType.DecimalPad;
break;
case Fields.Remainder:
NavigationItem.Title = "What remains of your budget?";
tbValueArea.Text = "0";
tbValueArea.KeyboardType = UIKeyboardType.DecimalPad;
break;
default:
throw new NotImplementedException("ViewDidAppear has a FieldType not accounted for");
}
}
partial void clickedDone (UIBarButtonItem sender)
{
switch (FieldType) {
case Fields.Name:
EnvelopeToEdit.Name = tbValueArea.Text;
break;
case Fields.Budget:
break;
default: // Not implemented yet
throw new NotImplementedException("FieldType not accounted for yet");
break;
}
this.ParentViewController.DismissModalViewControllerAnimated(true);
}
I've seen an article on StackOverflow regarding doing something with DidAppear, but it didn't seem to work in my case and it wasn't clear what exactly the problem is to me (rather than just patching over it).
Move the following code,
tbName.TouchDown += delegate(object sender, EventArgs e) {
// Set everything up for the editting of the envelope's name
_dataViewController.NumericPad = false;
_dataViewController.FieldType = NewEnvelopeDataViewController.Fields.Name;
_dataViewController.EnvelopeToEdit = _envelope;
this.PresentModalViewController(_dataViewController, true);
};
from the ViewDidAppear method to the ViewDidLoad method. The ViewDidAppear gets triggered everytime the view appears, including when a modal view controller gets dismissed. Hence, what your code right now is doing is adding another handler to the object's TouchDown event so when it is triggered, it always executes one more extra time, resulting in presenting the same modal view controller many times. That's why it works the first time, but the second time it crashes.
In general, if you want to subscribe to events in ViewDidAppear, be sure to unsubscribe them in the ViewDidDisappear or, even better, always subscribe events in ViewDidLoad and unsubscribe in ViewDidUnload.