The application value is passed.

Source: Internet
Author: User

1. Layout under Layouts

Activity_main.xml

<?xml version="1.0"encoding="Utf-8"? ><relativelayout 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: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.app.androidvaluespass.MainActivity"> <Button Android:id="@+id/btnsubmit"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Value Passing"/></relativelayout>

Values.xml for receiving values

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"> <EditText android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:id="@+id/msg"/></linearlayout>

2. Class and Startup items

Mainactivity

Package Com.example.app.androidvaluespass;import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.Button; Public classMainactivity extends Activity {PrivateButton btn; PrivateMyApp MyApp; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); BTN= (Button) This. Findviewbyid (R.id.btnsubmit); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {myApp=(MYAPP) getapplication (); Myapp.setname ("nerf!");//the modified nameIntent Intent =NewIntent (mainactivity. This, Valuesactivity.class);            StartActivity (Intent);    }        }); }}

Define a application class MYAPP

Package Com.example.app.androidvaluespass;import android.app.Application;/** * Created by Honghong on 2016/6/18.*/ Public classMyApp extends Application { PublicString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override Public voidonCreate () {super.oncreate (); SetName ("Zhang San"); }}

Defines a class that receives a value

Package Com.example.app.androidvaluespass;import Android.app.activity;import android.os.bundle;import Android.widget.TextView;/** * Created by Honghong on 2016/6/18.*/ Public classValuesactivity extends Activity {PrivateMyApp MyApp; PrivateTextView EditText; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.values); EditText= (TextView) This. Findviewbyid (r.id.msg); MYAPP=(MYAPP) getapplication ();    Edittext.settext (Myapp.getname ()); }}

Finally, configure it in the Androidmanifest.xml file.

<?xml version="1.0"encoding="Utf-8"? ><manifest xmlns:android="http://schemas.android.com/apk/res/android" Package="Com.example.app.androidvaluespass"> <application android:name =". MyApp "//← Inherit the name of the application class, this must correspond. android:allowbackup="true"Android:icon="@mipmap/ic_launcher"Android:label="@string/app_name"Android:supportsrtl="true"Android:theme="@style/apptheme"> <activity android:name=". Mainactivity"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity><activity android:name= ". Valuesactivity "></activity>  //This class calls the application class Valuesactivity</application></manifest>

The application value is passed.

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.