2011-04-15 88 views
1

如何從視圖中獲取字符串?更具體地說,我在tabview中有36個按鈕。當我點擊按鈕,它會調用Android:從視圖中返回一個字符串?

android:onClick="onClick" 
從我的XML調用的方法 onClick(View v)

。然後,我想通過意圖將變量傳遞給另一個基於點擊按鈕的活動。現在我知道我按下的按鈕視圖是'v',我想知道的是如何把這個視圖做成一個我可以操作的字符串。

+0

你是說你想將按下按鈕的信息傳遞給另一個活動?通過將視圖轉換爲字符串,我不明白你的意思。 – Albinoswordfish 2011-04-15 03:55:28

回答

0

在XML定義按鈕的設置標籤吧:

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

而且在onClick功能: 公共無效的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); 
} 
+0

轟!這就是我想要的!謝謝! – talitore 2011-04-16 01:04:23

1

你的意思是?它是明確的類鑄造,Java語言功能。

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

這將是我選擇的,但我的按鈕是imageButtons,所以我不得不使用標籤。感謝您的幫助! – talitore 2011-04-16 01:09:12