2017-02-10 4 views
1

これは私の最初のアプリです。プログラミングに関する知識はほとんどありません。ただし、アプリを開いたときに、ユーザーはスクロールビューから文字を選択して、残りのアプリで使用することになります。この選択されたキャラクターを他の活動画面に表示させるにはどうすればよいですか?ここでアプリケーション全体で文字のユーザー選択を保存したい

は、選択した文字の活動のためのXMLです:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_choose_pet" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.mc.cibi.ChoosePet"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:text="Please Choose Your CiBi Pet!" 
    android:ems="10" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="522dp" 
    android:gravity="center"/> 

<HorizontalScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="12dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:weightSum="1"> 

     <!-- <ImageButton 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:background="@drawable/gifraffe" 
      android:layout_weight="0.01" 
      android:id="@+id/giraffe" 
      android:onClick="giraffebtn"/>--> 

     <ImageButton 
       android:layout_width="300dp" 
       android:layout_height="300dp" 
       android:background="@drawable/goat" 
       android:id="@+id/goat" 
       android:onClick="goatbtn" 
      android:layout_gravity="center"/> 

     <Space 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_weight="1" /> 

     <ImageButton 
      android:layout_width="300dp" 
      android:layout_height="300dp" 
      android:background="@drawable/monkey" 
      android:id="@+id/monkey" 
      android:onClick="monkeybtn" 
      android:layout_gravity="center" 
      /> 

     <Space 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_weight="1" /> 

     <ImageButton 
      android:layout_width="300dp" 
      android:layout_height="300dp" 
      android:background="@drawable/moose" 
      android:id="@+id/moose" 
      android:onClick="moosebtn" 
      android:layout_gravity="center"/> 
     <Space 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_weight="1" /> 

     <ImageButton 
      android:layout_width="300dp" 
      android:layout_height="300dp" 
      android:background="@drawable/owl" 
      android:id="@+id/owl" 
      android:onClick="owlbtn" 
      android:layout_gravity="center"/> 

     <Space 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_weight="1" /> 

     <ImageButton 
      android:layout_width="300dp" 
      android:layout_height="300dp" 
      android:background="@drawable/rhino" 
      android:id="@+id/rhino" 
      android:onClick="rhinobtn" 
      android:layout_gravity="center"/> 


    </LinearLayout> 





</HorizontalScrollView> 

</RelativeLayout> 

私は、ユーザーがアプリケーション内で将来の画面上で自分の選択した画像ボタンの画像を見ることができるようにしたいです。すべての助けに感謝します!

+0

彼は最後にこれを必要とする場合 –

+0

@PratikMohanraoGondil共有の好みはOKです、この[リンク](http://androidexample.com/Android_SharedPreferences_Basics/index.php?view=article_discription&aid=126)を訪問発射の間に、彼がしなければ過度の力を発揮する。そして彼がそうしても、彼はURLを保存するべきではなく、選択肢の識別子を保存して、キャラクターについてのすべてのデータを再び得ることができます。それで、彼は起動の間に最後にする必要がある場合、おそらく共有の設定を使用するよりもディスクにシリアル化する方が良いでしょう。 –

答えて

0

あなたはプログラミングにとって全く新しいと言いますから、ここでは多くの基本的な詳細について説明します。

他のアクティビティにデータを送信するのは簡単です。インテントを作成する際に作成するときに、そのデータをエクストラとして追加します。

質問は次に追加するものです。一般に、このようなデータを保持するためには、クラスCharacterを定義する必要があります。このクラスは、名前、画像URLなどの文字に関する重要な情報をすべて保持します。名前を変更したり、親の名前を設定したりすることもできます。あなたがそれにする必要があるものは何でもすることができます。この技術はオブジェクト指向プログラミングと呼ばれています。あなたは、一般的に、各ボタンのためにこのクラスのインスタンスを作成します。次に、いずれかのボタンを押すと、そのインスタンスを保存します。後で、次のアクティビティを起動すると、前に述べたように、そのアクティビティをインテントに追加します。

Intentを介してクラスを渡すには、ParcelableまたはSerializableのいずれかを実装する必要があります。私はちょうどSerializableを使用することをお勧めします - それは少し遅いですが、クラスは問題ではないほど小さく、実装するのがはるかに簡単です(一般にあなたは関数をオーバーライドする必要はありません)。あなたが使用して共有preferncesで行うことができます

関連する問題