I'm very new to Java and OOP. Here is what I've got:
public class AmountChanged implements View.OnFocusChangeListener {
#Override
public void notFocused(View edittext1, boolean focused) {
//Do this awesome stuff
}
How do I instantiate and use this on one of my editText boxes in the mainActivity? I have already declared the editText boxes in the onCreate method.
In your class where you are writing the code for the editext,in that activity's onCreate() method, you need to write
yourEditext.setOnFocusChangeListener(new AmountChanged());
Also give an eye to this please as you can use anonymous classes too.
Let's imagine you created an EditBox:
EditText editText = new EditText(this);
To set focus change listener, you should provide OnFocusChangeListener instance to the setOnFocusChangeListener. Since AmountChanged implements OnFocusChangeListener, you can do the following:
editText.setOnFocusChangeListener(new AmountChanged());
If you are going to use the same listener on many EditText items, you can save this listener as a variable somewhere:
View.OnFocusChangeListener myListener = new AmountChanged();
...
editText.setOnFocusChangeListener(myListener);
In the onCreate method where you have the editText box(es) you want to use this with,
View.OnFocusChangeListener ac = new AmountChanged();
editText.setOnFocusChangeListener(ac);
From the View Android Developer Guide,
Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, all views will let you set a listener to be notified when the view gains or loses focus. You can register such a listener using setOnFocusChangeListener(View.OnFocusChangeListener). Other view subclasses offer more specialized listeners. For example, a Button exposes a listener to notify clients when the button is clicked.
Listener in android means that it is gonna listen to some event(OnTouchListener, OnClickListener, OnFocusChangedListener etc.). As you see OnFocusChangedListener interface is announced inside View class, in scope of Android it usually means that any child of View can produce this event, so you need to "listen" to those events.
In scope of EdiText what you have to do is something like this:
editText.setOnFocusChangedListener(new AmmountChanged());
EdiText is a child of View. So we are start "listening" to all OnFocusChanged events that will happen inside editText by registering our instance implementation OnFocusChangeListener.
Related
I'm playing with Android and I'd like to know if there's any way to invoke a listener call programmatically, for example, having an onClick() listener and invoke a call to this listener without touching the screen when the activity is created.
There is no way to get the set OnClickListener. So you need to store it and call your OnClickListener directly.
OnClickListener store = new OnClickListener() {/*...*/};
view.setOnClickListener(store);
store.onClick(view);
Never tried that, but after assigning a clickListener to your object (for example a Button), call on your onCreate method myButton.performClick().
Android doc :
public boolean performClick ()
Added in API level 1
Call this view's OnClickListener, if it is defined. Performs
all normal actions associated with clicking: reporting accessibility event,
playing a sound, etc.
Returns
True there was an assigned OnClickListener that was called,
false otherwise is returned.
Although this is possible, I'd actually advise against it. Listeners should be called from the UI but the business logic behind it is what should actually be called directly. This would provide "separation of concern" between both layers.
You should be calling the code that the listener calls in it's onClick method rather than invoking the onClick directly.
let me take a interface in Android-
OnClickListener and it's method is setonClickListener(), it takes object of
View.OnClickListener or it takes as this
why it is taking parameters--that object or this
The listener object which implements the event call back method.
You can register a listener on the view using setOnClickListener(). This listener will be called by android when the view is clicked. The listener can be any object implementing the OnClickListener interface.
And the method called by android on the listener will be onClick(). And android will pass the view which has been clicked to the method, so that the listener knows which view has been clicked. This allows using the same listener for several views.
I need help. I am struggling to get my Observers working in java. Can someone explain to me using MODEL-VIEW-CONTROLLER Architecture how to create and observer from View To Controller.
This is because if i press a button on the view the action event has to call notify the controller of that button being pressed.
For that I'm implementing observers to minimize class coupling.
I have a class Controller, View (Swing using JFrame), and an Application Class that holds the main method.
I tried implementing it so that Controller implements Observer and the View extends Observable.
After triggering the event of clicking the button all code except the notifyObservers("OBJECT") gets called. It disappears somewhere in the java library.
Any Help Will be much appreciated.
the model should extend observable and the view should implement observer (you want the view to depend on the model). you will need to call setChanged to after you change the state of the model to force the observers to be notified.
Double check, that your controller is really observing/listening to the (correct) button instance. Use a debugger and set some breakpoints to check whether notifyObservers is called and who is receiving the notification.
sorry if this question is stupid, but I can't wrap my head around Java syntax..I learnt C/C++
I know View is a class which is good..but I don't understand if View.OnClickListener() is a method.
I doubt it unless it returns an object?
I think View is a class which has a static OnClickListener member object..again that doesn't make sense to me..
Can some explain what is happening with this line of code?
button1 = (Button) findByView(R.id.button1) ;
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
}
}
So what is happening with this code?
Button1 is a reference to the button1 object in the xml file.
button1 object has a member object setOnClickListener which I from its name I assume initializes an event to the button or something. But it receives View.OnClicListener() object.
I am confused by that..onClick receives a View object so onClickListener is not an object returns a View object?? I don't get it at all.
Can someone explain what happens in that line View.onClickListener() is it another way of saying new this?
View.OnClickListener is an interface, you don't call it, but creates a new instance of it (new View.OnClickListener() is a call to the constructor)
The instance you create is of anonymous class that implements View.OnClickListener, in the brackets right under new View.OnClickListener()
Any class that implements View.OnClickListener must implement the methods declared in it (e.g. onClick)
setOnClickListener just saves the reference to the View.OnClickListener instance you supplied, and when someone clicks the button, the onClick method of the listener you set is getting called.
OnClickListener is an interface. An interface provides a set of Methods other classes can implement. http://download.oracle.com/javase/tutorial/java/concepts/interface.html
You could have another class (Like and adapter), that extends OnClickListener, then your Adapter class could add the method "OnClick(View v)", and it would also be able to handle Click events. Or you could use the code you posted, where you just create an anonymous class, that implements OnClickListener.
-Woody
Android code is geared for event based responses. The block of code is as follows:
Find a button that you've added to the active layout, and assign it to a local variable:
button1 = (Button) findByView(R.id.button1);
Set the on click listener for the button. This is a class that will be invoked when the button registers an event. The class is constructed here, it is anonymous as you don't assign it to a variable, but android will keep track of the reference.
Button events are always due to being pressed, so when the button registers that it has been pressed, it will inform the onClickListener class that the even occurred, and pass itself in as the view. The onClickListener is constructed as:
new View.OnClickListener()
{
public void onClick(View v)
{
}
}
That onClick method is used by the listener to handle the event (in this case, a button press). So, you would put the code you would like executed in that method.
To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.
I am unable to understand the meaning of Has****Handlers interfaces in GWT. What would be the difference if a class implements HasClickHandlers (addClickHandler) and ClickHandler (onClick) interfaces.
thank you
HasClickHandlers - something that can be clicked, e.g. a button
ClickHandler - some code that handles on a click
A HasClickHandlers object is a widget, like a button, that can react when the user clicks on it. But a button by itself does not know what should happen when a user clicks on it. A developer can craft a ClickHandler object, which is some code that implements what should happen when the user clicks on that button. A button can be given a ClickHandler to react to the user's click, i.e. the button can have/hold a click handler - HasClickHandlers.
One may ask why does GWT say applications should define view interfaces with method signatures like:
HasClickHandlers getSaveButton();
instead of simply
Button getSaveButton();
Google advocates decoupling the view from presenter. The presenter usually cares very little for all the functionality of a button - it usually only cares that the button is something that can take a click handler and use it. An interface like HasClickHandler has very few methods and is very easy to mock. Using a real button however will sometimes require initializing some or part of the whole UI framework and instantiating prerequisite context classes in order to create a button for testing.
By having the interface return HasClickHandler instead Button, the unit test code for the presenter can decouple completely from the complexity of the UI framework by simply mocking interfaces like HasClickHandler. This means simpler test scaffolding and very fast unit tests (since you don't have the overhead of initializing/interacting with a UI framework).
http://googletesting.blogspot.com/2009/08/tott-testing-gwt-without-gwttest.html
Edit
OP asks:
ok, e.g. if ABC class implements Hasclickhandlers and Clickhandler and then onClick and addClickHandler (which returns HandlerRegistration), it means that 1)it will act on click event thru onClick method and 2)will let any other class(es) know (who is implementing ClickHandler and used addClickHandler of ABC class to register the event) that click has just been occurred? right?
Your classes like ABC will not implement HasClickHandlers. Only GWT widgets like buttons implement HasClickHandlers. Google is simply providing the HasClickHandlers interface as an alternative way to declare variable references to some widgets like buttons. These widgets will notify registered ClickHandler about a button click.
Your class ABC may implement ClickHandler or may contain an inner (possible anonymous) class that derives from ClickHandler. A typical usage looks like:
public class ABC {
...
getSaveButton().addClickHandler(
new ClickHandler() {
public void onClick(ClickEvent event) {
saveToDatabase();
}
}
}
...
The HasClickHandlers is for objects that generate click events. The ClickHandler is for objects that deal with the events.
For example, a Button will generate a click event. When you want to handle a click event, you create a ClickHandler that contains the code that does so. You register this ClickHandler with the Button object so that when a click happens, the Button knows who to tell about it. The HasClickHandlers interface is implemented by Button (via the FocusWidget parent class) which is just the interface for registering ClickHandlers. This interface simply standardizes the registering of ClickHandlers.