一個算數(shù)小游戲的代碼
JAVA代碼
package com.mycompany.myapp2;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity?
{
? ??
?int max=10,min=1;
?int jiashu1 = (int) (Math.random() * (max - min) + min);?
?int jiashu2 = (int) (Math.random() * (max - min) + min);
?private TextView text = null;
?private Button btn = null;
?private EditText edit = null;
?private TextView exp = null;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState)
? ? {
? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.main);
? this.exp = (TextView)super.findViewById(R.id.exp);
? this.text = (TextView)super.findViewById(R.id.text);
? this.btn = (Button)super.findViewById(R.id.btn);
? this.edit = (EditText)super.findViewById(R.id.edit);
? text.setText(jiashu1+"+"+jiashu2+"=?");
? btn.setOnClickListener(new btnClick());
? ? }
?private class btnClick implements OnClickListener
?{
? public void onClick(View v)
? {
? ?int sum2 = (jiashu1 + jiashu2);
? ?int sum1 = Integer.valueOf(edit.getText().toString());
? ?//Toast.makeText(MainActivity.this,,Toast.LENGTH_SHORT).show();
? ?if (sum2==sum1)
? ?{
? ? exp.setText("??");
? ?}
? ?else
? ?{
? ? exp.setText("??");
? ?}
? ?int max=10,min=1;
? ?jiashu1 = (int) (Math.random() * (max - min) + min);?
? ?jiashu2 = (int) (Math.random() * (max - min) + min);
? ?text.setText(jiashu1+"+"+jiashu2+"=?");
? }
?}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
?xmlns:android="http://schemas.android.com/apk/res/android"
?android:layout_width="match_parent"
?android:layout_height="match_parent"
?android:orientation="vertical"
?android:id="@+id/mainLinearLayout1"
?android:alpha="1">
?<LinearLayout
? android:layout_height="wrap_content"
? android:layout_width="wrap_content"
? android:orientation="horizontal">
? <TextView
? ?android:layout_height="wrap_content"
? ?android:layout_width="wrap_content"
? ?android:text="1+1=?"
? ?android:id="@+id/text"
? ?android:textSize="35sp"/>
? <TextView
? ?android:layout_height="wrap_content"
? ?android:layout_width="wrap_content"
? ?android:text=""
? ?android:textSize="35sp"
? ?android:id="@+id/exp"/>
?</LinearLayout>
?<EditText
? android:layout_height="wrap_content"
? android:layout_width="wrap_content"
? android:ems="9"
? android:id="@+id/edit"
? android:hint="輸入數(shù)字"
? android:inputType="number"
? style="@style/AppTheme"/>
?<Button
? android:layout_height="wrap_content"
? android:layout_width="wrap_content"
? android:text="提交"
? android:id="@+id/btn"/>
</LinearLayout>