gpt4 book ai didi

java - Toast 不会在 Android 应用程序上显示,使用 android studio

转载 作者:行者123 更新时间:2023-12-01 20:14:30 24 4
gpt4 key购买 nike

早上好,我尝试让 toast 来显示学生人数是否小于 0 或大于 100。该应用程序似乎工作正常,除了显示 toast 之外。这项作业将于明天晚上截止。

这是我的代码:

package co.tekitall.classroommanager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

import static co.tekitall.classroommanager.R.id.NumOfStudents;

public class Classroom extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classroom);
Button button = (Button) findViewById(R.id.SubmitClassRoomInfoButton);

}

void ClassroomElements() {
//ArrayList
ArrayList<EditText> arrayList = new ArrayList<>();
arrayList.add((EditText) findViewById(R.id.Teacher_Name));
arrayList.add((EditText) findViewById(R.id.Room_Number));
arrayList.add((EditText) findViewById(R.id.ClassroomHelper));
arrayList.add((EditText) findViewById(R.id.NumOfStudents));
}

OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText numofstudents = (EditText) findViewById(R.id.NumOfStudents);
String getstucount = numofstudents.getText().toString();

int setstucount = Integer.parseInt(getstucount);

if(setstucount < 0) {

numofstudents.setText("");
Toast toast = Toast.makeText(Classroom.this, "Please try Again! Number must be greater then 0 and less than 100...", Toast.LENGTH_LONG);
toast.show();

}

if(setstucount > 100) {

numofstudents.setText("");
Toast toast = Toast.makeText(Classroom.this, "Please try Again! Number must be greater then 0 and less than 100...", Toast.LENGTH_LONG);
toast.show();

} else
numofstudents.setText("");
Toast toast = Toast.makeText(Classroom.this, "Good Job",
Toast.LENGTH_LONG);
toast.show();
}
};
}

最佳答案

您需要使用绑定(bind)按钮和监听器

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classroom);
Button button = (Button) findViewById(R.id.SubmitClassRoomInfoButton);
button.setOnClickListener(listener);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

另外使用 ifelse-if 而不是多个 if,因为数字只能小于 0 否则大于 100 或介于 |等于 0 或 100

if(setstucount < 0){//...code}
else if(setstucount > 100){//...code}
else {//...code}

关于java - Toast 不会在 Android 应用程序上显示,使用 android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46141144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com