Created
September 9, 2011 09:38
-
-
Save alexvbush/1205842 to your computer and use it in GitHub Desktop.
Android various definition of OnClickListeners.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AndroidOnClickDefinitionInXMLActivity extends Activity implements OnClickListener { | |
private Button buttonWithActivityAsListener; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
buttonWithActivityAsListener = (Button) findViewById(R.id.button_with_activity_as_listener); | |
buttonWithActivityAsListener.setOnClickListener(this); | |
} | |
/** You implement this method after adding OnClickListener interface to your Activity. **/ | |
public void onClick(View v) { | |
Toast.makeText(this, "Button with Activity as Listener has been clicked.", 2500).show(); | |
} | |
// | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AndroidOnClickDefinitionInXMLActivity extends Activity { | |
private Button buttonWithInlineAsListener; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
buttonWithInlineAsListener = (Button) findViewById(R.id.button_with_inline_class_as_listener); | |
buttonWithInlineAsListener.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inline Class as Listener has been clicked.", 2500).show(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AndroidOnClickDefinitionInXMLActivity extends Activity { | |
private Button buttonWithInnerClassAsListener; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
buttonWithInnerClassAsListener = (Button) findViewById(R.id.button_with_inner_class_as_listener); | |
buttonWithInnerClassAsListener.setOnClickListener(new InnerOnClickButtonListener()); | |
} | |
/** Inner Class to respond to OnClick events. **/ | |
class InnerOnClickButtonListener implements OnClickListener { | |
public void onClick(View v) { | |
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inner Class as Listener has been clicked.", 2500).show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AndroidOnClickDefinitionInXMLActivity extends Activity { | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
} | |
/** Name of the method and it's triggering comes from main.xml file. | |
* Button's widget attribute android:onClick allows you to specify name of the method | |
* which you have to implement. **/ | |
public void onButtonClick(View v) { | |
Toast.makeText(this, "Button Without a Listener has been clicked.", 2500).show(); | |
} | |
// | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.smartcloudcompany.onclickinxml; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.Toast; | |
public class AndroidOnClickDefinitionInXMLActivity extends Activity implements OnClickListener { | |
private Button buttonWithActivityAsListener; | |
private Button buttonWithInnerClassAsListener; | |
private Button buttonWithInlineAsListener; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
buttonWithActivityAsListener = (Button) findViewById(R.id.button_with_activity_as_listener); | |
buttonWithActivityAsListener.setOnClickListener(this); | |
buttonWithInnerClassAsListener = (Button) findViewById(R.id.button_with_inner_class_as_listener); | |
buttonWithInnerClassAsListener.setOnClickListener(new InnerOnClickButtonListener()); | |
buttonWithInlineAsListener = (Button) findViewById(R.id.button_with_inline_class_as_listener); | |
buttonWithInlineAsListener.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inline Class as Listener has been clicked.", 2500).show(); | |
} | |
}); | |
} | |
/** You implement this method after adding OnClickListener interface to your Activity. **/ | |
public void onClick(View v) { | |
Toast.makeText(this, "Button with Activity as Listener has been clicked.", 2500).show(); | |
} | |
// | |
/** Name of the method and it's triggering comes from main.xml file. | |
* Button's widget attribute android:onClick allows you to specify name of the method | |
* which you have to implement. **/ | |
public void onButtonClick(View v) { | |
Toast.makeText(this, "Button Without a Listener has been clicked.", 2500).show(); | |
} | |
// | |
/** Inner Class to respond to OnClick events. **/ | |
class InnerOnClickButtonListener implements OnClickListener { | |
public void onClick(View v) { | |
Toast.makeText(AndroidOnClickDefinitionInXMLActivity.this, "Button with Inner Class as Listener has been clicked.", 2500).show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
> | |
<Button android:id="@+id/button_with_activity_as_listener" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Button with Activity As Listener" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="100dp" | |
/> | |
<Button android:id="@+id/button_without_listener" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Button Without listener" | |
android:layout_gravity="center_horizontal" | |
android:onClick="onButtonClick" | |
/> | |
<Button android:id="@+id/button_with_inner_class_as_listener" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Button With Inner Class As Listener" | |
android:layout_gravity="center_horizontal" | |
/> | |
<Button android:id="@+id/button_with_inline_class_as_listener" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Button With Inline Class As Listener" | |
android:layout_gravity="center_horizontal" | |
/> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment