Java 除非整个箱子都装满了,否则不要进行下一个活动
我制作了一个应用程序,用于计算出现在另一个活动中的字符串值。我的应用程序有3个编辑文本和一个按钮。如果我们在编辑文本中输入并按下按钮Java 除非整个箱子都装满了,否则不要进行下一个活动,java,android,validation,onclick,android-edittext,Java,Android,Validation,Onclick,Android Edittext,我制作了一个应用程序,用于计算出现在另一个活动中的字符串值。我的应用程序有3个编辑文本和一个按钮。如果我们在编辑文本中输入并按下按钮calculate,计算值将显示在第二个活动中。我成功地做到了这一点,但我的问题是,如果用户将任何框留空并按下按钮,系统将强制关闭。为了避免这种情况,我在图形布局中预先输入了一个提示,并制作了editText。但是,如果用户在编辑editText后再次意外按下按钮,系统将显示强制关闭。 我尝试了许多if-else方法,但都不起作用。有人告诉我如何正确地选中所有edi
calculate
,计算值将显示在第二个活动中。我成功地做到了这一点,但我的问题是,如果用户将任何框留空并按下按钮,系统将强制关闭。为了避免这种情况,我在图形布局中预先输入了一个提示,并制作了editText
。但是,如果用户在编辑editText
后再次意外按下按钮,系统将显示强制关闭。
我尝试了许多if-else
方法,但都不起作用。有人告诉我如何正确地选中所有editText
框
public class Screen1 extends Activity {
public static StringBuilder EXTRA_MESSAGE=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
Button bu1= (Button)findViewById(R.id.bu);
bu1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View message)
{ int f1,f3,f4;
String vis;
EditText t1=(EditText)findViewById(R.id.a);
EditText t3=(EditText)findViewById(R.id.c);
EditText t4=(EditText)findViewById(R.id.d);
String e1=t1.getText().toString();
String e3= t3.getText().toString();
String e4= t4.getText().toString();
f1 =Integer.parseInt(e1);
f4=Integer.parseInt(e4);
f3=Integer.parseInt(e3);
StringBuilder sb = new StringBuilder();
for( float i= (float)0.1;i<=5;i=i+(float) .1)
{
sb.append(String.format("%.1f", i)+" mcg = "+(f1*60*i*f4)/(f3*1000)+"\n");
}
vis=sb.toString();
Intent intent = new Intent(message.getContext(),Screen2.class);
intent.putExtra("EXTRA_MESSAGE",vis);
startActivity(intent);
}
});
}
}
公共类屏幕1扩展活动{
公共静态StringBuilder额外消息=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
按钮bu1=(按钮)findViewById(R.id.bu);
bu1.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(查看消息)
{int f1,f3,f4;
弦vis;
EditText t1=(EditText)findViewById(R.id.a);
EditText t3=(EditText)findViewById(R.id.c);
EditText t4=(EditText)findViewById(R.id.d);
字符串e1=t1.getText().toString();
字符串e3=t3.getText().toString();
字符串e4=t4.getText().toString();
f1=整数.parseInt(e1);
f4=整数.parseInt(e4);
f3=整数.parseInt(e3);
StringBuilder sb=新的StringBuilder();
对于(float i=(float)0.1;i使用[TextUtils][1]
检查isEmpty()
单击中的文本:
@Override
public void onCreate(Bundle savedInstanceState){
EditText t1=(EditText)findViewById(R.id.a);
EditText t3=(EditText)findViewById(R.id.c);
EditText t4=(EditText)findViewById(R.id.d);
Button bu1 = (Button) findViewById(R.id.bu);
bu1.setOnClickListener(this);
}
@Override
public void onClick(View message){
boolean foundEmpty = false;
if(TextUtils.isEmpty(t1.getText())) {
foundEmpty = true;
t1.setError("Please Enter a value");
}
if(TextUtils.isEmpty(t2.getText()){
foundEmpty = true;
t2.setError("Please Enter a value");
}
if(TextUtils.isEmpty(t3.getText()){
foundEmpty = true;
t3.setError("Please enter a value");
}
if(!foundEmpty){
/* none of your text fields are empty */
int f1, f3, f4;
f1 =Integer.parseInt(e1);
f4=Integer.parseInt(e4);
f3=Integer.parseInt(e3);
final StringBuilder sb = new StringBuilder();
for( float i= (float)0.1;i<=5;i=i+(float) .1) {
sb.append(String.format("%.1f", i)+
" mcg = "+
(f1*60*i*f4)/(f3*1000)+"\n");
}
final String vis = sb.toString();
Intent intent = new Intent(message.getContext(), Screen2.class);
intent.putExtra("EXTRA_MESSAGE", vis);
startActivity(intent);
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
EditText t1=(EditText)findViewById(R.id.a);
EditText t3=(EditText)findViewById(R.id.c);
EditText t4=(EditText)findViewById(R.id.d);
按钮bu1=(按钮)findViewById(R.id.bu);
bu1.setOnClickListener(此);
}
@凌驾
public void onClick(查看消息){
布尔值foundEmpty=false;
if(TextUtils.isEmpty(t1.getText())){
foundEmpty=true;
t1.设置错误(“请输入一个值”);
}
if(TextUtils.isEmpty(t2.getText()){
foundEmpty=true;
t2.设置错误(“请输入一个值”);
}
if(TextUtils.isEmpty(t3.getText()){
foundEmpty=true;
t3.设置错误(“请输入一个值”);
}
如果(!foundEmpty){
/*没有一个文本字段为空*/
int f1、f3、f4;
f1=整数.parseInt(e1);
f4=整数.parseInt(e4);
f3=整数.parseInt(e3);
最终StringBuilder sb=新StringBuilder();
对于(float i=(float)0.1;i,根据我从您的问题中收集的信息,当单击bu1
时,您会收到一个错误,并且至少有一个editText为空。这是因为您正在调用Integer.parseInt()
在空字符串上。以下代码将检查框是否为空,如果为空,则显示Toast。如果用户输入任何无效数字,也将显示Toast:
public class Screen1 extends Activity
{
private static final String EXTRA_MESSAGE = "extra_msg";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
final EditText t1 = (EditText)findViewById(R.id.a);
final EditText t3 = (EditText)findViewById(R.id.c);
final EditText t4 = (EditText)findViewById(R.id.d);
Button bu1 = (Button)findViewById(R.id.bu);
bu1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View message)
{
int f1,f3,f4;
String e1 = t1.getText().toString();
String e3 = t3.getText().toString();
String e4 = t4.getText().toString();
if (e1.equals("") || e3.equals("") || e4.equals(""))
{
Toast.makeText(Screen1.this, "Please enter all numbers.", Toast.LENGTH_SHORT).show();
return;
}
try
{
f1 = Integer.parseInt(e1);
f4 = Integer.parseInt(e4);
f3 = Integer.parseInt(e3);
StringBuilder sb = new StringBuilder();
for (float i = 0.1f; i <= 5; i = i + .1f)
{
sb.append(String.format("%.1f", i) + " mcg = " + (f1 * 60 * i * f4) / (f3 * 1000) + "\n");
}
Intent intent = new Intent(Screen1.this, Screen2.class);
intent.putExtra(EXTRA_MESSAGE, sb.toString());
startActivity(intent);
}
catch (NumberFormatException e)
{
Toast.makeText(Screen1.this, "Invalid numbers.", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
公共类屏幕1扩展活动
{
私有静态最终字符串EXTRA\u MESSAGE=“EXTRA\u msg”;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
最终编辑文本t1=(编辑文本)findViewById(R.id.a);
最终编辑文本t3=(编辑文本)findViewById(R.id.c);
最终编辑文本t4=(编辑文本)findViewById(R.id.d);
按钮bu1=(按钮)findViewById(R.id.bu);
bu1.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(查看消息)
{
int f1、f3、f4;
字符串e1=t1.getText().toString();
字符串e3=t3.getText().toString();
字符串e4=t4.getText().toString();
如果(e1.等于(“”)| e3.等于(“”)| e4.等于(“”)
{
Toast.makeText(屏幕1.this,“请输入所有数字”,Toast.LENGTH_SHORT.show();
返回;
}
尝试
{
f1=整数.parseInt(e1);
f4=整数.parseInt(e4);
f3=整数.parseInt(e3);
StringBuilder sb=新的StringBuilder();
for(float i=0.1f;i什么是t1、t3、t4?它们根本没有初始化。@Binghammer,我忘记了这个问题,但我本想告诉您,空的EditText将返回空字符串,而不是空值。此外,您的if条件中有语法错误。请将此标记为已接受。帮助我们所有的代表:)非常感谢过去几个月我一直在为我的问题寻求解决方案这个答案帮助我,非常感谢