Android Beginner Tutorial Activity small case (calculator multiplication)

Source: Internet
Author: User
Tags gettext

First set up two layout files, one layout file to input data, get addition operations, another layout file to display the final result. Activity1 starts the Activity2 and passes the calculated result value to Activity2.

Main.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    ><edittext android:id=" @+id/factorone "    android:layout_width=" Fill_parent     " android:layout_height= "Wrap_content"/><textview  android:id= "@+id/symbol"    android:layout_width= " Fill_parent "     android:layout_height=" wrap_content "     /><edittext android:id=" @+id/factortwo    " Android:layout_width= "Fill_parent"     android:layout_height= "wrap_content"/><buttonandroid:id= "@+id/ Calculate "    android:layout_width=" fill_parent "     android:layout_height=" Wrap_content "/></ Linearlayout>

Page Show:

Result.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    >    <textview    android:id=" @+id/result "    android:layout_width=" Fill_parent "    android:layout_height= "Wrap_content"    /></linearlayout>

Interface display:

ACTIVITY03 Activities:


Package Mars.activity03;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import android.widget.edittext;import android.widget.textview;//1. In Activity03, To declare four control//2. To set the displayed value for two of these controls//3. Create a listener class that listens for button pressed actions//4. Binds the Listener class object to the button object public class Activity03 extends Activity {/** Called when the activity is first created.    */private EditText factorone;p rivate EditText factortwo;private TextView symbol;private Button calculate;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        Gets the object that represents the control according to the control's id factorone = (EditText) Findviewbyid (R.id.factorone);        Factortwo = (EditText) Findviewbyid (r.id.factortwo);        Symbol = (TextView) Findviewbyid (R.id.symbol); calculate = (Button) Findviewbyid (R.ID.CALCULate);        Set the displayed value for symbol and calculate/symbol.settext ("multiply");//Calculate.settext ("calculation"); Symbol.settext (R.string.symbol);//This is referred to in the string file by way of reference.        Ensure the business logic, views, reference resources separate calculate.settext (r.string.calculate);    Binds the Listener object to the button object above Calculate.setonclicklistener (new Calculatelistener ()); }//When the customer clicks the menu button, the method is called @Overridepublic boolean oncreateoptionsmenu (Menu menu) {menu.add (0, 1, 1, r.string.exit    );    Menu.add (0,2,2,r.string.about); return Super.oncreateoptionsmenu (menu);} This method is called when the customer taps an option in the menu @overridepublic boolean onoptionsitemselected (MenuItem item) {if (item.getitemid () = = 1) { Finish ();} return super.onoptionsitemselected (item);} Class Calculatelistener implements onclicklistener{@Overridepublic void OnClick (View v) {//Gets the value of two EditText control string Factoronestr = Factorone.gettext (). toString (); String factortwostr = Factortwo.gettext (). toString ();//Store These two values in the intent object Intent Intent = new intent (); Intent.putextra ("One", factoronestr); intent.putExtra ("Both", factortwostr); Intent.setclass (Activity03.this, resultactivity.class);//    Use this intent object to start ResultActivityActivity03.this.startActivity (intent);} }}

Resultactivity:

Package Mars.activity03;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.widget.textview;//1. Accept the value passed from the Activity03//2. Calculates the product//3 of two values. Displays the calculated results on the activity public class resultactivity Extends Activity{private TextView resultview; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.result); ResultView = ( TextView) Findviewbyid (R.id.result);//Get the value of Intent object Intent Intent = Getintent (); String factoronestr = Intent.getstringextra ("one"); String factortwostr = Intent.getstringextra ("n"), int factoroneint = Integer.parseint (factoronestr); int Factortwoint = Integer.parseint (FACTORTWOSTR);//Calculate the product of two values int result = Factoroneint * Factortwoint;resultview.settext (Result + "");}}

String.xml:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "Hello" >hello world, activity03!</string>    <string name= "app_name" >activity03</string>    <string name= " ResultLabel ">result</string>    <string name=" symbol "> Times </string>    <string name=" Calculate "> Calculation </string>    <string name=" Exit "> Exit </string>    <string name=" about "> About </string></resources>

Finally, take a look at the configuration file: activities are registered and set Activity03 as the primary activity

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "      package=" mars.activity03 "      android:versioncode=" 1 "      android:versionname=" 1.0 ">    < Application android:icon= "@drawable/icon" android:label= "@string/app_name" >        <activity android:name= ". Activity03 "                  android:label=" @string/app_name ">            <intent-filter>                <action android:name=" Android.intent.action.MAIN "/>                <category android:name=" Android.intent.category.LAUNCHER "/>            < /intent-filter>        </activity><activity android:name= ". Resultactivity "android:label=" @string/resultlabel "/><!--here make Resultactivity title bar show Result-->    </ Application>    

Operation Result:


Android Beginner Tutorial Activity small case (calculator multiplication)

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