安卓:从视图返回一个字符串? [英] Android: Return a string from a view?
问题描述
我如何从一个视图字符串?为了更具体我有一个TabView的36个按钮。当我点击按钮,它会调用
How do I get a string from a view? To be more specific I have 36 buttons in a tabview. When I click the button it calls an
android:onClick="onClick"
从我的XML来调用方法的onClick(视图v)
。然后,我想通过一个意图基于点击的按钮另一个活动传递变量。现在我知道我的按钮pressed观是V,我想知道的是如何采取这种观点并使其我可以操纵的字符串。
from my XML to call the method onClick(View v)
. I then want to pass variables via an intent to another activity based on the button clicked. Now I know my View of the button pressed is 'v', what I want to know is how to take that view and make it a string that I can manipulate.
推荐答案
在你的XML定义按钮,对于它设置标记:
In your xml define of Button, set tag for it:
<Button
android:layout_width="wrap_content"
android:layout_height="40dip"
android:text="Btn 1"
android:tag="1"android:onClick="onClick"/>
而在的onClick
函数:
公共无效的onClick(视图v)
And in onClick
function:
public void onClick(View 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);
}
这篇关于安卓:从视图返回一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!