In my Android app i click on the "random" button and the app shows me a random xml file out of first.xml, second.xml or third.xml. This works all fine but i wondered if there is an easier or a nicer way to implement it if you have 100+ xml files.
I found some code (which works fine) which shows random 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);
This is how my main.xml looks:
<?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"
开发者_Go百科 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"
/>
and this is the code which is processed when you click on a button:
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;
}
}
}
}
Any help is much appreciated!
Now I've got a fairly similar question. So far i implemented a "Random"-button. If you click on it a random xml-file will be shown. Note: The content (TextViews, ImageViews) are different in the xml-files but the java code (clicking on buttons etc.) is the same!
That's the code if you click on the "Random"-button:
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);
and this is in the 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]);
}
}
What i'd like to do is creating a DisplaySpecificPage.class. Above I've shown my main.class with the switch-case-clause. So when i click on the first button, it will start the FirstPage.class, clicking the second, it will start SecondPage.class, and so on. So for each xml-file i have to create a new java-class although the different java-classes do all the same. Only the xml-files are different. So i'd like to put something like this:
pseudo-code:
case R.id.first_button:
startActivity(new Intent(this, DisplaySpecificPage.class)) with R.layout.first_page;
break;
how do i pass the ID from the layout (R.layout.first_page) on?
If your classes all do the same thing except have a different layout called, you could create a single display class
public class DisplayPage extends Activity
and send it the id of the layout in the intent extras.
You can get the id by doing something like
Class c = Class.forName("com.random.R$layout");
Integer iLayout = new Integer(c.getField("layout"+rand).getInt(new R.layout()));
(assuming your layouts are called layout1.xml, layout2.xml etc.)
Send it to your DisplayPage,
Intent intent = new Intent(this, DisplayPage.class);
intent.putExtra("Layout", iLayout);
startActivity(intent);
and get it back by doing something like
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);
}
You'll also want to override onSaveInstanceState to include that int so that, e.g., changing the screen orientation doesn't make it forget what it was showing.
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();
}
精彩评论