package com.somitsolutions.java.SimulationOfAndroidButtonClickEvent;
public class View {
static class ListenerInfo {
OnClickListener mClickListener;
OnLongClickListener mLongClickListener;
}
ListenerInfo mListenerInfo;
//Lazy Initialization
ListenerInfo getListenerInfo() {
if (mListenerInfo != null) {
return mListenerInfo;
}
mListenerInfo = new ListenerInfo();
return mListenerInfo;
}
public void performClick() {
mListenerInfo.mClickListener.onClick(this);
}
public interface OnClickListener
{
public void onClick (View view);
}
public interface OnLongClickListener
{
public void onLongClick (View view);
}
public void setOnClickListner(OnClickListener li){
getListenerInfo().mClickListener = li;
}
public void setOnClickListner(OnLongClickListener li){
getListenerInfo().mLongClickListener = li;
}
}
package com.somitsolutions.java.SimulationOfAndroidButtonClickEvent;
public class Button extends View {
private String mButtonText;
public Button ()
{
}
public String getButtonText() {
return mButtonText;
}
public void setButtonText(String buttonText) {
this.mButtonText = buttonText;
}
}
package com.somitsolutions.java.SimulationOfAndroidButtonClickEvent;
public class Activity implements View.OnClickListener{
Button button;
public Activity() {
button = new Button();
button.setOnClickListner(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(view.equals(button)) {
button.setButtonText("You clicked me...");
System.out.println(button.getButtonText());
}
}
}
package com.somitsolutions.java.SimulationOfAndroidButtonClickEvent;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Activity activity = new Activity();
View view = (View)activity.button;
view.performClick();
}
}
No comments:
Post a Comment