带有 EditText 的 Java /Android 对话框 - 对字符串进行布尔检查
我有一个返回 true 或 false 的布尔方法来检查字符串内部是否存在数据。如果用户输入所有数据或不运行对话框,则一切正常......但是......如果用户未在“getItemsEditText”弹出对话框中输入数据并且仍然单击“确定”,则此布尔值是解析为 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;
}
}
这是测试布尔值以查看 true 或 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();
还有其他方法可以做到这一点吗?为什么用户在没有输入任何内容的情况下仍然可以单击“确定”?有什么想法吗?谢谢你们!
I have a boolean method returning true or false to check whether or not data exists inside of strings. Everything works ok if the user enters all data or does not run through the dialogs.....BUT....if the user DOES NOT enter data in the "getItemsEditText" dialog popup AND still clicks "OK", this boolean is resolving to true, even though "pricePerItemText" still has nothing stored. This is the boolean method:
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;
}
}
Here is where the boolean is being tested to see if true or 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();
}
}
Here is how the dialog with EditText and OK/Cancel buttons is written:
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();
Is there another way to do this? Why can the user still click "OK" if they did not enter anything at all? Any ideas? Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布了太多代码。但我立即注意到
假设 PricePerItemText 是一个字符串,我们真的不知道,因为您没有包含它,这不是在 java 中比较字符串的方式。它需要
编辑:
在java中,
==
运算符比较对象引用,而不是值。因此永远不会打印 true,因为
mytext
变量指向某个内存位置,这绝对与“text”指向的位置不同。事实上,
这是 Java 保留字符串池的一个产物,因此它不必重新分配新字符串。这是造成混乱的一个主要原因。
这是一个随机链接,它可能更好地描述它
http://leepoint.net/notes- java/data/expressions/22compareobjects.html
You posted way too much code. But right away I noticed this
Assuming pricePerItemText is a string, which we really have no idea since you didn't include that, that's not how you compare strings in java. It needs to be
Edit:
In java, the
==
operator compares objects references, not values. Sowill never print true because the
mytext
variable is pointing to some memory location, which is most definitely not the same as where "text" points to.The fact that
is true is a an artifact of Java keeping a string pool so it doesn't have to reallocate new strings. This is a major cause of confusion.
Here's a random link which describes it probably better
http://leepoint.net/notes-java/data/expressions/22compareobjects.html