【问题标题】:radio button activity source code单选按钮活动源代码
【发布时间】:2012-12-28 09:04:03
【问题描述】:

此方法在单击按钮时调用,因为已为 按钮的“OnClick属性”,下面的代码中大小写的正确代码应该是什么。它给出的错误应该是大小写的常量表达式。

public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
  RadioButton AButton = (RadioButton) findViewById(R.id.radio0);
  RadioButton BButton = (RadioButton) findViewById(R.id.radio1);
  if (text.getText().length() == 0) {
    Toast.makeText(this, "Please enter a valid number",
        Toast.LENGTH_LONG).show();
    return;
  }

【问题讨论】:

标签: android radio-button android-activity


【解决方案1】:

您必须在将被标记为的项目中使用此代码。是吗?

如果是,那么您必须了解在这种情况下 view.getId() 值不能用作常量。 在此类项目中,您可以使用 if else 语句。

这背后的原因是,如果你在主项目中定义了一个具有相同 id 的资源,则会返回或初始化错误的结果。 所以,这是为了避免这种情况,因为它会更难以调试。

使用 if - else 语句。

另外,我不记得 ADT 版本,但之后视图 ID 不被视为常量。

【讨论】:

    【解决方案2】:

    只需将您的 switch case 语句转换为 if 语句就足够了。解释阅读switch case statement error: case expressions must be constant expression

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 2012-03-04
      • 2012-09-23
      • 2012-07-22
      • 2015-07-15
      • 2011-11-02
      • 2019-07-16
      • 1970-01-01
      • 2014-05-14
      相关资源
      最近更新 更多