У меня есть небольшая дилемма с приложением, которое я делаю. В основном, приложение предназначено для игры, для отображения информации о каждом существе в игре. В игре есть не менее 30 существ, поэтому я создал файл класса и макета для каждого существа, чтобы я мог отображать различную информацию для каждого из них. Вопрос, который я задаю, заключается в том, есть ли у меня способ иметь один статический файл класса и когда я нажимал кнопку, чтобы перейти на страницу одного существа, вместо того, чтобы создавать целый класс и файл макета для этого существа, это просто изменит все строки в одном статическом классе на нужную мне информацию? Я очень новичок в программировании на Java, поэтому знаю только несколько вещей. Я просто хотел сделать код немного чище. Спасибо за помощь!Как сократить количество классов в приложении для Android
Это одно из файлов классов существа (более 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>
Вы могли бы показать код? – nio
Просто добавил несколько примеров – user1798956