Android's implementation layout jump

Source: Internet
Author: User

1. A jump between two. xml files:

Achieve the effect: when the button is clicked, you can jump to another page

A. Method One: Use the Setcontentview () method with the function call to implement:

public class Mainactivity extends Appcompatactivity {
Private Button bt1;
Private Button BT2;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
bt1= (Button) Findviewbyid (R.id.button1);
Bt1.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
JumpToLayout2 ();
}
});
}
public void JumpToLayout2 () {
Setcontentview (r.layout.mylayout);
Bt2= (Button) Findviewbyid (R.id.button2);
Bt2.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
JUMPTOLAYOUT1 ();
}
});
}
public void JumpToLayout1 () {
Setcontentview (r.layout.activity_main);
bt1= (Button) Findviewbyid (R.id.button1);
Bt1.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
JumpToLayout2 ();
}
});
}
}
B. Method two: Through an activity binding an XML file, and then implement the activity of the jump to achieve the two XML file jump:
It is important to note that because there are two activity to be used here, it is important to declare in mainfest that the newly created activity:
 <activity android:name= ". Mainactivity " 
<intent-filter>
<action android:name=" Android.intent.action.MAIN "/> ;

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</a Ctivity>
<activity android:name= ". Mylayout";
< Intent-filter>
<action android:name= "Android.intent.action.MAIN"/>

<category Androi D:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

And then through the intent way to achieve the jump:
for mainactivity page:
 public class Mainactivity extends Appcompatactivity {
private Button bt;
@Override
protected void onCreate (final Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
//Bind layout file Activity_main.xml file
setcontentview (R.layout.activity_main);
bt= (Button) Findviewbyid (r.id.button1);
Bt.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {//For the following two parameters, respectively, represents this page and the parameters of the jump page, but note that this page is used this, to jump to the page is the class
inte NT Intent=new Intent (Mainactivity.this,mylayout.class);
startactivity (intent);
}
});
}
}
for mylayout page:
public class Mylayout extends Activity {
Private Button BT;
@Override
protected void OnCreate (final Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Binding mylayout.xml Files
Setcontentview (r.layout.mylayout);
bt= (Button) Findviewbyid (R.id.button2);
Bt.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent intent=new Intent (mylayout.this,mainactivity.class);
StartActivity (Intent);
}
});
}
}

Android's implementation layout jump

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.