私が作っているアプリには小さなジレンマがあります。基本的に、このアプリはゲーム用で、ゲーム内の各クリーチャーに関する情報を表示します。ゲームには少なくとも 30 体のクリーチャーが存在するため、クリーチャーごとにクラスとレイアウト ファイルを作成し、それぞれに異なる情報を表示できるようにしました。私が尋ねている質問は、1 つの静的クラス ファイルのようなものを持ち、ボタンをクリックして 1 つのクリーチャーのページに移動する方法があるかどうかです。その 1 つのクリーチャーのクラス全体とレイアウト ファイルを作成する代わりに、 1 つの静的クラス内のすべての文字列を必要な情報に変更するだけでしょうか? 私はJavaプログラミングに非常に慣れていないので、いくつかのことしか知りません。コードをもう少しきれいにしたかっただけです。助けてくれてありがとう!
これは Creature のクラス ファイルの 1 つです (これらのうち 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>