我正在开发一个小型的Android应用程序,让用户猜出1-100之间的数字,它会说猜测是正确的,是太小还是太高。它仍处于开发阶段,这是最基本的代码。 我想提到的另一件事是我是初学Android程序员,这是我的第二个应用程序。
activity_main.xml中
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText android:id="@+id/number_input"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number" />
<Button android:id="@+id/guess_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/guess_button" />
</LinearLayout>
<TextView android:id="@+id/message_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java 包com.vessialte.guessthenumber;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button guess_button;
TextView message_view;
EditText number_input;
int number;
int randNumber;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize views
guess_button = (Button) findViewById(R.id.guess_button);
message_view = (TextView) findViewById(R.id.message_view);
number_input = (EditText) findViewById(R.id.number_input);
number = Integer.parseInt(number_input.getText().toString());
// button click listener
guess_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (number < 1 || number > 100) {
AlertDialog.Builder errDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("The number has to be between 1 and 100")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// temporary
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
errDialog.show();
} else {
r = new Random();
randNumber = r.nextInt(100) + 1;
if (number == randNumber) {
message_view.setText("Your guess was correct! The number is: " + randNumber);
} else if (number < randNumber) {
message_view.setText("Your guess was too low! The number is: " + randNumber);
} else if (number > randNumber) {
message_view.setText("Your guess was too high! The number is: " + randNumber);
}
}
}
});
}
}
提前非常感谢你。
答案 0 :(得分:2)
这必须在onClickListener中。
number = Integer.parseInt(number_input.getText().toString());
你猜我猜NumberFormatException。因为Initially Edittext Value为空,并且您尝试转换为整数。
guess_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number = Integer.parseInt(number_input.getText().toString());
}
});
答案 1 :(得分:1)
您尝试从number_input
editText获取值,甚至在填写任何内容之前。移动
number = Integer.parseInt(number_input.getText().toString());
在setOnClickListener()
内,这样你只要在点击guess_button
时就让android获取值,因此editText中有一个值。
P.S。进行检查以确保在单击guess_button
时editText不为空,否则您将再次收到错误。