实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述小学四则运算口算练习app---No.3相关的知识,希望能为你提供帮助。
今天主要是实现按照指定的题目出题数目出题。在昨天设置页面的基础上,今天首先要学习的是接收不同页面间的参数问题。详解如下:
文章图片
然后就开始我的传参和接收参数的问题!
在当前的Activity上进行跳转,
代码如下:
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:
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 {
privateRandom num1=new Random(); //随机数生成
privateRandom num2=new Random();
privateRandom 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】
推荐阅读
- 小学四则运算口算练习app---No.4
- 小学四则运算口算练习app---No.5
- 小学四则运算口算练习app---No.2
- android彻底关闭应用程序方法
- Install Android Studio in Ubuntu 16.04 LTS
- 魅族box价格多少钱?魅族box售价
- 磊科路由器no1怎样?磊科自动选择路由器no1技巧评测
- 磊科路由器默认密码是啥?磊科路由器初始密码大全
- 磊科路由器怎样设置无线网络?磊科无线路由器设置无线网络图文详细教程