Elementary school arithmetic mental arithmetic practice app---No.7

Source: Internet
Author: User

Today mainly changed the settings page of the prompt box and the button's flash back problem:

Activity_calculators. Class

Package com.example.mmmjh.calculator;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.app.AlertDialog;
Import android.content.Intent;
Import Android.widget.TextView;
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.content.DialogInterface;
Import Android.widget.Toast;

public class Activity_calculators extends Appcompatactivity {
Private Button begin;
EditText b,min,sec;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_calculators);
Begin= (Button) Findviewbyid (R.id.button1);

b= (EditText) Findviewbyid (R.ID.EDITTEXT2);
Min= (EditText) Findviewbyid (R.ID.EDITTEXT3);
Sec= (EditText) Findviewbyid (R.ID.EDITTEXT4);
Begin.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Intent intent=new Intent ();
String Tishu=b.gettext (). toString ();//Gets the value entered
String Minute=min.gettext (). toString ();//Gets the value entered
String Second=sec.gettext (). toString ();//Gets the value entered
Bundle bundle = new bundle ();
Bundle.putstring ("Tishu", Tishu);
Bundle.putstring ("Minute", minute);
Bundle.putstring ("Second", second);
Intent.putextras (bundle);//Pass the number of topics to the next page this is a method
if (second.length () = = 0 | | minute.length () = = 0) {
Alertdialog.builder builder1 = new Alertdialog.builder (activity_calculators.this)
. Setmessage ("Setup is wrong, please perfect!" ")
. Setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {//pop-up window

@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Dialog.dismiss ();
}
});
Builder1.create (). Show ();
}
if (Second.length ()! = 0 &&minute.length ()! = 0) {
Intent.setclass (Activity_calculators.this, calculatoractivity.class);//Jump
StartActivity (Intent);
Activity_calculators.this.finish ();
}
}
});
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Menu.add (Menu.none, Menu.first + 2, 2, "save"). SetIcon (Android. R.drawable.ic_menu_edit);
Menu.add (Menu.none, Menu.first + 3, 6, "help"). SetIcon (Android. R.DRAWABLE.IC_MENU_HELP);
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case Menu.first + 2:
Toast.maketext (This, "Save menu is clicked", Toast.length_long). Show ();
Break
Case Menu.first + 3:
Toast.maketext (This, "Help menu was clicked", Toast.length_long). Show ();

Break
}
return false;
}
}



Button's Flashback (occasional) Issue:

Originally thought that is because of the following reasons, but still error after the change, continue to find problems in other pages, and finally found that the question is the class of the page is not the problem of division, the mother is not 0,

Here you add a judgment denominator is 0, and this position should be added to the first position above! The first thing to judge is whether he is 0.

Calculator.class (final version)

Package com.example.mmmjh.calculator;
Import Android.os.Build;
Import Android.os.Bundle;
Import Java.util.Random;
Import Android.os.Bundle;
Import Android.os.SystemClock;
Import Android.app.AlertDialog;
Import android.app.Activity;
Import android.content.Intent;
Import Android.support.annotation.RequiresApi;
Import Android.text.InputType;
Import Android.text.TextUtils;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.content.DialogInterface;
Import Android.widget.Button;
Import Android.widget.Chronometer;
Import Android.widget.Chronometer.OnChronometerTickListener;
Import Android.widget.EditText;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import Android.view.ViewGroup;
Import Android.widget.RelativeLayout;
;
public class Calculatoractivity extends Activity {

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

LinearLayout linearlayout = (linearlayout) Findviewbyid (r.id.viewobj);
Relativelayout relativelayout = new Relativelayout (this);

Intent Inten = Getintent ();
Bundle bundle = Inten.getextras ();
String Tishu = bundle.getstring ("Tishu");//receive the number of topics passed over

String min = bundle.getstring ("minute");
String sec = bundle.getstring ("second");//Receive Time

int i1 = 0, minute = 0, second = 0;
try {
I1 = Integer.parseint (Tishu);//i1 here is the number of topics in the Shaping value
minute = Integer.parseint (min);
Second = Integer.parseint (sec);
} catch (NumberFormatException e) {
E.printstacktrace ();
}


Final edittext[] input = new EDITTEXT[I1];
textview[] Showti = new TEXTVIEW[I1];
Final int[] shu1 = new INT[I1];
Final int[] Shu2 = new INT[I1];
Final int[] result = new INT[I1];
Final string[] user = new STRING[I1];
Final string[] show = new STRING[I1];
LinearLayout ll = (linearlayout) Findviewbyid (r.id.viewobj);

Random r = new Random ();
char[] ch = {' + ', '-', ' x ', ' ÷ '}; Character array

Final chronometer ch1 = new Chronometer (this);
Ch1.setbase (Systemclock.elapsedrealtime ());
Ch1.setformat ("Time used:%s");
Ch1.start ();
Ll.addview (CH1);
for (int i = 0; i < I1; i++) {
int index = R.nextint (ch.length); Random number, less than the length of the array, 0~3
Char flag = ch[index];//Get operation symbol
String d = string.valueof (flag);//Operator
Show[i] = "";
int a = (int) (Math.random () * 100);
int b = (int) (Math.random () * 100);

Shu1[i] = A;
Shu2[i] = b;

while ((D.equals ("+") && (A + B > 100)) | | (D.equals ("-") && (A-b <= 0)) | | (D.equals ("x") && (A > 9 | | A < 1 | | b > 9 | | b < 1)) | | (D.equals ("÷") && (b==0| | A/b > 9 | | A% b! = 0 | | A > 81 | | B > 9 | | A < b)) {
A = (int) (Math.random () * 100);
b = (int) (Math.random () * 100);
}

if (d.equals ("+"))
Result[i] = a + b;
else if (d.equals ("-"))
Result[i] = a-B;
else if (D.equals ("X"))
Result[i] = a * b;
Else
Result[i] = A/b;
String sa = integer.tostring (a);
String sb = integer.tostring (b);
Showti[i] = new TextView (this);
Show[i] + = "\ n" + sa + D + sb + "=";
Showti[i].settextsize (20);
Showti[i].settext (Show[i]);
Showti[i].setid (View.generateviewid ());
Input[i] = new EditText (this);
Input[i].settextsize (25);
Input[i].setinputtype (Inputtype.type_class_number);
Input[i].setid (View.generateviewid ());
Input[i].setems (6);
Relativelayout.layoutparams Layoutparams =
New Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if (i = = 0)
{
Layoutparams.setmargins (250,0,0,0);
Showti[i].setlayoutparams (Layoutparams);
Relativelayout.addview (Showti[i]);
}
else {
Layoutparams.addrule (Relativelayout.below,showti[i-1].getid ());
Layoutparams.setmargins (250,0,0,0);
Showti[i].setlayoutparams (Layoutparams);
Relativelayout.addview (Showti[i]);
}

Layoutparams =
New Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Layoutparams.addrule (Relativelayout.right_of,showti[i].getid ());
if (i! = 0) Layoutparams.addrule (Relativelayout.below,showti[i-1].getid ());
Input[i].setlayoutparams (Layoutparams);
Relativelayout.addview (Input[i]);
}
Linearlayout.addview (relativelayout);
Final button finish = New button (this);
Finish.settext ("Done");
Linearlayout.layoutparams Layoutparams =
New Linearlayout.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Finish.setlayoutparams (Layoutparams);
Linearlayout.addview (finish);

Final int timeover = minute * + second;

Ch1.setonchronometerticklistener (New Onchronometerticklistener () {
@Override
public void Onchronometertick (Chronometer arg0) {
TODO auto-generated Method Stub
if (Systemclock.elapsedrealtime ()-ch1.getbase () >=timeover*1000) {
Alertdialog.builder builder1 = new Alertdialog.builder (calculatoractivity.this)
. Setmessage ("Time to!" ")
. Setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {

for (int i = 0; i < shu1.length; i++) {
if (Textutils.isempty (Input[i].gettext ())) {
User[i] = "not answered";
} else {
User[i] = Input[i].gettext (). toString ();
}
}
String time = Ch1.gettext (). toString ();
Intent Intent = new Intent ();
Bundle bundle = new bundle ();
Bundle.putintarray ("Shu1", shu1);//number produced
Bundle.putintarray ("Shu2", shu2);//number produced
Bundle.putstringarray ("user", user);//The result of the calculation
Bundle.putstringarray ("show", show);//The resulting formula
Bundle.putintarray ("result", result);//correct results
Bundle.putstring ("Time");//Times used
Intent.putextras (bundle);
Intent.setclass (Calculatoractivity.this, resultactivity.class);//Jump
StartActivity (Intent);
CalculatorActivity.this.finish ();
Dialog.dismiss ();
}
});

Builder1.create (). Show ();

Finish.callonclick ();
}

}
});

Finish.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {//Touch button Event Completion page jump into answer page
TODO auto-generated Method Stub

for (int i = 0; i < user.length; i++) {
if (Textutils.isempty (Input[i].gettext ())) {
User[i] = "not answered";
} else {
User[i] = Input[i].gettext (). toString ();
}
}
Ch1.stop ();
String time = Ch1.gettext (). toString ();
Intent Intent = new Intent ();
Bundle bundle = new bundle ();
Bundle.putintarray ("Shu1", shu1);//number produced
Bundle.putintarray ("Shu2", shu2);//number produced
Bundle.putstringarray ("user", user);//The result of the calculation
Bundle.putstringarray ("show", show);//The resulting formula
Bundle.putintarray ("result", result);//correct results
Bundle.putstring ("Time");//Times used
Intent.putextras (bundle);
if (Systemclock.elapsedrealtime ()-ch1.getbase () <timeover*1000) {
Intent.setclass (Calculatoractivity.this, resultactivity.class);//Jump
StartActivity (Intent);
CalculatorActivity.this.finish ();//comment on this sentence press the back button on your phone to return to the previous layer
}
}
});
}
}

Elementary school arithmetic mental arithmetic practice app---No.7

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.