我正在制作的应用程序有一个小难题。基本上,该应用程序是用于游戏的,用于显示游戏中每个生物的信息。游戏中至少有 30 个生物,所以我为每个生物制作了一个类和布局文件,这样我就可以为每个生物显示不同的信息。我要问的问题是,有没有办法让我拥有一个静态类文件,当我单击一个按钮转到一个生物的页面时,而不是为那个生物创建一个完整的类和布局文件,它只是将一个静态类中的所有字符串更改为我需要的信息吗?我对 Java 编程很陌生,所以我只知道几件事。我只是想让代码更干净一些。谢谢您的帮助!
这是 Creature 的类文件之一(其中有 20 多个完全没有做任何事情)。以下是 Bat.java 的示例:
public class Bat extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String userTheme = prefs.getString("theme", "main");
if (userTheme.equals("main"))
setTheme(R.style.MainTheme);
else if (userTheme.equals("light"))
setTheme(R.style.HoloLight);
else if (userTheme.equals("lightdark"))
setTheme(R.style.HoloLightDark);
else if (userTheme.equals("dark"))
setTheme(R.style.HoloTheme);
setContentView(R.layout.bat);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
inflater.inflate(R.menu.options, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.statistics:
Intent intent0 = new Intent(Bat.this, Statistics.class);
startActivity(intent0);
return true;
case R.id.funfacts:
Intent intent1 = new Intent(Bat.this, FunFacts.class);
startActivity(intent1);
return true;
case R.id.home:
Intent intent2 = new Intent(Bat.this, Home.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这些生物的真实信息在我的 xml 文件中(我也有 20 多个)这是 Bat.xml 的一个示例
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="144dp"
android:src="@drawable/batmc" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overview"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bats are the second smallest mob in the game. They will spawn only in large caves, or with a spawn egg. They may also spawn in a house, considering its dark, and big enough."
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Behavior"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bats sleep during the day, and become active at night. While idle, a bat will hang upside down until a player approaches, which it will then fly away. They cannot hang on non-solid or transparent blocks. If you place a bat in a minecart, it will move the minecart on its own."
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:visibility="invisible" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drops"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="None"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_column="1"
android:text="Spawn - Light level 10 or less"
android:padding="3dip"
android:gravity="left"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_column="1"
android:text="Health - 6"
android:padding="3dip"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_column="1"
android:text="First Appearance - 12w28a"
android:padding="3dip"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Network ID - 65"
android:padding="3dip"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
</LinearLayout>
</ScrollView>