码迷,mamicode.com
首页 > 移动开发 > 详细

小学四则运算口算练习app---No.5

时间:2018-03-01 23:33:06      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:result   长度   pack   相关   show   equal   oid   随机   alc   

今天主要设置答案页面,主要是出题页面和答案页面之间的传参问题以及答案页面的展示问题!这里运用数组讲出的题目包装成一个String类型数目(包括等号和使用者的作答) 讲正确答案单独包装成一个数组,以及相关提示包装成一个数组,在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");//接收传过来的题目的数量

String min = bundle.getString("minute");
String sec = bundle.getString("second");//接收时间

int i1 = 0, minute = 0, second = 0;
try {
i1 = Integer.parseInt(tishu);//i1在这里是题目数量的整形值
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 = {‘+‘, ‘-‘, ‘ב, ‘÷‘}; //字符数组

final Chronometer ch1 = new Chronometer(this);
ch1.setBase(SystemClock.elapsedRealtime());
ch1.setFormat("已用时间:%s");
ch1.start();
ll.addView(ch1);
for (int i = 0; i < i1; i++) {
int index = r.nextInt(ch.length); //随机数,小于数组的长度数, 0~3
char flag = ch[index];//获取运算符号
String d = String.valueOf(flag);//运算符
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("×") && (a > 9 || a < 1 || b > 9 || b < 1)) || (d.equals("÷") && (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("×"))
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("完成");
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
finish.setLayoutParams(layoutParams);
linearLayout.addView(finish);

final int timeover = minute * 60 + 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("时间到!")
.setPositiveButton("确认", 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] = "未作答";
} else {
user[i] = input[i].getText().toString();
}
}
String time = ch1.getText().toString();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putIntArray("shu1", shu1);//产生的数字
bundle.putIntArray("shu2", shu2);//产生的数字
bundle.putStringArray("user", user);//算的结果
bundle.putStringArray("show", show);//产生的算式
bundle.putIntArray("result", result);//正确的结果
bundle.putString("time", time);//使用的时间
intent.putExtras(bundle);
intent.setClass(CalculatorActivity.this, resultActivity.class);//跳转
startActivity(intent);
// CalculatorActivity.this.finish();
dialog.dismiss();
}
});

builder1.create().show();

finish.callOnClick();
}

}
});

finish.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {//触及按钮事件完成页面跳转 进入答案页面
// TODO Auto-generated method stub

for (int i = 0; i < user.length; i++) {
if (TextUtils.isEmpty(input[i].getText())) {
user[i] = "未作答";
} 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);//产生的数字
bundle.putIntArray("shu2", shu2);//产生的数字
bundle.putStringArray("user", user);//算的结果
bundle.putStringArray("show", show);//产生的算式
bundle.putIntArray("result", result);//正确的结果
bundle.putString("time", time);//使用的时间
intent.putExtras(bundle);
if(SystemClock.elapsedRealtime()-ch1.getBase()<timeover*1000){
intent.setClass(CalculatorActivity.this, resultActivity.class);//跳转
startActivity(intent);
// CalculatorActivity.this.finish();//注释了这句话按手机上的返回键可以返回上一层
}
}
});
}
}

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");//产生的数字
int []shu2 = inten.getIntArrayExtra("shu2");//产生的数字
String []user = inten.getStringArrayExtra("user");//算的结果
String []show = inten.getStringArrayExtra("show");//产生的算式
int []result = inten.getIntArrayExtra("result");//正确的结果
String time=bundle.getString("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++;//答对的数目
select+="??";
}
else
select+="??";
all+=""+show[i]+user[i]+" "+result[i]+" "+select+"\n";
text.setText(all);
}
text2.setText("本次一共"+shu1.length+"道题,回答正确"+score+"道!"+time);

}

}
}
结果如下

技术分享图片

 

因为每道题组成的string类型长度不一样 所以位置不能固定!只能加空格调试!本来是这样的

技术分享图片

后来还是改成了如上代码所述那样 无论怎样位置都是不能固定的(对于这样的数组传参来说!)

布局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="你的答案 正确答案 判断" />

<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>

 

(按钮的闪退问题还是没能解决!)


小学四则运算口算练习app---No.5

标签:result   长度   pack   相关   show   equal   oid   随机   alc   

原文地址:https://www.cnblogs.com/mm20/p/8490743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!