Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to switch between two interfaces with Android studio

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article editor for you detailed introduction "Android studio how to achieve the switch between the two interfaces", the content is detailed, the steps are clear, the details are handled properly, I hope this "Android studio how to achieve the switch between the two interfaces" article can help you solve doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

There are two ways to realize the switching between the two interfaces, the first is the switching between xml, and the other is the switching between two Activity.

Example: to achieve the function shown in the figure in two different ways, click button to switch from the first page to the second page.

In scheme 1, the switching between xml is implemented by implementing anonymous inner classes, which is suitable for only one-time use of the listener. After the code block is run, the listener no longer exists.

Step1: create a new project File-New-New Project

Step2: then next all the way, and finally finish.

Step3: after the new project is completed, expand the first app-java--MainActivity in the left column. Write Java programs here

Package com.example.interaction;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / first call activity_main Button ok= (Button) this.findViewById (R.id.btn) in xml; / / implement anonymous inner class? Ok.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {setContentView (R.layout.twolayout);});}}

Step4: expand app-res-layout-activity_main.xml in the left column, and write the first xml program here.

Step5: add a twolayout to the existing project, click File-New-XML-Layout XML File, and name it twoLayout

Step6: writing programs

Scenario 2: switching between two activity, this method inherits the listener interface by defining an inner class in the Activity

Step1: create a new project File-New-New Project, which is the same as in solution 1.

Step2: then next all the way, and finally finish, which is the same as in scenario 1.

Step3: create a new .java file, File-New-Java Class

Name it TwoActivity and click OK.

Step4: writing programs in MainActivity

Package com.example.interaction;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / call activity_main in xml. Button ok = (Button) this.findViewById (R.id.btn); ok.setOnClickListener (new ButtonListener ());} private class ButtonListener implements View.OnClickListener {@ Override public void onClick (View v) {Intent intent=new Intent (MainActivity.this,TwoActivity.class); / / set switch corresponding to activity startActivity (intent); / / start switching}

Step5: writing programs in TwoActivity

Package com.example.interaction;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class TwoActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.twolayout); / / call twolayout}}

Step6: click app-manifests in the left column, add a line of code, add the location as shown in the figure, must be inside, outside.

After reading this, the article "how to switch between the two interfaces in Android studio" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report