热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

今天主要是实现按照指定的题目出题数目出题。在昨天设置页面的基础上,今天首先要学习的是接收不同页面间的参数问题。详解如下:然后就开始我的传参和接收参数的问题!在当前的Activity上

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

 

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

 

 

在当前的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 {
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("-")&&i2100)||(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;
}

}

 

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

 

 

 


推荐阅读
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 详解Android  自定义UI模板设计_由浅入深
    学习安卓已有一些日子,前段时间整理了不少笔记,但是发现笔记不变分享与携带。今天开始整理博客,全当是与大家分享交流与自身学习理解的过程吧。结合最近在做的一个新闻类app及学习中的问题,一点一点整理一下, ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文介绍了一款名为TimeSelector的Android日期时间选择器,采用了Material Design风格,可以在Android Studio中通过gradle添加依赖来使用,也可以在Eclipse中下载源码使用。文章详细介绍了TimeSelector的构造方法和参数说明,以及如何使用回调函数来处理选取时间后的操作。同时还提供了示例代码和可选的起始时间和结束时间设置。 ... [详细]
  • Java图形化计算器设计与实现
    本文介绍了使用Java编程语言设计和实现图形化计算器的方法。通过使用swing包和awt包中的组件,作者创建了一个具有按钮监听器和自定义界面尺寸和布局的计算器。文章还分享了在图形化界面设计中的一些心得体会。 ... [详细]
  • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
  • android 触屏处理流程,android触摸事件处理流程 ? FOOKWOOD「建议收藏」
    android触屏处理流程,android触摸事件处理流程?FOOKWOOD「建议收藏」最近在工作中,经常需要处理触摸事件,但是有时候会出现一些奇怪的bug,比如有时候会检测不到A ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Android源码中的Builder模式及其作用
    本文主要解释了什么是Builder模式以及其作用,并结合Android源码来分析Builder模式的实现。Builder模式是将产品的设计、表示和构建进行分离,通过引入建造者角色,简化了构建复杂产品的流程,并且使得产品的构建可以灵活适应变化。使用Builder模式可以解决开发者需要关注产品表示和构建步骤的问题,并且当构建流程发生变化时,无需修改代码即可适配新的构建流程。 ... [详细]
  • VS2010MFC(对话框:为对话框添加控件)
    转自:http:www.jizhuomi.comsoftware151.html上一讲创建了一个名为“Addition”的工程,目的是生成一个实现加法运 ... [详细]
  • android:EditText属性去边框EditText继承关系:View--TextView--EditTextEditText的属性很多,这里介绍几个:android:h ... [详细]
author-avatar
雅皮享客被
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有