Android:点击按钮时如何弹出提示框

4

我对Android还比较新,正在熟悉常见的东西,但是我无法理解onClickListner();我基本上有两个复选框和一个按钮,点击按钮后应该出现一个提示,显示哪些复选框被选中,哪些没有被选中。

public class ExActivity extends Activity implements View.OnClickListener {
    CheckBox cb;
    CheckBox cb2;
    Button buton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cb=(CheckBox) findViewById(R.id.cb);
        cb2=(CheckBox) findViewById(R.id.checkbox);
        buton = (Button)findViewById(R.id.buton);
        buton.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        Toast toast;
        if(cb.isChecked()&&cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Amandoua sunt bifate", Toast.LENGTH_SHORT);
        else if(cb.isChecked()&&!cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Doar prima e bifata", Toast.LENGTH_SHORT);
        else if(!cb.isChecked()&&cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Doar a doua e bifata", Toast.LENGTH_SHORT);
        else if(!cb.isChecked()&&!cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Nici una nu e bifata", Toast.LENGTH_SHORT);
    }
}

忽略罗马尼亚语变量名和文本,XML是正确的。 我也尝试像这样添加onClick():
buton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // my code;
    }
});

但是这个更糟糕。需要帮助吗?

你实际上的困难是什么? - hovanessyan
5个回答

5

两种方式都是正确的。我觉得你只是最后没有显示弹窗,这看起来就像onClick没有执行。

添加:

if(toast != null) {
    toast.show();
}

在您的onClick()方法结尾处加上一个null检查即可。
(如果之前没有满足条件创建Toast实例,则进行null检查)

谢谢,就是这样。看起来我没有完全阅读Toast文档。然而,即使有条件,程序也拒绝运行,并告诉我Toast可能尚未初始化。因此,我只是用随机文本对其进行了初始化,因为我的条件完全覆盖了所有情况。 - FloIancu
没错,你在这种情况下会收到一个警告。你也可以只设置 Toast toast = null。那样做是一样的。 - user658042

4
您需要调用show()来显示提示框:
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();

1

你只是忘记在 .makeText(context,text,duration) 后面加上 .show() 了。

所以你的代码应该像这样:

[...]
if(cb.isChecked()&&cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Amandoua sunt bifate", Toast.LENGTH_SHORT).show();
    else if(cb.isChecked()&&!cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Doar prima e bifata", Toast.LENGTH_SHORT).show();
    else if(!cb.isChecked()&&cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Doar a doua e bifata", Toast.LENGTH_SHORT).show();
    else if(!cb.isChecked()&&!cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Nici una nu e bifata", Toast.LENGTH_SHORT).show();
[...]

好的,其他人已经回答了这个问题,但是你似乎提出了同一方法的不同实现。我很喜欢,谢谢! :) - FloIancu

1

之前的答案是正确的,告诉你不要调用 Toast 的 show() 方法。 你也可以看一下这个tutorial,了解如何在 XML 中为按钮定义处理程序方法。这样代码会更清晰,因为你不必显式地实现 onClickListener 接口或将新的 onClickListener 设置给按钮(这在幕后完成)。以下是一个简单的示例(你可以轻松地用“showToast”方法补充 System 打印):

XML 中的按钮定义:

      <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="Click Me"
    android:onClick="buttonHandler" />

活动类

public class TwoCheckboxesActivity extends Activity {

    private CheckBox check1;
    private CheckBox check2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        check1 = (CheckBox)findViewById(R.id.checkbox1);
        check2 = (CheckBox)findViewById(R.id.checkbox2);

    }

    public void buttonHandler(View view) { 
        System.out.println("Button Clicked"); 

        System.out.println(check1.isChecked());
        System.out.println(check2.isChecked()); 
    }
}

是的,这看起来更加简洁,需要时也更容易实现和修改 :) - FloIancu

0

将此代码添加到您的Java资源中

  public void toast(View v) {
  Toast.makeText(MainActivity.this, "Hey I'm a toast messsage",         
          Toast.LENGTH_LONG).show();

}

然后,这段代码用于添加一个按钮并将其实现为在您的 XML 资源中显示 toast 消息。
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toast Button"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="toast" />

希望这有所帮助。 编程愉快。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接