English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Four writing methods of activity jump button event in Android

Specific implementation code:

public class MainActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
// method1. Uses a class that implements the OnClickListener interface 
((Button) findViewById(R.id.btn1) 
.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
Intent intent = new Intent(MainActivity.this, 
ButtonActivity1); 
startActivity(intent); 
} 
}); 
// method2. Uses anonymous inner class 
((Button) findViewById(R.id.btn2) 
.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
Intent intent = new Intent(MainActivity.this, 
ButtonActivity2); 
startActivity(intent); 
} 
}); 
// method3. 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); 
MainActivity.this.startActivity(intent); 
} 
}); 
// method4. The label directly indicates the triggering event 
((Button) findViewById(R.id.btn4) 
.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
Intent intent = new Intent(MainActivity.this, 
ButtonActivity4); 
MainActivity.this.startActivity(intent); 
} 
}); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it 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 will 
// automatically handle clicks on the Home/Up button, so long 
// as you 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 above code:

1The MainActivity class we have created needs to inherit from Activity

2and need to override the onCreate method, and load the corresponding layout file through setContentView method

3and find the corresponding control (defined in the layout file) through findViewById method and bind a Click event (implemented through listeners in Java, and through delegates in C#)

4and data can be passed through an Intent object and jump to other Activities

5and onCreateOptionsMenu and onOptionsItemSelected are the methods added when a menu item is selected.

The following are the contents of the four activities respectively:

The 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 way to write the event, defined by an inner class", 2000).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 way to write the event, in the form of an anonymous inner class", 2000).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 to write the event, directly implement the OnClick method of OnClickListener interface", 2000).show(); 
break; 
default: 
Toast.makeText(ButtonActivity3.this, "no trigger", 2000).show(); 
break; 
} 
} 
}

The fourth type:

The btnClickEvent method needs to be specified in the 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.Button"4Activity"> 
<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 writing method of the event, directly binding Click event on the Button tag of the layout file", 2000).show(); 
} 
}

above4writing method, the3method is used more frequently. When there are multiple buttons in an activity that need to trigger the click event, through the3writing method is easier to manage and maintain button event code.

Layout is an important content, I will explain it in the following blog, and just mention it briefly here.

We use LinearLayout (linear layout, others include relative layout, absolute layout, etc.), and set the Android:orientation attribute value to vertical (vertical), starting from top to bottom to display controls.

Others3layout files have the same content as this, all of which only place a button.

The configuration of activity_main.xml is as follows (simply placed4A button):

<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" 
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:onClick="btnClickEvent"/> 
</LinearLayout>

The most important step is to configure the registered Activity in the AndroidManifest.xml file, the complete configuration is as follows:

<&63;xml version="1.0" encoding="utf-8"&63;> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.test" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk 
android:minSdkVersion="14" 
android:targetSdkVersion="14" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<application 
android:allowBackup="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 is a point that needs attention here,

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

Set MainActivity as the "main activity", that is, the Activity that is displayed first when the program starts.

The following multiple activities need to be registered in the "manifest file" so that these Activities can be found in the program.

The content configured in the strings.xml file is as follows:

<&63;xml version="1.0" encoding="utf-8"&63;> 
<resources 
<string name="app_name">test<//string> 
<string name="hello_world">Hello world!<//string> 
<string name="action_settings">Settings<//string> 
<string name="clickMe1">button event1</string> 
<string name="clickMe2">button event2</string> 
<string name="clickMe3">button event3</string> 
<string name="clickMe4">button event4</string> 
<string name="button1">button1</string> 
<string name="button2">button2</string> 
<string name="button3">button3</string> 
<string name="button4">button4</string> 
</resources>

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

The four writing methods of activity jump button events in Android introduced by the editor are as follows, hoping to be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Thank you very much for your support of the Yelling tutorial website!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You may also like