0

私が作っているアプリには小さなジレンマがあります。基本的に、このアプリはゲーム用で、ゲーム内の各クリーチャーに関する情報を表示します。ゲームには少なくとも 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>
4

4 に答える 4

0

WebKit ブラウザーと内部の html ページで 1 つのアクティビティを作成するのはどうですか? クリーチャーや派閥のリストを含む「Gameopaedia」全体をそこに置くことができます。たぶん、これはたった1つのアクティビティで乗り越えることができます.

次に、Statistics、FunFacts、および Home の約 3 つの画面があり、それらが同じデータを表示する場合、3 つのアクティビティのみを作成し、それらを何らかの方法でパラメーター化します。

起動されたアクティビティへのパラメーターの受け渡しについては、この回答を確認してください。

于 2013-08-05T08:13:32.270 に答える
0

各クリーチャーのデータを含むローカル データベース (または単にローカル JSON ファイル) を用意するだけです。次に、2 つのフラグメントを作成します。そのうちの 1 つがマスター フラグメントになり、もう 1 つが詳細フラグメントになります。

マスター フラグメントの場合、おそらくListFragmentをサブクラス化して、リスト機能を取得する必要があります。次に、ArrayAdapter を作成して、クリーチャーをデータベースからリスト フラグメントにフィードします。

詳細フラグメントでは、選択したクリーチャーの詳細を表示するフィールドを作成します。これらはフラグメントであるため、携帯電話やタブレット向けに UI を最適化できます。電話では、最初はマスター フラグメントのみを表示し、ユーザーがクリックして詳細フラグメントと交換するまで待ちます。ここで、タブレットで、マスター フラグメントと詳細フラグメントを並べて表示します。ユーザーは左側でクリーチャーを選択し、右側で詳細を確認できます。

ユーザーがクリーチャーを選択すると (電話またはタブレットを使用しているかどうかに関係なく)、詳細フラグメントに選択したクリーチャーのデータが入力されます。これを行うには、JSON またはデータベース エントリをロードしてから、各ウィジェットのコンテンツを設定します。

詳細については、このトレーニング クラスを必ずご覧ください:フラグメントを使用した動的 UI の構築

于 2013-08-05T17:40:33.850 に答える
0

「ファクトリーパターン」に従うだけです。どこからでも選択して例を示し、それに応じてコードを変更します。抽象ファクトリ クラスを記述し、必要に応じて拡張できます。

于 2013-08-05T07:59:00.487 に答える