2013-10-27 41 views
0

我正在製作一個包含超過200個按鈕的Android應用程序............ 每個按鈕都會響應一個活動...........每個活動都包含一個按鈕,文本查看和和圖像............ 是否有任何簡短的方式,以便我可以擺脫這些許多活動,並有一個按鈕和一個圖像的單一活動。而當我點擊一個按鈕,那麼它會在單個活動中顯示一個按鈕的相應圖像。 我包含兩個按鈕的主要活動命名爲烏爾都語和英語...........如何在android中進行單個活動來處理許多活動?

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/main_first_background" 
    android:gravity="center" 
    android:orientation="vertical" > 

<Button 
    android:id="@+id/urduForLang" 
    android:layout_width="200dp" 
    android:layout_height="80dp" 
    android:layout_gravity="center" 
    android:text="URDU" 
    android:textColor="@android:color/background_light" > 
</Button> 

<Button 
    android:id="@+id/EnglishForLang" 
    android:layout_width="200sp" 
    android:layout_height="80dp" 
    android:layout_below="@+id/urduForLang" 
    android:layout_gravity="center" 
    android:layout_marginTop="2dp" 
    android:text="English" 
    android:textColor="@android:color/background_light" > 
</Button> 
</RelativeLayout> 

當烏爾都語用戶點擊它然後打開包含99個鍵的活動,並點擊英語時,它打開使用英語中的相同按鈕啓動一項活動。 因此,我必須做出99個不同的活動......我真的不想要。

當用戶觸摸任何按鈕然後...那麼該活動與尊重圖像和按鈕將被顯示。 對不起,我的英語.................

回答

0

您應該使用Intents並將一些參數傳遞給Intent中的Activity。你可以傳遞你的圖片ID,然後在Activity中讀取它並決定顯示哪個圖片。

take a look here.

0

您應該使用Intents但它可能是一個更爲有用,如果你可以創建一個根據其paremeters充當一個不同的活動。這樣,您可以使用Intent.putExtra(...)在Intent對象中設置參數,然後在已調用的Activity中使用getIntent().getExtras()。請注意,方法getExtras()會返回一個Bundle對象,因此您需要單獨提取您從該對象的主要活動發送的所有值。祝你好運!

0

創建單個活動並在運行時爲該活動生成視圖。通過這種方式,您可能需要編寫200個條件,但實際上您將爲200個活動節省代碼。