【问题标题】:Android: Return a string from a view?Android:从视图中返回一个字符串?
【发布时间】:2011-04-15 03:46:40
【问题描述】:

如何从视图中获取字符串?更具体地说,我在 tabview 中有 36 个按钮。当我单击按钮时,它会调用一个

android:onClick="onClick"

从我的 XML 调用方法 onClick(View v)。然后,我想根据单击的按钮通过意图将变量传递给另一个活动。现在我知道按下按钮的视图是'v',我想知道的是如何获取该视图并使其成为我可以操作的字符串。

【问题讨论】:

  • 您是说要将有关按下按钮的信息传递给另一个活动吗?我不太明白您将 View 转换为字符串是什么意思。

标签: android view onclick android-intent


【解决方案1】:

你是这个意思吗?它是显式类转换,一种 Java 语言功能。

public void onClick(View v)
{
    Button button = (Button) v;
    String info = button.getText();
    Intent intent = new Intent();
    .....
}

【讨论】:

  • 这本来是我选择的,但我的按钮是 imageButtons,所以我不得不使用标签。不过感谢您的帮助!
【解决方案2】:

在 Button 的 xml 定义中,为其设置标签:

<Button
    android:layout_width="wrap_content"
    android:layout_height="40dip"
    android:text="Btn 1"
    android:tag="1"android:onClick="onClick"/>

onClick 函数中: public void onClick(查看 v)

{
    Button button = (Button) v;
    String tag = button.getTag.toString();
    //now open new Activity with this tag
    Intent intent = new Itent();
    Bundle b = new Bundle();
    b.putString("tag", tag);
    intent.putExtras(b);
    startActivity(intent);
}

【讨论】:

  • 轰隆隆!这就是我想要的!谢谢!
猜你喜欢
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多