android 如果editText为空,如何吐司消息?

x6492ojm  于 2023-01-19  发布在  Android
关注(0)|答案(4)|浏览(141)

我正在尝试在单击按钮时显示一条消息,以告知用户填写空白字段。目前,如果字段为空,则会崩溃/强制关闭应用程序。我需要的条件是具有摄氏度/华氏度选项的微调器,如果微调器位于同一选项上或值为空,则会显示提示消息
下面是代码

public class MainActivity extends AppCompatActivity {

        private TextView result;
        private EditText enterTemp;
        double result0;

        Spinner ConvertFromUnitTypeSpinner;
        Spinner ConvertToUnitTypeSpinner;

        @Override

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

        //set spinner variables

        ConvertFromUnitTypeSpinner = (Spinner) findViewById(R.id.ConvertFromUnitTypeSpinner);
        ConvertToUnitTypeSpinner =  (Spinner) findViewById(R.id.ConvertToUnitTypeSpinner);

        //String for spinner items
        String [] unitTypes =  {"Fahrenheit", "Celsius"};

        //set up adapter for the layout of the spinner
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,      
         android.R.layout.simple_list_item_1, unitTypes);

        //set up adapter to spinner
        ConvertFromUnitTypeSpinner.setAdapter(spinnerAdapter);
        ConvertToUnitTypeSpinner.setAdapter(spinnerAdapter);

        //set up convert button
        Button convert = findViewById(R.id.convert);
        result = findViewById(R.id.result);
        enterTemp = findViewById(R.id.enterTemp);

        convert.setOnClickListener(new View.OnClickListener(){
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick (View v) {
                double temp = Double.parseDouble(enterTemp.getText().toString());

            if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Fahrenheit"&&
             ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Celsius")
                {
                    result0 = (temp - 32 )*5/9;
                    result.setText(String.valueOf(result0));
                }
           if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Celsius"
              &&ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Fahrenheit")
                {
                    result0 = (temp * 9/5) + 32;
                    result.setText(String.valueOf(result0));
                }
            if (ConvertToUnitTypeSpinner.getSelectedItem()
                ==ConvertToUnitTypeSpinner.getSelectedItem()){
                    result.setText("Units are the same!");
                }
                if (enterTemp.getText().toString().length() == 0)
                {
                    Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
                  }

                }
              });
             }
           }

我试着把密码

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

到这个

if(isEmpty(enterTemp.getText().toString())){
            Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
         }

也没有运气

11dmarpk

11dmarpk1#

首先,你应该检查EditText是否为空,并在其他部分做你的转换:

if (enterTemp.getText().toString().isEmpty())
   {
     Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
} else {
         if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Fahrenheit"&&
                   
         ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Celsius")
            {
                 result0 = (temp - 32 )*5/9;
                 result.setText(String.valueOf(result0));
            }
            if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Celsius"
                  
          &&ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Fahrenheit")
             {
                  result0 = (temp * 9/5) + 32;
                  result.setText(String.valueOf(result0));
             }
              if (ConvertToUnitTypeSpinner.getSelectedItem()
                        ==ConvertToUnitTypeSpinner.getSelectedItem())
               {
                    result.setText("Units are the same!");
                }
     }
hpxqektj

hpxqektj2#

您应该使用TextUtils来实现您想要的结果。

import android.text.TextUtils;

if ( TextUtils.isEmpty(enterTemp.getText()) ) {
   // Display the toast
}
uklbhaso

uklbhaso3#

您的代码将是:

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

s4n0splo4#

试试这个-〉

if (enterTemp.getText().toString().equals("")) {
            Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
        }

相关问题