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

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

时间:2018-03-01 23:31:49      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:layout   new   inflate   详解   set   指定   ext4   tis   protect   

今天主要是实现按照指定的题目出题数目出题。在昨天设置页面的基础上,今天首先要学习的是接收不同页面间的参数问题。详解如下:

技术分享图片

 

然后就开始我的传参和接收参数的问题!

 

 

在当前的Activity上进行跳转,

代码如下:

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();//获取输入的数值

Bundle bundle = new Bundle();
bundle.putString("tishu", tishu);
intent.putExtras(bundle);//将题目数量传入下一个页面 这是一个方法
          
intent.setClass(activity_calculators.this, CalculatorActivity.class);//跳转
startActivity(intent);
activity_calculators.this.finish();
}
}


跳转的目标页面的java代码:
CalculatorActivity.class:

package com.example.mmmjh.calculator;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
public class CalculatorActivity extends Activity {
private Random num1=new Random();//随机数生成
private Random num2=new Random();//随机数生成
private Random r = new Random();
private char[] ch = {‘+‘,‘-‘,‘ב,‘÷‘}; //字符数组
private int index = r.nextInt(ch.length); //随机数,小于数组的长度数, 0~3
private char a=ch[index];//获取运算符号

private TextView text1,text2,text3;
private EditText answer;
private Button surebutton;//确定按钮
private int i1,i2,i3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String tishu = bundle.getString("tishu");//接收传过来的题目的数量
int i=0;
try {
i = Integer.parseInt(tishu);//i在这里是题目数量的整形值
} catch (NumberFormatException e) {
e.printStackTrace();
}

text1=(TextView)findViewById(R.id.textView1);//随机数字
text2=(TextView)findViewById(R.id.textView2);//运算符号
text3=(TextView)findViewById(R.id.textView3);//随机数字
answer=(EditText)findViewById(R.id.editText1);//运算结果
String c=String.valueOf(num1.nextInt(100));
i2=Integer.valueOf(c);//将产生的数值转换成整形
String d=String.valueOf(a);//运算符
String e=String.valueOf(num2.nextInt(100));
i3=Integer.valueOf(e);//将产生的数值转换成整形

while((d.equals("÷")&&i2%i3!=0)||(d.equals("-")&&i2<i3)||(d.equals("×")&&(i2*i3)>100)||(d.equals("+")&&(i2+i3)>100))
{
text1=(TextView)findViewById(R.id.textView1);//随机数字
text3=(TextView)findViewById(R.id.textView3);//随机数字
c=String.valueOf(num1.nextInt(100));
i2=Integer.valueOf(c);//将产生的数值转换成整形
e=String.valueOf(num2.nextInt(100));
i3=Integer.valueOf(e);//将产生的数值转换成整形
}


text1.setText(c);//随机数1-100 为他们赋值
text2.setText(d);//随机运算符+,-,*,/
text3.setText(e);//随机数1-100 赋值 在手机上显示出来
surebutton=(Button)findViewById(R.id.surebutton);//确定按钮

surebutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final String b=answer.getText().toString();//获取输入的数值
i1=Integer.valueOf(b);//将获取的值转换成整形

switch(index){//index是上面判断是数组中哪个运算符的
case 0:
{
if(i1==(i2+i3))
Toast.makeText(CalculatorActivity.this, "正确,答案为"+b, Toast.LENGTH_SHORT).show();
else
Toast.makeText(CalculatorActivity.this, "错误,正确答案为"+(i3+i2), Toast.LENGTH_SHORT).show();
break;
}
case 1:
{
if(i1==(i2-i3))
Toast.makeText(CalculatorActivity.this, "正确,答案为"+b, Toast.LENGTH_SHORT).show();
else
Toast.makeText(CalculatorActivity.this, "错误,正确答案为"+(i2-i3), Toast.LENGTH_SHORT).show();
break;
}
case 2:{
if(i1==(i2*i3))
Toast.makeText(CalculatorActivity.this, "正确,答案为"+b, Toast.LENGTH_SHORT).show();
else
Toast.makeText(CalculatorActivity.this, "错误,正确答案为"+(i2*i3), Toast.LENGTH_SHORT).show();
break;
}
case 3:
{
if(i3!=0){
if(i1==((double)i2/(double)i3))
Toast.makeText(CalculatorActivity.this, "正确,答案为"+b, Toast.LENGTH_SHORT).show();
else
Toast.makeText(CalculatorActivity.this, "错误,正确答案为"+((double)i2/(double)i3), Toast.LENGTH_SHORT).show();
}
break;
}
}
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}

}

 

目前只能做到这个效果,还要把横线改位置!

技术分享图片

 


 

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

标签:layout   new   inflate   详解   set   指定   ext4   tis   protect   

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

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