Building Your First APP

Source: Internet
Author: User

Http://www.cnblogs.com/gcg0036/p/4321246.html

Building a simple User Interface:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" >     <EditTextAndroid:id= "@+id/edit_message"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:hint= "@string/edit_message" />     <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button_send" /> </LinearLayout>

A description of some properties:

Android:id

This defines the unique identifier of the view, which you can refer to in the code of the program,
only when you define a resource ID for the first time. This is to tell the SDK that this resource ID needs to be created. After the application is compiled, the SDK can use the ID value directly, and Edit_message is creating a new identifier in the project Gen/r.java file, which is associated with EditText. Once the resource ID is created, the other resource will no longer need the + number if it references the ID. This is the only attribute that needs a + sign

Android:layout_width and Android:layout_height

for width and height it is not recommended to specify a specific size, and when specified with "Wrap_content", this view is only a space that occupies the size of the content. If you use "Fill_parent", then EditText will be filled with the entire screen, as it will fit the size of the parent layout

Android:hint

When the text box is empty, the string is displayed by default. The resource referenced for the value of the string "@string/edit_message" should be defined in a separate file instead of using the string directly. Because you are using a value that is a resource that exists, you do not need to use the + sign.

This button does not need to specify the properties of the Android:id because it is not referenced in the activity code.

It's better for a text box to be stained with the entire width. LinearLayout use the property of the weight to achieve this, you can set it using the Android:layout_weight property.

You can specify the size of the values in the circle based on the space occupied by each part, and its total number is determined by the same level of components. It is similar to the composition of the drink: "Two vodka, one coffee liqueur", which means that the vodka in the wine accounted for two-thirds. For example, if you set a view with a weight of 2 and another view with a weight of 1, then the total is 3, when the first view occupies 2/3 of the space and the second occupies 1/3 of the space. If you add a third view and the weight is set to 1, then the first view will occupy 1/2 of the space, and the rest is split equally between the other two view.

For all view default weights is 0, if you only set a view with a weight greater than 0, then this view will occupy all the remaining space except the space occupied by the other view itself. The weight of the edittext is therefore set to 1 so that it can occupy all the space except for the button.

In order to achieve a more efficient layout, when you set weights, you should set the width of the edittext to 0

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "App_name">Demoandroid</string>    <stringname= "Hello_world">Hello world!</string>    <stringname= "Action_settings">Settings</string>    <stringname= "Edit_message">Enter a message</string>    <stringname= "Button_send">Send</string></Resources>

Note : The string resource uses the same name as the ID (edit_message). However, references to resources are type-sensitive (such as ID and string), so using the same name does not cause a conflict.

Starting another activity:respond to the Send button-response send button
< Button      Android:layout_width = "Wrap_content"     android:layout_height= "Wrap_content"    android:text= "@string/button_ Send "    android:onclick=" SendMessage "/>
/***  /publicvoid  sendMessage (view view)    {new Intent (This, displaymessageactivity.  Class);     = (EditText) Findviewbyid (r.id.edit_message);     = Edittext.gettext (). toString ();    Intent.putextra (Extra_message, MESSAGE);    StartActivity (intent);  }

Note that in order for the system to match this method (the SendMessage method you just added in myfirstactivity) with the method name provided in the Android:onclick attribute, their names must be the same, especially if the method must meet the following criteria:

    • of public
    • No return value
    • There is a unique view parameter (this view is the view that will be clicked)

There are two parameters in this intent constructor: The first argument is the context (the one that can be used because the current activity (myfirstactivity) is the subclass of the context) The system needs to pass the class object of the intent application component (in this case, the activity should be started)

Intent can carry a collection of various data types as key-value attached pairs. The PutExtra () method takes the key name as the first argument and the value as the second argument.

It is good practice to use the application package name as a prefix to define intent keys. If your application interacts with other applications, you can ensure that the intent key is unique.

 Public Final Static String extra_message = "Com.example.myfirstapp.MESSAGE";

To start an activity, you only need to call the StartActivity () method and then pass in your intent (intent) the system will instantiate the activity specified in intent after receiving your request.

Fragments break down the functionality and user interface of the application into reusable modules

All subclasses of the activity must implement the OnCreate () method. This mode is called when the new instance of the activity is created, and the active layout must be defined with Setcontentview (), and the active component should be initially set up.

<ActivityAndroid:name=". Displaymessageactivity "Android:label= "@string/title_activity_display_message"Android:parentactivityname=". Mainactivity " >            <Meta-dataAndroid:name= "Android.support.PARENT_ACTIVITY"Android:value= "Com.amIurs.MainActivity" />        </Activity>

The Android:parentactivityname property declares the active parent class activity name in the application logical hierarchy.

The system uses this value to implement the default navigation actions, such as Navigationon Android 4.1 (API level 16) or later.

Use the Support library and add the <meta-data> element as shown below to provide the same functionality for older versions of Android

Receive the intent-get Intent (Intent)

Every activity that is called by intent, regardless of where the user navigates it, you can get intent and intent contained data through the Getintent () method in the activated activity

 Public classDisplaymessageactivityextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //get information from intentIntent Intent =getintent (); String message=Intent.getstringextra (mainactivity.extra_message); //Create a TextView objectTextView TextView =NewTextView ( This); Textview.settextsize (40);    Textview.settext (message);    Setcontentview (TextView); }

Sdf

Building Your First APP

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.