Create a new activity with the button to open it, and then close it by the button

Source: Internet
Author: User
Tags one more line

You first need to create an activity to open and close, first create a new class Activity1 in the package of the current project under SCR,

and choose to inherit from the activity class, as shown in:

After configuring the Androidmainifest.xml file, the configuration is followed by one more line in the Androidmainfest.xml code

<activity android:name= "Activity1" ></activity>

After it is created, it overrides a OnCreate () method and binds to Activity1.

Package Cn.hx.helloworld;import Android.app.activity;import Android.os.bundle;import android.view.view;import android.widget.button;//Create a new class Activity1 let it inherit from activity//to run this class is to configure to the Androidmainifest.xml file at the public class Activity1 Extends Activity {private Button btn_closeaty1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity1);//Bind to Activity1}}

To the previous Activity_main.xml pull a button, the ID is changed to BtnStartActivity1, in the Mainactivity.java declaration of a button private buttons btnStartAty1;

Then through Findviewbyid find BtnStartActivity1, and then set an event listener to the button, click the button to perform open Activity1 operation, such as:

1  Public classMainactivityextendsactionbaractivity {2 3     PrivateButton btnStartAty1;4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8         9btnstartaty1=(Button) Findviewbyid (r.id.btnstartaty1);Ten         //Error here type Mismatch:cannot convert from View to Button One         //because Findviewbyid returns the view type, it must be forced to replace the button type ABtnstartaty1.setonclicklistener (NewView.onclicklistener () { -         //set an event listener for button buttons - @Override the              Public voidOnClick (View v) { -                 //TODO auto-generated Method Stub -Intent i=NewIntent (mainactivity. This, Activity1.class); - startactivity (i); +             } -         }); +}

In the same vein, you want to close the activity. To add a close button to the Activity.xml, click Close, and complete the button declaration in Acticity1.java, the event is monitored

 PackageCn.hx.HelloWorld;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;//Create a new class Activity1 let it inherit from activity//To run this class, you need to configure it to the Androidmainifest.xml file . Public classActivity1extendsActivity {PrivateButton btn_closeaty1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity1); //bind to Activity1btn_closeaty1=(Button) Findviewbyid (r.id.btn_closeaty1); Btn_closeaty1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stubfinish (); //Finish () is a way to perform a close activity operation            }        }); }        }

Create a new activity with the button to open it, and then close it by the button

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.