Android hands-on tutorial The first of the simplest calculator _android

Source: Internet
Author: User
Tags gettext

Starting today, this column continues to update the Android Simple Combat blog post. Unlike previous columns, this column has only instances. Each example as far as possible according to the knowledge point corresponding to the content of the chapter to write, Step-by-step. Some instances may have duplicate articles with another column.

The first simple case to start this column:

First set two layout files, one layout file to input data, get addition operation; Another layout file to display the final result. Activity1 starts the Activity2 and passes the computed 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"  
  Wrap_content "  
  /> 
<button 
  android:id=" @+id/calculate "android:layout_width=" Fill_ 
  Parent "  
  android:layout_height=" wrap_content "  
  /> 
</LinearLayout> 

Page Show:

Result.xml

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, declare four control//2. To set the displayed value for two of these controls//3. Creates a listener class that listens to the action//4 the button is pressed on. The object of the listener class, bound on the button object public class Activity03 ex 
 Tends activity {/** called ' when the ' activity is ' is a-created private edittext factorone; 
 Private EditText Factortwo; 
 private TextView symbol; 
 Private Button calculate; 
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.main); 
  Gets the object representing the control Factorone = (edittext) Findviewbyid (R.id.factorone), based on the ID of the control; 
  Factortwo = (edittext) Findviewbyid (r.id.factortwo); 
  Symbol = (TextView) Findviewbyid (R.id.symbol); calculate = (Button) findviewbYid (r.id.calculate); 
Set the displayed value//Symbol.settext ("times") for symbol and calculate; 
  Calculate.settext ("calculation"); Symbol.settext (R.string.symbol);//This is referenced in the string file by reference. 
  Ensure the business logic, view, reference resources separate calculate.settext (r.string.calculate); 
 Binds the listener's object to the button object Calculate.setonclicklistener (new Calculatelistener ()); //When the customer clicks the Menu button, the method is invoked @Override public boolean Oncreateoptionsmenu (Menu menu) {menu.add (0, 1, 1, R.string.exi 
  T); 
  Menu.add (0,2,2,r.string.about); 
 return Super.oncreateoptionsmenu (menu); //When the customer clicks on one of the options in the menu, the method is invoked @Override public boolean onoptionsitemselected (MenuItem item) {if Item.getitemid () = 
  = 1) {finish (); 
 return super.onoptionsitemselected (item); Class Calculatelistener implements onclicklistener{@Override public void OnClick (View v) {//Get two EditText 
   The value of the 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 ("Two", factortwostr); 
   Intent.setclass (Activity03.this, Resultactivity.class); 
  Use this intent object to start the resultactivity Activity03.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 results of the calculation on the activity public 
class Resultactivity extends activity{ 
 private TextView Resultview; 
 @Override 
 protected void onCreate (Bundle savedinstancestate) { 
  //TODO auto-generated method stub 
  Super.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 ("two"); 
  int factoroneint = Integer.parseint (FACTORONESTR); 
  int factortwoint = Integer.parseint (FACTORTWOSTR); 
  A product int of two values is calculated 
  = 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> 

Finally, look at the profile: the activity is registered and the ACTIVITY03 is set up 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/>< the resultactivity title bar is shown here result--> </ 
 application> 
 <uses-sdk android:minsdkversion= "4"/> 
 

Results:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.