暂停从一个活动调用另一活动
我有一些代码可以从主活动中调用活动
Button btnAdd=(Button)findViewById(R.id.button1);
btnAdd.setOnClickListener(new View.OnClickListener() {
EditText Name =((EditText)findViewById(R.id.editText1));
public void onClick(View view) {
if (name1 != "")
{
Intent startSub = new Intent(MainActivity.this,activity2.class);
startSub.putExtra("name", name1);
startActivity(startSub);
} else {
Nm.setError("Enter Name");
}
}
我的问题是,当我单击按钮而不输入名称时,它会启动下一个活动(即使我将意图放入 if 语句中)。 但我想坚持这个活动,直到我输入名称。
有人可以帮助我吗?
I have some code to call an Activity from the main activity
Button btnAdd=(Button)findViewById(R.id.button1);
btnAdd.setOnClickListener(new View.OnClickListener() {
EditText Name =((EditText)findViewById(R.id.editText1));
public void onClick(View view) {
if (name1 != "")
{
Intent startSub = new Intent(MainActivity.this,activity2.class);
startSub.putExtra("name", name1);
startActivity(startSub);
} else {
Nm.setError("Enter Name");
}
}
My problem is when I click the button with out entering the name, its launching the next Activity (even i put intent in if statement).
But I want to stick with this Activity until i enter the name .
Can any one help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
始终为 true,因为 != 不比较字符串,而是比较对象引用。 name1 和 "" 不相等。
您需要使用 equals() 以一种有用的方式比较字符串相等性:
或者在这种情况下: TextUtils.isEmpty(CharSequence str) 甚至更好,因为如果 name1 为 null,它也为 true:
will always be true because != does not compare Strings but the object references. Those are not equal for name1 and "".
You need to use equals() to compare Strings equality in a useful way:
or in this case: TextUtils.isEmpty(CharSequence str) is even better because it also is true if name1 is null: