Four ways to spell activity jump button events in Android _android

Source: Internet
Author: User
Tags anonymous

Specific implementation code:

public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {SUPER.ONCR 
Eate (savedinstancestate); 
Setcontentview (R.layout.activity_main); Method 1. Use the Class (Button) Findviewbyid (R.ID.BTN1) that implements the Onclicklistener interface. Setonclicklistener (new View.onclicklistener () {@ 
Override public void OnClick (View v) {Intent Intent = new Intent (mainactivity.this, Buttonactivity1.class); 
StartActivity (Intent); 
} 
}); Method 2. An anonymous inner class ((Button) Findviewbyid (R.ID.BTN2)) is used. Setonclicklistener (new View.onclicklistener () {@Override public void Oncl 
Ick (View v) {Intent Intent = new Intent (mainactivity.this, Buttonactivity2.class); 
StartActivity (Intent); 
} 
}); Method 3. The activity directly implements the Onclicklistener interface ((Button) Findviewbyid (R.ID.BTN3)). Setonclicklistener (New View.onclicklistener () {@ 
Override public void OnClick (View v) {Intent Intent = new Intent (mainactivity.this, Buttonactivity3.class); 
MainActivity.this.startActivity (Intent); 
} 
}); Method 4.The label directly labels the trigger event (Button) Findviewbyid (R.ID.BTN4). Setonclicklistener (New View.onclicklistener () {@Override public void O 
Nclick (View v) {Intent Intent = new Intent (mainactivity.this, Buttonactivity4.class); 
MainActivity.this.startActivity (Intent); 
} 
}); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action bar if I 
T is present. 
Getmenuinflater (). Inflate (R.menu.main, menu); 
return true; @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long//as your specify a parent activity in 
androidmanifest.xml. int id = item.getitemid (); 
if (id = = r.id.action_settings) {return true; 
return super.onoptionsitemselected (item); }

In the code above:

1, we established the Mainactivity class needs to inherit from the activity

2, the need to overwrite the OnCreate method, and through the Setcontentview method to load the corresponding layout (layout) file

3, through the Findviewbyid method to find the appropriate control (in the layout layout file defined by the control) and binding a click event (Java in the listener implementation, in C # through the delegate implementation)

4, can pass the data through the intent object and jump to other activity

5, Oncreateoptionsmenu and onoptionsitemselected are the ways to add and select menu items.

Here are the contents of the four activity:

First type:

public class ButtonActivity1 extends Activity { 
button button; 
@Override 
protected void onCreate (Bundle savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (r.layout.activity_button1); 
Button = (button) Findviewbyid (R.ID.BTN1); 
Button.setonclicklistener (New MyListener ()); 
} 
public class MyListener implements Onclicklistener { 
@Override public 
void OnClick (View v) { 
Toast.maketext (Buttonactivity1.this, "This is the first form of the event, the inner class defines the event", "the". Show ();}} 

The second type:

public class ButtonActivity2 extends Activity { 
button button; 
@Override 
protected void onCreate (Bundle savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (r.layout.activity_button2); 
Button = (button) Findviewbyid (R.ID.BTN1); 
Button.setonclicklistener (New View.onclicklistener () { 
@Override public 
void OnClick (View v) { 
Toast.maketext (Buttonactivity2.this, "This is the second type of event, the form of anonymous inner class",). Show ();}} 

The third type:

public class ButtonActivity3 extends activity implements Onclicklistener { 
button button; 
@Override 
protected void onCreate (Bundle savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_button3); 
button= (Button) Findviewbyid (R.ID.BTN1); 
Button.setonclicklistener (this); 
} 
@Override public 
void OnClick (View v) { 
switch (V.getid ()) {case 
r.id.btn1: 
toast.maketext ( Buttonactivity3.this, 
"This is the third way of writing the event, directly implement the Onclicklistener interface of the OnClick method,". Show (); 
break; 
Default: 
toast.maketext (Buttonactivity3.this, "no Trigger", "Watts"). Show (); 
break; 
}}} 

The fourth kind:

The Btnclickevent method needs to be specified in the layout layout file XML.

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 
xmlns:tools= "http:// Schemas.android.com/tools " 
android:layout_width=" match_parent " 
android:layout_height=" Match_parent " 
android:orientation= "vertical" 
tools:context= "com.example.test.Button4Activity" > 
<button 
android:id= "@+id/btn4" 
android:layout_width= "match_parent" 
android:layout_height= "Wrap_content" 
android:text= "Click Me" 
android:onclick= "btnclickevent"/> 
</LinearLayout> Public 
class ButtonActivity4 extends Activity { 
@Override 
protected void onCreate (Bundle savedinstancestate) { 
Super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_button4); 
} 
public void Btnclickevent (View v) { 
toast.maketext (buttonactivity4.this, "This is the fourth way of writing the event, Bind click events directly to the button label of the layout file. Show (); 

The 3rd method in the above 4 ways is a little more. When more than one button in an activity needs to trigger the Click event, it is easier to manage and maintain the button event code through the 3rd way.

Layout is a very important piece of content, I will be in the following blog to explain, here a brief mention.

We use LinearLayout (linear layout, other relative layout, absolute layout, etc.), set the Android:orientation property value to Vertical (vertical), and then start to display the control from the top down.

The other 3 layout files, like this one, are just a single button.

The Activity_main.xml configuration is as follows (4 buttons are simply placed):

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:o" rientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen Activity_horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen 
Activity_vertical_margin "tools:context=" com.example.test.MainActivity "> <button android:id=" @+id/btn1 " 
Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "@string/clickme1"/> <button android:id= "@+id/btn2" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android : text= "@string/clickme2"/> <button android:id= "@+id/btn3" android:layout_width= "Match_parent" android:layout _height= "Wrap_content" android:text= "@string/clickme3"/> <button android:id= "@+id/btn4" andRoid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "@string/clickme4" Android:o nclick= "Btnclickevent"/> </LinearLayout>

The final, more important step, is to configure the registration activity in the Androidmanifest.xml file, complete with the following configuration:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.example.test "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:mins dkversion= "android:targetsdkversion=" "/> <uses-permission android:name=" Android.permission.CALL_PHONE "/> <uses-permission android:name=" Android.permission.INTERNET "/> <application" 
True "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </ intent-filter> </activity> <activity android:name= ". ButtonActivity1 "android:label=" @string/button1 "/> <activity android:name=". ButtonActivity2 "Android:lAbel= "@string/button2"/> <activity android:name= ". ButtonActivity3 "android:label=" @string/button3 "/> <activity android:name=". ButtonActivity4 "android:label=" @string/button4 "/> </application> </manifest>

There's a place that needs to be noticed.

<action android:name= "Android.intent.action.MAIN"/>

Set Mainactivity as the primary activity, which is the first activity that is displayed at startup.

The following multiple activity needs to be registered in the manifest file so that the activity can be found in the program.

Contents of strings.xml File configuration:

<?xml version= "1.0" encoding= "Utf-8"?> 
<resources> 
<string name= "App_name" >test</ string> 
<string name= "Hello_world" >hello "world!</string>" <string name= 
"Action_settings" >Settings</string> 
<string name= "clickMe1" > button event 1</string> <string name= 
"ClickMe2 "> button event 2</string> 
<string name=" clickMe3 > button event 3</string> <string name= 
"ClickMe4" > button Event 4</string> 
<string name= "Button1" > button 1</string> <string name= 
"Button2" > Button 2</string> 
<string name= "Button3" > button 3</string> <string name= 
"button4" > button 4 </string> 
</resources>

Of course, you can also write directly in the layout file, but this is more conducive to maintenance, which is the recommended method of Android development.

The above is a small set to introduce the Android Activity Jump button event four kinds of writing, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.