Android Development Study Notes (4) activity Switching

Source: Internet
Author: User

This application mainly refers to the tutorial on the EOE development website, which is to switch from one activity to another. Design intent to intent.

The implementation of the main interface layout is very simple. There is only one edittext and one button.

<LinearLayout xmlns:android=""    xmlns:tools=""    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <EditText        android:id="@+id/edit"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:hint="@string/Type"        />    <Button        android:id="@+id/Btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/Send"         android:onClick="sendMessage"        /></LinearLayout>

However, there are two points that I have never touched before.

First, it is

Android: layout_width = "0dp"
Android: layout_weight = "1"

The two indicate that the edittext width is set to 0 first, and then all spaces except the button are filled.

Android: layout_weight is just like dividing the land. If the second button also has an Android: layout_weight = "1", the two tags are evenly divided into one row. If there is a third tag such as <textview> and there is also an Android: layout_weight = "1", the three tags are split into one row.

Second, yes

Android: onclick = "sendmessage"

Previously I used Java to write and bound a button to the listener. here we can use XML directly, which is very simple.

Because multiple activities are used, you need to change the content of androidmanifest. xml.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android=""    package="com.xujin.send"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="16" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity  android:name="com.xujin.send.DisplayMessageActivity"/>        <activity            android:name="com.xujin.send.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

Note that the activity tag contains an activity, that is, an activity corresponds to a tag. When you click an icon to enter an application,
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>


Then the most important acitvity

Mainactivity. Java

package com.xujin.send;import;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity {public final static String EXTRA_MESSAGE="com.xujin.send.MESSAGE";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void sendMessage(View view){Intent intent = new Intent(this, DisplayMessageActivity.class);EditText editText = (EditText)findViewById(;String message = editText.getText().toString();intent.putExtra(EXTRA_MESSAGE, message);if(message.length()>0)startActivity(intent);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(, menu);return true;}}

This acitvity has three functions, one of which is the hook function, oncreat () and oncerateoptionsmenu. By default, I have not made many changes. In addition, the sendmessage function is added. The task of this function is to send the content that the user inputs to edittext to another activity. Then start the second activity.

Displaymessageactivity. Java

Package COM. xujin. send; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textview; public class displaymessageactivity extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // retrieves information from intent = getintent (); string message = intent. getstringextra (mainactivity. extra_message); // create textview object textview = new textview (this); textview. settextsize (40); textview. settext (Message); setcontentview (textview );}}

Result demonstration:

Related Article

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: 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.