Android realizes a simple multiplication code _android

Source: Internet
Author: User
Tags bind gettext

Development environment: android4.1.

Experiment function:
Enter 2 digits in the 2 multiplier inputs in the first interface, and press the result button to automatically skip to the second interface and display the result of entering 2 numbers. If you click on the System's menu button in the first interface, a menu will automatically pop up, the menu bar includes exit and about 2 buttons, click the Exit button, then quit the program.

Experiment Description:
1. The steps of the 1th activity are probably as follows:
Create a listener.
Creates a intent and enters the intent as a key-value pair for the 2 input digits that are fetched.
Connect the 1th activity and the 2nd activity.
Start Intent.
Bind the listener to the calculation result button.
2. The steps of the 2nd activity are probably as follows:
Create a listener.
Create a intent, and get the string from the key value pairs in the intent, and save it well.
Converts a string to a number and multiplies it.
The multiplication result is displayed in the TextView.
3. Adding menu items requires overriding the Oncreateoptionsmenu () function without adding the corresponding controls in the XML layout.
4. The onoptionsitemselected () function needs to be overridden when the corresponding menu is clicked.
5. When importing packages into Android Java files, be sure to distinguish between the case of the name.
6. In general, when displaying text in a Java program, it is best not to specify the text content in Java in order to internationalize the program, but to specify it in the corresponding XML file, and then call its resources in Java code. Because then we just need to change an XML file ( Of course, there are more than one language version of the XML file, and then different versions of the software invoke different XML.
7. The inner class is more common in Java, which is the embedded class in the class, which is more widely used in multiple threads. You can use the member variables, functions, and objects of an external class in an inner class.
8. The menu control in the Android control does not need to be given in XML, so rewrite some functions directly in Java source code.
9. Intent can not only transmit data in 2 actibity in the same program, but also data transfer in activity in different programs.
The syntax for converting strings to integers in Java is integer.parseint (string);
In 11.java operations, as long as 2 of them have one character type, the other is automatically converted to a string.

Experimental results:




The main part of the experiment Code and comments:
Mainactivity.java:

Copy Code code as follows:

Package com.example.factor;

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;

public class Mainactivity extends activity {

Private TextView factor;
Private EditText Factor1;
Private EditText Factor2;
Private Button result;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Factor = (TextView) Findviewbyid (r.id.factor);
Factor1 = (edittext) Findviewbyid (R.id.factor1);
Factor2 = (edittext) Findviewbyid (R.ID.FACTOR2);
result = (Button) Findviewbyid (R.id.result);
Factor.settext (R.string.factor);
Result.settext (R.string.result);
Bind a listener to this button
Result.setonclicklistener (New Onresultclicklistener ());
}

The function is actually a callback function, but this function is automatically called when the menu button is pressed in the system
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
To join before returning the code
Menu.add (0, 1, 1, r.string.exit);
Menu.add (0, 2, 2, r.string.about);
return Super.oncreateoptionsmenu (menu);
}

menu item Selection function override
@Override
public boolean onoptionsitemselected (MenuItem item) {
if (1 = item.getitemid ())
Finish ();
return super.onoptionsitemselected (item);
}


Create a listener
Class Onresultclicklistener implements onclicklistener{

public void OnClick (View v) {
Get 2 edit-input numbers
String factor1_str = Factor1.gettext (). toString ();
String factor2_str = Factor2.gettext (). toString ();
Establish the intent and pass the data in and activate the resultactivity
Intent Intent = new Intent ();
Intent.putextra ("Factor1", factor1_str);
Intent.putextra ("Factor2", factor2_str);
Intent.setclass (Mainactivity.this, Resultactivity.class);
Start Intent
MainActivity.this.startActivity (Intent);
}

}
}



Resultactivity.java:
Copy Code code as follows:

Package com.example.factor;

Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.widget.TextView;

public class Resultactivity extends activity {

Private TextView Result_dis;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_result);

Result_dis = (TextView) Findviewbyid (R.id.result_view);
Intent Intent = Getintent ();
String factor_str1 = Intent.getstringextra ("Factor1");
String factor_str2 = Intent.getstringextra ("Factor2");
syntax for converting strings into integers in Java
int factor_int1 = Integer.parseint (FACTOR_STR1);
int factor_int2 = Integer.parseint (FACTOR_STR2);
int result = Factor_int1 * FACTOR_INT2;
In Java operations, as long as 2 of them have one character type, the other is automatically converted to a string
Result_dis.settext (Result + "");

}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_result, menu);
return true;
}
}


Activity_main.xml:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<edittext
Android:id= "@+id/factor1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
/>
<textview
Android:id= "@+id/factor"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
/>
<edittext
Android:id= "@+id/factor2"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
/>
<button
Android:id= "@+id/result"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
/>

</LinearLayout>



Activity_result.xml:
Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

<textview
Android:id= "@+id/result_view"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"
Android:layout_centervertical= "true"
></TextView>

</RelativeLayout>



Experiment Summary:
This experiment has a little understanding of the application of intent, and realized that Java and C + + programming slightly different places.




Author: tornadomeet

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.