[英]how to toast a message if editText is empty by clicking button?
我的应用程序中有2个编辑文本,有1个按钮可在编辑文本中添加输入数字,还有1个文本视图以显示结果。 如果我单击按钮时编辑文本选项卡为空或为null,我想发表祝酒词。 我搜索并尝试了所有解决方案。似乎没有任何效果。 请帮助!!
这是我将两个数字相加的代码。
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);
}
}
用户单击按钮时,请检查:
if (editText1.getText().toString().trim().length() <= 0) {
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
修剪它以避免空格。
要创建并显示Toast,请使用以下代码:
Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
为了正常工作,您应该替换以下代码:
if (editText1.equals("")) {
editText1.setError("please input number");
}
if (editText2.equals("")) {
editText2.setError("please input number");
}
有了这个:
if (editText1.getText().toString().length() == 0 || editText1.getText().toString().length() == 1) {
Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
}
您可以尝试将这两个函数用于字符串empty , not empty或null 。 简单返回true或false。 它在所有项目中都非常有用。
if(isEmpty(edittext.getText().toString())){
// your Toast message if string is empty
}
if(isNotEmpty(edittext.getText().toString())){
// your Toast message if string is not empty
}
public static boolean isEmpty(String str) {
if (str == null)
return true;
else if (str.toString().trim().length() == 0)
return true;
return false;
}
public static boolean isNotEmpty(String str) {
if (str == null)
return false;
else if (str.toString().trim().length() == 0)
return false;
return true;
}
添加到您的点击侦听器检查
Toast toast = Toast.makeText(MainActivity.this, "Your Message", Toast.LENGTH_SHORT);
toast.show();
您的代码将是:
@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) {
if(editText1.getText().toString().trim().length() == 0 || editText2.getText().toString().trim().length() == 0) {
Toast.makeText(this,"Edittext is null",Toast.LENGTH_SHORT).show();
}
else {
double edit1 = Double.valueOf(editText1.getText().toString());
double edit2 = Double.valueOf(editText2.getText().toString());
double text = edit1 + edit2;
textView1.setText(String.valueOf(text));
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.