start Android Studio to create a new project
Public void sendMessage (view view) { Intent intent=new Intent (this,dispalymessageactivity.class); EditText Text = (EditText) Findviewbyid (r.id.edit_message); String Message=text.gettext (). toString (); Intent.putextra (extra_message,message); StartActivity (intent); }
Add a button response function sendMessage The response function that is bound in the view must be a function that exposes no return value with a view parameter to get the data on the view by view injection
Intent Object Common terms start a new activity
Findviewbyid gets to the control object through the ID in the view this is similar to JS getting input
Get to input box text content via GetText
Intent.putextra saving data to the intent object here Extra_message is a constant defined in the current class
StartActivity language Start an activity
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"android:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"App:layout_behavior= "@string/appbar_scrolling_view_behavior"Tools:showin= "@layout/activity_my"Android:id= "@+id/content"> <EditTextAndroid:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:hint= "@string/edit_message"Android:id= "@+id/edit_message"/> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button_message"Android:onclick= "SendMessage"/></LinearLayout><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"android:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"App:layout_behavior= "@string/appbar_scrolling_view_behavior"Tools:showin= "@layout/activity_my"Android:id= "@+id/content"><EditTextAndroid:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:hint= "@string/edit_message"Android:id= "@+id/edit_message"/><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button_message"Android:onclick= "SendMessage"/></LinearLayout>
Start effect then click Send To jump to a new activity
Add an Activity
Add a class under MyActivity and inherit appcompatactivity
Overriding the OnCreate method
protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.display_message); Getsupportactionbar (). Setdisplayhomeasupenabled (true); Intent Intent = Getintent (); String message = Intent.getstringextra (myactivity.extra_message); TextView TextView = new TextView (this); Textview.settextsize (+); Textview.settext (message); Textview.setwidth (+); Relativelayout layout = (relativelayout) Findviewbyid (r.id.content); Layout.addview (TextView); }
This gets the intent object through Getintent
Get the message data saved in
Create a TextView object
Get to view by ID and add a textview space to the view dynamically
<? XML version= "1.0" encoding= "Utf-8" ?> < xmlns:android= "http://schemas.android.com/apk/res/android" android:id = "@+id/content" android:layout_width= "Match_parent" android:layout_height= " Match_parent "> </relativelayout>
Register the view in the Androidmanifest (list)
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.xuelei.myapp"> <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:roundicon= "@mipmap/ic_launcher_round"Android:supportsrtl= "true"Android:theme= "@style/apptheme"> <ActivityAndroid:name=". MyActivity "Android:label= "@string/app_name"Android:theme= "@style/apptheme.noactionbar"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ActivityAndroid:name= "Com.example.xuelei.myapp.DispalyMessageActivity"Android:label= "@string/title_activity_display_message"Android:parentactivityname= "Com.example.xuelei.myapp.MyActivity" > <Meta-dataAndroid:name= "Android.support.PARENT_ACTIVITY"Android:value= "Com.example.xuelei.myapp.MyActivity"/> </Activity> </Application></Manifest>
Add an activity name the fully qualified name of the class, the package name + the name
Lable set the lable name for the activity display
PARENTACTIVITYNAME Specifies the parent window object Package name + class name
Meta-data The name value is an additional arbitrary data that can be supplied to the parent component. A component element can contain any number of meta-data child elements. All of their values are collected in the bundle object and made available as the Packageiteminfo.metadata field for the component.
The general value can be specified by the Value property, but if you want to specify a resource ID as a value, use the Resource property instead. Used here to get myactivity this object
This completes an activity jump.
Get started with Android create a project from start activity in response to a button event start another activity and pass parameters