【问题标题】:How to shorten the amount of classes in an Android App如何缩短 Android App 中的类数量
【发布时间】:2013-08-05 20:47:06
【问题描述】:

我在制作应用程序时遇到了一个小难题。基本上,该应用程序是用于游戏的,用于显示游戏中每个生物的信息。游戏中至少有 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>

【问题讨论】:

  • 你能展示一些你的代码吗?
  • 刚刚添加了一些示例

标签: android xml class


【解决方案1】:

我只需要一个本地数据库(或只是一个本地 JSON 文件),其中包含每个生物的数据。然后,创建两个fragments。其中一个将是您的主片段,另一个将是您的详细信息片段。

对于主片段,您可能希望将ListFragment 子类化,以便获得列表功能。然后创建一个ArrayAdapter 将数据库中的生物输入到列表片段中。

对于详细信息片段,创建将显示所选生物详细信息的字段。现在有一个巧妙的技巧:由于这些是片段,您可以针对手机和平板电脑优化您的 UI。在手机上,您一开始只显示主片段,然后等到用户单击将其与细节片段交换。现在,在平板电脑上,您可以并排显示主片段和详细片段:用户可以在左侧选择一个生物并在右侧查看详细信息。

当用户选择一个生物时(无论您是使用手机还是平板电脑),您将使用所选生物的数据填充详细信息片段。为此,您可以加载 JSON 或数据库条目,然后为每个小部件设置内容。

详情请务必查看本期培训班:Building a Dynamic UI with Fragments

【讨论】:

    【解决方案2】:

    首先要使您的 xml 文件可重用,您应该删除所有硬编码字符串,例如:

    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."
    

    您应该将类​​似的字符串放入数据库中,或者,如果您不想要数据库,则放入您可以在 yourProject/res/values 中找到的 strings.xml 文件中。这样做你的 xml 文件将是一个空结构,可重复使用以显示许多生物类。然后你将使用像setText(String string)这样的方法从Bat.java类动态注入生物.xml中的适当字符串。

    对于 java 类的可重用性,问题更复杂,因为我真的不知道你在想什么。我建议您使用一个抽象类,该类将实现所有类通用的所有方法,例如:

    @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.creature_info_structure);
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        inflater.inflate(R.menu.options, menu);
        return true;
    }
    

    进一步尝试设计自己的项目对我来说是不可能的。我唯一能做的就是 建议您阅读有关多态性和继承性的内容,这是 OO 编程的两个原则,这将有助于您设计和开发项目。 希望我能把你引向正确的方向。

    【讨论】:

      【解决方案3】:

      使用 WebKit 浏览器和 html 页面创建一个活动怎么样?你可以在那里拥有一个完整的“Gameopaedia”,其中包含生物、派系的列表。也许你可以只做一个活动来解决这个问题。

      然后,如果您只有大约 3 个屏幕 Statistics、FunFacts 和 Home,并且如果它们显示相同的数据,我将只制作 3 个活动并以某种方式对其进行参数化。

      查看this answer 以了解将参数传递给已启动的活动。

      【讨论】:

      • 问题是,我不认为我可以在里面使用 HTML 页面,因为关于生物的所有信息都在 20 多岁的 XML 文件中的 TextView 中。所以我不认为 HTML 视图会有所帮助。难道只有一种方法可以拥有一个类和一个布局文件,并且根据我点击的生物,它会改变那个布局文件中的 TextView 以匹配我点击的生物吗?必须有办法做到这一点!
      【解决方案4】:

      您只需要遵循“工厂模式”。从任何地方挑选示例并相应地修改您的代码。您可以编写一个抽象工厂类,并可以根据需要进行扩展。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-18
        • 2017-04-14
        • 1970-01-01
        相关资源
        最近更新 更多