我是一名独立研究班的高中生,我绝对不知道我在使用eclipse或java做什么。我正在尝试制作一个给出数学问题的应用程序,让您回答并继续前进。如果你把它弄错了,它会显示一个吐司通知,但无论如何都会继续前进。我无法弄清楚如何在提交答案后让它刷新所有值,它会告诉你它是对还是错。我假设我需要某种类型的循环来继续通过代码,直到你关闭应用程序,但我不知道这应该是什么样子。有人能帮助我吗?
package com.example.droidmaze;
import java.util.Random;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class App extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
final Button sub=(Button) findViewById(R.id.button1);
final EditText answer=(EditText) findViewById(R.id.editText1);
final TextView messages=(TextView) findViewById(R.id.textView1);
int number1=0;
int number2=0;
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx){
int randomInt = randomGenerator.nextInt(10);
number1 += randomInt;
}
Random randomGenerator2 = new Random();
for (int idx = 1; idx <= 10; ++idx){
int randomInt = randomGenerator2.nextInt(10);
number2 += randomInt;
}
String message = number1 + "\n + \n" + number2;
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
final String sum = ""+ (number1 + number2);
messages.setText(message);
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String user_answer=answer.getText().toString();
if (sum.equals(user_answer)){
Toast.makeText(getApplicationContext(), "Congrats!",
Toast.LENGTH_LONG).show();
}
if (sum.equals(user_answer)){
Toast.makeText(getApplicationContext(), "Thats not quite right!",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app, menu);
return true;
}
}
答案 0 :(得分:1)
可以通过以下方式完成:
public class MyActivity extends Activity {
private Button sub;
private EditText answer;
private TextView messages;
private String sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
sub = (Button) findViewById(R.id.button1);
answer = (EditText) findViewById(R.id.editText1);
messages = (TextView) findViewById(R.id.textView1);
generateNumbers();
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String user_answer = answer.getText().toString();
String toastMessage = "";
if (sum.equals(user_answer)) {
toastMessage = "Congrats!";
} else {
toastMessage = "Thats not quite right!";
}
Toast.makeText(getApplicationContext(), toastMessage ,
Toast.LENGTH_LONG).show();
generateNumbers();
}
});
}
private void generateNumbers() {
int number1 = 0;
int number2 = 0;
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx) {
int randomInt = randomGenerator.nextInt(10);
number1 += randomInt;
}
Random randomGenerator2 = new Random();
for (int idx = 1; idx <= 10; ++idx) {
int randomInt = randomGenerator2.nextInt(10);
number2 += randomInt;
}
String message = number1 + "\n + \n" + number2;
messages.setText(message);
sum = String.valueOf(number1 + number2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app, menu);
return true;
}
}
背后的逻辑是什么?好吧,数字生成的所有代码都转移到了另一种方法 - Don't Repeat Yourself
规则。我们从Activity
开始,找到Views
,生成数字。在onClickListener
我们正在检查输入的值,如果它是正确的,那么我们正在显示一条消息,如果不是 - 另一条消息,并且在任何情况下都会生成新的数字。如果它们是外部类的字段,你也可以在内部类中使用变量,在这种情况下不需要final
修饰符。