如何在editText上留下“空白”时阻止关闭模拟器?

问题描述 投票:-1回答:2

我在Android工作室制作了一个简单的应用程序,它包含两个数字。它成功地给了我结果,但问题是当我把数字和另一个editText部分留空时,模拟器会自动关闭。我怎么能解决这个问题,例如,如果我按下第一个数字10而另一个留在“空白”,当我按下add时应该给10,而不是关闭模拟器。请帮我。

enter image description here

码:

package com.example.victo.test.feature;

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.TextView;

public class MainActivity extends AppCompatActivity {

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

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

    // Example of a call to a native method

    Button addButton = (Button)findViewById(R.id.Addbutton);
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            EditText firtsNumberEditText = (EditText) 
 findViewById(R.id.firstNumberEditText);
            EditText secondNumberEditText = (EditText) 
 findViewById(R.id.secondNumberEditText);

            TextView resultTextView = (TextView) 
 findViewById(R.id.resultTextView);

            int n1 = 
 Integer.parseInt(firtsNumberEditText.getText().toString());
            int n2 = 
 Integer.parseInt(secondNumberEditText.getText().toString());

            int result = n1 + n2;


            resultTextView.setText(result +"");
        }

    });

}

/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */

 }

感谢您的回复,它使用以下代码:enter image description here

java android
2个回答
0
投票

尝试使用此代码,如果edittext为null,则必须添加条件。

private EditText firtsNumberEditText,secondNumberEditText;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method

        Button addButton = (Button)findViewById(R.id.Addbutton);
        firtsNumberEditText = (EditText) findViewById(R.id.firstNumberEditText);
        secondNumberEditText = (EditText) findViewById(R.id.secondNumberEditText);
        TextView resultTextView = (TextView) findViewById(R.id.resultTextView);

        addButton.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {

                int n1, n2;

                if (firtsNumberEditText.getText().toString().isEmpty() || firtsNumberEditText.getText().toString() == "")
                    n1 = 0;
                else n1 = Integer.parseInt(firtsNumberEditText.getText().toString());

                if (secondNumberEditText.getText().toString().isEmpty() || secondNumberEditText.getText().toString() == "")
                    n2 = 0;
                else n2 = Integer.parseInt(secondNumberEditText.getText().toString());

                int result = n1 + n2;

                resultTextView.setText(result +"");
            }

        });

    }

0
投票

更改您的代码如下 -

if (firtsNumberEditText.getText().toString().isEmpty() || secondNumberEditText.getText().toString().isEmpty())
{
    Toast.makeText(this,"Please Enter both Number", Toast.LENGTH_SHORT).show();
}else{
    int result = n1+n2;
}

resultTextView.setText(result +"");

或者你可以在下面使用

if (firtsNumberEditText.getText().toString().isEmpty() || firtsNumberEditText.getText().toString() == "")
                n1 = 0;
            else n1 = Integer.parseInt(firtsNumberEditText.getText().toString());

            if (secondNumberEditText.getText().toString().isEmpty() || secondNumberEditText.getText().toString() == "")
                n2 = 0;
            else n2 = Integer.parseInt(secondNumberEditText.getText().toString());

            int result = n1 + n2;

            resultTextView.setText(result +"");
© www.soinside.com 2019 - 2024. All rights reserved.