如果单击按钮,如果editText为空,如何发送消息? [英] how to toast a message if editText is empty by clicking button?

查看:158
本文介绍了如果单击按钮,如果editText为空,如何发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有2个编辑文本,有1个按钮用于在编辑文本中添加输入数字,还有1个文本视图以显示结果.如果我在单击按钮时,编辑文本"选项卡为空或为null,我想发表祝酒词.我已经搜索并尝试了所有解决方案.似乎没有任何效果.请帮忙!!

i have 2 edit Text in my application, 1 button to add the input numbers in the edit Text and 1 text view to display the result. I would like to put a toast message if my edit text tab is empty or null when i click the button. I have searched and tried all the solutions..and nothing seems to work. help please!!

这是我将两个数字相加的代码.

here is my code for adding the two numbers.

package com.example.mycalc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {



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

    final EditText editText1 = (EditText)findViewById(R.id.editText1);

    final EditText editText2 = (EditText)findViewById(R.id.editText2);

    Button button1 = (Button)findViewById(R.id.button1);

    final TextView textView1 = (TextView)findViewById(R.id.textView1);
    textView1.setText(" ");



    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


                double edit1 = Double.valueOf(editText1.getText().toString());  
                double edit2 = Double.valueOf(editText2.getText().toString());

                double text = edit1 + edit2;

                textView1.setText(String.valueOf(text));




        }
    });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

推荐答案

当用户单击按钮检查时:

when user clicks button check:

if (editText1.getText().toString().trim().length() <= 0) {
    Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}

将其修剪以避免空格.

这篇关于如果单击按钮,如果editText为空,如何发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆