暂停从一个活动调用另一活动

发布于 2025-01-04 16:11:45 字数 547 浏览 0 评论 0原文

我有一些代码可以从主活动中调用活动

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2025-01-11 16:11:45
name1 != ""

始终为 true,因为 != 不比较字符串,而是比较对象引用。 name1 和 "" 不相等。

您需要使用 equals() 以一种有用的方式比较字符串相等性:

if (!"".equals(name1)) {

或者在这种情况下: TextUtils.isEmpty(CharSequence str) 甚至更好,因为如果 name1 为 null,它也为 true:

if (!TextUtils.isEmpty(name1)) {
name1 != ""

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:

if (!"".equals(name1)) {

or in this case: TextUtils.isEmpty(CharSequence str) is even better because it also is true if name1 is null:

if (!TextUtils.isEmpty(name1)) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文