2011-05-07 81 views
0

在我的Android應用程序中,我點擊「隨機」按鈕,應用程序向我顯示first.xml,second.xml或third.xml中的隨機xml文件。這工作都很好,但我想知道是否有更容易或更好的方式來實現它,如果你有100 + XML文件。在Android中顯示隨機xml文件

我發現了一些代碼(正常工作),顯示隨機ImageViews:

private Integer [] mImageIds = { 
     R.drawable.one, 
     R.drawable.two, 
     R.drawable.three, 
     }; 
    private static final Random rgenerator = new Random(); 

    private ImageView iv; 

    . 
    . 
    . 

    Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)]; 

    iv = (ImageView) findViewById(R.id.imageviewyeah); 
    iv.setImageResource(q); 

這是我的main.xml的樣子:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<Button 
     android:id="@+id/first_button" 
     android:onClick="change" 
     android:text="first" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
     /> 
<Button 
     android:id="@+id/second_button" 
     android:onClick="change" 
     android:text="second" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
     /> 
<Button 
     android:id="@+id/third_button" 
     android:onClick="change" 
     android:text="third" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
     /> 
<Button 
     android:id="@+id/random_button" 
     android:onClick="change" 
     android:text="random" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
     /> 

,這是代碼當您點擊一個按鈕時將處理該圖像:

package com.random; 

    import java.util.Random; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 

    public class Main extends Activity { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
} 

public void change(final View view){ 
    switch (view.getId()) { 
    case R.id.first_button: 
     startActivity(new Intent(this, FirstPage.class)); 
     break; 
    case R.id.second_button: 
     startActivity(new Intent(this, SecondPage.class)); 
     break; 
    case R.id.third_button: 
     startActivity(new Intent(this, ThirdPage.class)); 
     break; 
    case R.id.random_button: 

     Random random = new java.util.Random(); 
     int rand = random.nextInt(3); 

     switch (rand) { 
     case 0: 
      startActivity(new Intent(this, FirstPage.class)); 
      break; 
     case 1: 
      startActivity(new Intent(this, SecondPage.class)); 
      break; 
     case 2: 
      startActivity(new Intent(this, ThirdPage.class)); 
      break; 
      } 
     } 
    } 
} 

任何幫助非常感謝!


現在我有一個相當類似的問題。到目前爲止,我實現了一個「隨機」按鈕。如果你點擊它,將會顯示一個隨機的xml文件。注意:內容(TextViews,ImageViews)在xml文件中是不同的,但是java代碼(點擊按鈕等)是相同的!

那是,如果你點擊「隨機」按鈕上的代碼:

switch (view.getId()) { 
    case R.id.first_button: 
     startActivity(new Intent(this, FirstPage.class)); 
     break; 
    case R.id.second_button: 
     startActivity(new Intent(this, SecondPage.class)); 
     break; 
    case R.id.third_button: 
     startActivity(new Intent(this, ThirdPage.class)); 
     break; 
    case R.id.random_button: 
     Intent intent = new Intent(this, DisplayRandomPage.class); 
     startActivity(intent); 

,這是在DisplayRandomPage.class

public class DisplayRandomPage extends Activity { 

private Integer [] mLinearLayoutIds = { 
     R.layout.one 
     R.layout.two, 
     R.layout.three 
     }; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    Random random = new java.util.Random(); 
    int rand = random.nextInt(3); 

    setContentView(mLinearLayoutIds[rand]); 
      } 
    } 

我希望做的是創造一個DisplaySpecificPage.class。上面我用switch-case-clause展示了我的main.class。所以當我點擊第一個按鈕時,它將啓動FirstPage.class,單擊第二個按鈕,它將啓動SecondPage.class,等等。因此,對於每個xml文件,我必須創建一個新的java類,儘管不同的java類完全相同。只有xml文件是不同的。所以我喜歡把這樣的事情:

僞代碼:

case R.id.first_button: 
    startActivity(new Intent(this, DisplaySpecificPage.class)) with R.layout.first_page; 
    break; 

我如何從佈局(R.layout.first_page)通過ID?

回答

1

如果你的類都做同樣的事情,除了有不同的佈局叫,你可以創建一個單一的顯示類

public class DisplayPage extends Activity 

,並在意向額外發送佈局的ID。

你可以做這樣的事情

Class c = Class.forName("com.random.R$layout"); 
Integer iLayout = new Integer(c.getField("layout"+rand).getInt(new R.layout())); 

(假設你的佈局獲得ID被稱爲layout1.xml,layout2.xml等。)

它發送給DisplayPage,

Intent intent = new Intent(this, DisplayPage.class); 
intent.putExtra("Layout", iLayout); 
startActivity(intent); 

,並通過執行類似

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // default value 
    int iLayout = R.id.main; 

    if (savedInstanceState != null) 
    { 
    iLayout = savedInstanceState.getInt("Layout"); 
    } 
    else 
    { 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) 
    { 
     iLayout = extras.getInt("Layout"); 
    } 
    } 

    setContentView(iLayout); 

} 

您也想覆蓋到的onSaveInstanceState包括INT拿回來,這樣,例如,改變屏幕方向不會讓它忘記它顯示的內容。


try 
{ 
    Class c = Class.forName("com.random.R$layout"); 
    Field[] aFields = c.getFields(); 

    Random random = new Random();  
    boolean isUsableLayout = false; 
    Integer iLayout = 0; 

    while (!isUsableLayout) 
    { 
    int rand = random.nextInt(aFields.length);  
    iLayout = new Integer(c.getField(aFields[rand].getName()).getInt(new R.layout())); 

    if (iLayout != R.layout.main) 
    { 
     isUsableLayout = true; 
    } 
    } 

    Intent intent = new Intent(this, DisplayPage.class); 
    intent.putExtra("Layout", iLayout); 
    startActivity(intent); 
} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 
+0

本嗨! 我做了你發佈的內容,它的工作原理!但是我將這三個樣本佈局重新命名爲layout1,layout2 ......實際上我的佈局沒有這樣的相似名稱。我正在考慮做這樣的事情: private Integer [] mLinearLayoutIds = { R.id.one, R.id.two, R.id.three, }; 你有線索這將如何工作? – BenjaminButton 2011-05-07 11:42:51

+0

您可以使用Field [] afFields = c.getFields()將所有佈局ID作爲Fields數組返回,隨機選擇其中一個,檢查它是不是您不想使用的佈局(例如,主要),並使用它。否則,您將不得不手寫所有數百個佈局ID到您的陣列中。 – 2011-05-07 12:32:58

+0

再次嗨!似乎我在這裏卡住了。從未真正使用過Fields。所以這是我的switch-case-clause「now」中的代碼片段:class c = Class.forName(「com.random.R $ layout」); java.lang.reflect.Field [] afFields = c.getFields();'現在我不知道如何將它傳遞給DisplayPage.class,並且不知道如何在那裏使用它。最後id想知道如果我不得不檢查佈局,我不想顯示(例如主)如果我不給佈局一個ID或我完全錯了。如果是這樣,我該如何檢查不需要的佈局?像這樣:'(if afFields == main)then then randomize'Thanks !!! – BenjaminButton 2011-05-07 14:35:18