用户留下空白editText时如何设置Toast消息

时间:2019-07-10 10:37:35

标签: java android

我在这里创建了这个随机数游戏程序,用户输入数字b / w 1至20,如果用户输入的数字大于随机数,则将显示吐司消息,并且当用户输入的数字小于随机数时。但是,当用户留下空白的EditText并按下按钮时,如何设置Toast消息? 请帮助我。

package com.example.higherlowergame;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity
{
    int randnum;
    EditText e;

    public void guess(View view)
    {
        int guessnum= Integer.parseInt ( e.getText ().toString () );

        if(e == null)
        {
            Toast.makeText ( this , "Enter a num" , Toast.LENGTH_SHORT ).show ();
        }

        if(guessnum > randnum)
        {
            Toast.makeText ( this , "guess lower num" , Toast.LENGTH_SHORT ).show ();
        }
        else if (guessnum < randnum)
        {
            Toast.makeText ( this , "guess upper num" , Toast.LENGTH_SHORT ).show ();
        }
        else
        {
            Toast.makeText ( this , "yehh Thts the right num ! and try again for new num" , Toast.LENGTH_LONG ).show ();

            Random random=new Random (  );
            randnum=random.nextInt (20)+ 1;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );

        e=(EditText)findViewById ( R.id.et );

        Random random=new Random (  );
        randnum=random.nextInt (20) + 1;
    }
}

2 个答案:

答案 0 :(得分:0)

首先,您的代码容易出错。更改

 public void guess(View view)
    {


        int guessnum= Integer.parseInt ( e.getText ().toString () );

        if(e == null)
        {
            Toast.makeText ( this , "Enter a num" , Toast.LENGTH_SHORT ).show ();
        }

        if(guessnum > randnum)
        {
            Toast.makeText ( this , "guess lower num" , Toast.LENGTH_SHORT ).show ();
        }
        else if (guessnum < randnum)
        {
            Toast.makeText ( this , "guess upper num" , Toast.LENGTH_SHORT ).show ();
        }
        else
        {
            Toast.makeText ( this , "yehh Thts the right num ! and try again for new num" , Toast.LENGTH_LONG ).show ();

            Random random=new Random (  );
            randnum=random.nextInt (20)+ 1;
        }

    }

对此

 public void guess(View view){

       if (e.getText().toString.equals("")) {
           Toast.makeText ( this , "Empty field" , Toast.LENGTH_SHORT ).show ();
           return;
       }

    // Field not empty but text entered instead of numbers
        int guessnum;
        try{

        guessnum= Integer.parseInt ( e.getText ().toString () );
        }catch (Exception e) {
            Toast.makeText ( this , "Enter a num" , Toast.LENGTH_SHORT ).show ();
            return;
        }


        if(guessnum > randnum)
        {
            Toast.makeText ( this , "guess lower num" , Toast.LENGTH_SHORT ).show ();
        }
        else if (guessnum < randnum)
        {
            Toast.makeText ( this , "guess upper num" , Toast.LENGTH_SHORT ).show ();
        }
        else
        {
            Toast.makeText ( this , "yehh Thts the right num ! and try again for new num" , Toast.LENGTH_LONG ).show ();

            Random random=new Random (  );
            randnum=random.nextInt (20)+ 1;
        }

    } 

答案 1 :(得分:0)

按下按钮时,检查Edittext的长度是否大于零,然后继续使用该功能。

尝试以下代码,

import tensorflow as tf
#sess = tf.Session()
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
# more info on callbakcs: https://keras.io/callbacks/ model saver is cool too.
#from tensorflow.keras.callbacks import TensorBoard
from keras.callbacks import TensorBoard
import pickle
import time

NAME = "Cats-vs-dogs-CNN"

pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)

pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)

X = X/255.0

model = Sequential()

model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))

model.add(Dense(1))
model.add(Activation('sigmoid'))


model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'],
              )

tensor_board = TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)

model.fit(X, y,
          batch_size=16,
          epochs=1,
          validation_split=0.3,
          callbacks=[tensor_board])