2012-08-17 65 views
-3

我是新的android.I不理解如何添加與相應的選定圖像更改按鈕的textview。任何人都可以幫助我通過正確的code.How可以做到這一點嗎? 任何幫助是高度讚賞。textview與按鈕在畫廊

+1

用按鈕添加textview?那是什麼意思?請讓你的要求更清楚些,以便我們解決你的問題。 – RyanInBinary 2012-08-17 17:43:23

回答

0

取消您的迴應,設置一個集合或一個數組列表來保存您的對象,然後根據您點擊的內容來引用它們。

如果圖像是活動中唯一的對象,以下是如何使用單個圖像執行此操作的方法。

XML:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/image" 
     android:src="@drawable/YOUR_IMAGE_RESOURCE" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Text for the picture" 
     android:visibility="invisible" 
     android:id="@+id/text" 
     /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello" 
     android:id="@+id/button" 
     android:visibility="invisible" 
     /> 
</LinearLayout> 

的Java

ImageView photoImage = (ImageView)findViewById(R.id.image); 
TextView text = (TextView)findViewById(R.id.text); 
Button button = (Button)findViewById(R.id.button); 

photoImage.setOnClickListener(new OnClickListener{ 
    void onClick(View v){ 
     text.setVisibility(View.VISIBLE); 
     button.setVisibility(View.VISIBLE); 
    } 
}); 

// Set the OnClickListener for "button" here, to start a new intent or whatever 

如果您正在使用多個按鈕,設置一個ArrayList或集合像我上面所述。

[編輯]

你的代碼,把東西放到一個數組列表如下所示...

List<TextView> tvs = new ArrayList<TextView>(); 
// Setup the views 
for (int i = 0; i < numberOfTextViews; i++){ 
    TextView thisTextView = new TextView(context); 
    // Setup whatever layout info you want here 
    tvs.add(thisTextView); 
} 
// Use a view inflater here to add each of the textviews however you want (table layout, linearlayout, etc.) 

基礎上,無論你想,你可以再做...只是使用tvs.get(NUMBER)來攔截你正在尋找的任何東西。您可以使用按鈕或其他任何方式執行相同的操作,並使用相同的NUMBER同時參考字符串,文本瀏覽和按鈕。例如,點擊按鈕編號1 ...您可以使用tvs.get(1)buttons.get(1),stringResources.get(1)

這樣做更有意義嗎?

+0

我更新了我的代碼 – RyanInBinary 2012-09-04 14:49:23