在我的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?
本嗨! 我做了你发布的内容,它的工作原理!但是我将这三个样本布局重新命名为layout1,layout2 ......实际上我的布局没有这样的相似名称。我正在考虑做这样的事情: private Integer [] mLinearLayoutIds = { R.id.one, R.id.two, R.id.three, }; 你有线索这将如何工作? – BenjaminButton 2011-05-07 11:42:51
您可以使用Field [] afFields = c.getFields()将所有布局ID作为Fields数组返回,随机选择其中一个,检查它是不是您不想使用的布局(例如,主要),并使用它。否则,您将不得不手写所有数百个布局ID到您的阵列中。 – 2011-05-07 12:32:58
再次嗨!似乎我在这里卡住了。从未真正使用过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