安卓Activity:
package com.example.count_number;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private int x,y;//用以产生x,y
private int fhao;//用以产生符号
private EditText t1,t2,t3;
private Button b1;
private int answer,sure;
private EditText e1,e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = (int)(Math.random() *9)+1; //不能出现0
y = (int)(Math.random() *9)+1;
fhao= (int)(Math.random() *4)+1;
t1=(EditText)findViewById(R.id.et1);
t2=(EditText)findViewById(R.id.et2);
t3=(EditText)findViewById(R.id.et3);
b1=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
//显示
t1.setText(x);
switch(fhao)
{
case 1:
t2.setText("+");
sure=x+y;
break;
case 2:
t2.setText("-");
sure=x-y;
break;
case 3:
t2.setText("*");
sure=x*y;
break;
case 4:
t2.setText("/");
sure=x/y;
break;
}
t3.setText(y);
//输入和确定答案
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
answer=Integer.parseInt(e1.getText().toString());
if(answer==sure)
{
e2.setText("正确");
}
if(answer!=sure)
{
e2.setText("错误");
}
}
});
}
}
安卓XML 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".MainActivity" >
<EditText
android:id="@+id/et4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp" />
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true" />
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignRight="@+id/textView4"/>
<EditText
android:id="@+id/et3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/editText1" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="145dp"
android:ems="10" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginBottom="75dp"
android:text="你的输入:" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="21dp"
android:layout_toLeftOf="@+id/textView2"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignLeft="@+id/editText2"
android:layout_marginBottom="15dp"
android:text="是否正确:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:text="提交答案" />
</RelativeLayout>
这是一个简单的小学生四则运算APP