Page jumps (with return parameters)---------Android

Source: Internet
Author: User

On the internet to see a lot of button click events,, are configured to monitor what ..... I'm not using a configuration listener.

is similar to the WinForm event, first there are two interfaces to add the OnClick event to the button in the interface:

This is the first button in the main interface
<
Relativelayoutxmlns: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:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "button with return value"Android:id= "@+id/button"Android:layout_alignparenttop= "true"Android:layout_alignparentstart= "true"Android:onclick= "Button1_Click"/></Relativelayout>
This is the first method in the main interface
Public voidbutton1_click (View v) {//this is equivalent to a messenger, this is the current page,//The second parameter is the page to jump to//here the intent need to jump page, so need parameters ... When you do not need to jump the page, the parameters are not required. This class can also be used for other purposesIntent Inten =NewIntent ( This, Main2activity.class); //The second parameter is just a flag of the request, and if no return value is required, this method is not required.//It is possible that this page will have many other requests, and all requests will be given by the following Onactivityresult method to get the value returned by that page.//so this means that when the return value is obtained, it is used to judge to correspond to the correct requestStartactivityforresult (Inten, 1); } //here The Requestcode parameter, is the above set 1, when the jump page returns, through this to Judge//ResultCode, this parameter is specified in the jump page, it is also an int type of flag//The third parameter contains the returned value//This method is not required if you do not need the return value of the page you are jumping to.@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = = 1 && ResultCode = = 2)//the two flags mentioned earlier show the effect here { NewAlertdialog.builder ( This). Settitle (Data.getstringextra ("Data"))//Keys for key-value pairs returned on the jump page. Show (); } }

The interface and background code of the jump page

<Relativelayoutxmlns: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:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context= "Com.example.administrator.myapplication.Main2Activity">    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Returns a value to the previous page"Android:id= "@+id/button3"Android:layout_alignparenttop= "true"Android:layout_alignparentstart= "true"Android:onclick= "Button1_Click"/></Relativelayout>
 Public void button1_click (View v)    {        new  Intent ();        Inte.putextra ("Data", "Hello"); // here is the key value pair, "Data" is the key        Setresult (2,inte); // The return value, 2, is the returned flag and returns        Finish (); // destroy current page    }

So far, the return and no-return page jumps for Android are over.

Page jumps (with return parameters)---------Android

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.