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

Source: Internet
Author: User
Tags getbase gettext

Today the main set of answer page, mainly is the question page and answer page between the issue of the problem and the answer page display problem! This is used to wrap the title of the array into a number of string types (including the equals sign and the user's answer) to wrap the correct answer in a separate array, and to wrap the relevant hints into an array, inside the Calculatoractivity.class!

The code is as follows:

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 ("÷") && (A/b > 9 | | A% b! = 0 | | a > Bayi | | 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
}
}
});
}
}

Package com.example.mmmjh.calculator;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.app.ActionBar;
Import android.app.Activity;
Import android.content.Intent;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.TextView;
Import Android.widget.Toast;

public class Resultactivity extends Appcompatactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_result);
TextView text= (TextView) Findviewbyid (R.id.textresult);
TextView text2= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Intent inten=getintent ();
Bundle Bundle=inten.getextras ();
int []SHU1 = Inten.getintarrayextra ("shu1");//number produced
int []SHU2 = Inten.getintarrayextra ("SHU2");//number produced
String []user = Inten.getstringarrayextra ("user");//Result of calculation
String []show = Inten.getstringarrayextra ("show");//The resulting formula
int []result = Inten.getintarrayextra ("result");//correct result
String time=bundle.getstring ("Time");//Elapsed time
int score=0;
String select= "";
String all= "";
for (int i=0;i<shu1.length;i++)
{
Select= "";
String resultstring= "";
Resultstring=integer.tostring (Result[i]);
if (User[i].equals (resultstring))
{
score++;//Number of correct answers
select+= "??";
}
Else
select+= "??";
all+= "+show[i]+user[i]+" "+result[i]+" "+select+" \ n ";
Text.settext (All);
}
Text2.settext ("This time altogether" +shu1.length+ "the question, answers the correct" +score+ "the way! "+time);

}

}
}
The results are as follows

Because each problem consists of a different length of string, the position cannot be fixed! Only add empty style to try! It's supposed to be.

Later it was changed to the above code as described above, no matter how the location is not fixed (for such an array to pass the argument!) )

Layout Activity_result

<?xml version= "1.0" encoding= "Utf-8"?>
<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:weightsum= "1" >
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"

Tools:context= ". Resultactivity ">

<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "202DP"
android:orientation= "Vertical" >

<textview
Android:id= "@+id/texttitle"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "30DP"
Android:textsize= "22SP"
android:layout_margintop= "36DP"
android:text= "Your answer to the right answer is judged"/>

<textview
Android:id= "@+id/textresult"
Android:layout_width= "Wrap_content"
android:layout_height= "Match_parent"
android:layout_marginleft= "35DP"
android:text= "TextView"/>

<textview
Android:id= "@+id/textview1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "40DP"
Android:textsize= "12pt"
android:text= "TextView"/>

</LinearLayout>
</ScrollView>

(the button's flash-back problem is still not able to solve!) )


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

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.