带有EditText的Java/Android对话框-字符串上的布尔检查

带有EditText的Java/Android对话框-字符串上的布尔检查,java,android,dialog,boolean,android-edittext,Java,Android,Dialog,Boolean,Android Edittext,我有一个返回true或false的布尔方法来检查字符串中是否存在数据。如果用户输入所有数据或未通过对话框运行,则一切正常……但是……如果用户未在“getItemsEditText”对话框弹出窗口中输入数据并仍单击“ok”,则此布尔值将解析为true,即使“pricePerItemText”仍未存储任何内容。这是布尔方法: public Boolean doesAllDataExistCheckBool () { if (pricePerItemText != "" && i

我有一个返回true或false的布尔方法来检查字符串中是否存在数据。如果用户输入所有数据或未通过对话框运行,则一切正常……但是……如果用户未在“getItemsEditText”对话框弹出窗口中输入数据并仍单击“ok”,则此布尔值将解析为true,即使“pricePerItemText”仍未存储任何内容。这是布尔方法:

public Boolean doesAllDataExistCheckBool ()
{

  if (pricePerItemText != "" && itemsPerDayText != "" && sleepTimeText != "" && 
     wakeTimeText != "")
    {

        SharedPreferences.Editor editor = mySharedPreferences.edit
       (); //opens shared preference editor
        editor.putBoolean("storedDoesAllDataExist", true);
        editor.commit();  //commit changes to mySharedPreferences
        //End storing shared preferences
        return true;
    }
    else
    {
        SharedPreferences.Editor editor = mySharedPreferences.edit
        (); //opens shared preference editor
        editor.putBoolean("storedDoesAllDataExist", false);
        editor.commit();  //commit changes to mySharedPreferences
        //End storing shared preferences

        return false;
    }
}
下面是测试布尔值的地方,以查看是真是假:

if (position == 4)
  {
    allDataExists = doesAllDataExistCheckBool ();  //checks if true or false

    if (serviceStarted == true)
     {
       Context context = getApplicationContext();
       String text = "Schedule is already running";
       int duration = Toast.LENGTH_SHORT;
       Toast toast = Toast.makeText(context, text, duration);
       toast.show();
     }
   if (serviceStarted == false && doesAllDataExistCheckBool () == true)
     {
     startScheduleService();
     }
   if (serviceStarted == false && doesAllDataExistCheckBool () == false)
     {
       Context context = getApplicationContext();
       String text = "Please enter all data before starting!";
       int duration = Toast.LENGTH_SHORT;
       Toast toast = Toast.makeText(context, text, duration);
       toast.show();
     }

}
下面是如何编写带有EditText和OK/Cancel按钮的对话框:

case ITEMS_PER_DAY :

LayoutInflater li = LayoutInflater.from(this);

final View itemsEntryView = li.inflate(R.layout.settings_dialog_input, (ViewGroup)
findViewById(R.id.layout_root));

final EditText getItemsEditText = (EditText)itemsEntryView.findViewById
(R.id.DialogEditText);


return new AlertDialog.Builder(SettingsActivity.this)

 .setTitle("This is the title")

 .setView(itemsEntryView)

 .setPositiveButton("Ok", new DialogInterface.OnClickListener()
 {
  public void onClick(DialogInterface dialog, int whichButton)
  {

    itemsPerDayText = getItemsEditText.getText().toString();  //gets input from 
    edittext and saves it to a string itemsPerDayText


    //Initialize shared preferences
    SharedPreferences.Editor editor = mySharedPreferences.edit(); //opens editor
   editor.putString("storedItemsPerDayText", itemsPerDayText); 
   editor.commit();  //commit changes to mySharedPreferences
   //End storing shared preferences

   }
 })
 .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
  public void onClick(DialogInterface dialog, int whichButton)
   {
    //user click cancel
   }
}).create();

还有别的办法吗?如果用户根本没有输入任何内容,为什么仍然可以单击“确定”?有什么想法吗?谢谢大家

你发布的代码太多了。但我马上注意到了这一点

pricePerItemText != ""
假设pricePerItemText是一个字符串,我们真的不知道,因为您没有包括它,这不是java中比较字符串的方式。这是必须的

!pricePerItemText.equals("");
编辑:

在java中,
=
操作符比较对象引用,而不是值。所以

String mytext = "text";
if (mytext == "text"){ print "True"}
永远不会打印true,因为
mytext
变量指向某个内存位置,这肯定与“text”指向的位置不同

事实是

"text == "text"
is true是Java保留字符串池的工件,因此它不必重新分配新字符串。这是造成混乱的主要原因

这里有一个随机链接,它描述的可能更好


谢谢Falmari。很抱歉发布了所有这些代码。我只是想确保无论是谁在看它,都能从逻辑上贯穿整个过程。我知道这是件愚蠢的事。是的,你可能已经知道了。我是一个Java NOOB。@dell116:发布太多和太少的代码之间总是有一种权衡。理想情况下,您应该发布演示问题的最小可编译代码。虽然android有点棘手,但需要澄清的是……虽然我没有在代码中明确说明pricePerItemText是一个字符串,但我确实在描述中指定了我在测试字符串。这是有效的,因为布尔值现在计算正确了。正如我所说,我是Java新手,所以我的语法有时可能有点不正确,但我已经编程大约3年了。我不确定你是否是一个讽刺的线程巨魔,或者你真的想知道我是否理解……在这种情况下……是的……我理解……谢谢你的关心。#dell116:不,他是问你是否理解为什么pricePerItemText!=“”是真的,无论pricePerItemText的值是多少