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

Source: Internet
Author: User
Tags getbase gettext

Today, the main thing is to change the location of the various components in the page and the time of the reception and Time control, the code is as follows: (but there is a problem setting page Click OK button to enter the topic screen sometimes flash back, unresolved!) )

Calculatoractivity.class

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 ();
}
}

Intent Intent = new Intent ();

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 ();

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
}
}
});
}
}

The results are as follows:

Layout page: Activity_calculator


<?xml version= "1.0" encoding= "Utf-8"?>
<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
android:background= "@mipmap/qq"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
tools:context= ". Resultactivity" >



<!--TextView
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_gravity= "Center_horizontal"
android:text= "title"/-->
<linearlayout
Android:id= "@+id/viewobj"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
Android:textsize= "25SP"
Android:textcolor= "#7300ff"
android:layout_x= "1px"
android:layout_y= "1px"/>

</ScrollView>


This time with the popup box setting, there are two main styles of toast and alert. How to use this article: http://blog.csdn.net/dearmali/article/details/49885759
Android offers commonly used message popups, toast and alert.


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

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.