想要在整个应用程序中保存角色的用户选择

时间:2017-02-10 05:45:04

标签: java android

这是我的第一个应用程序,我几乎没有编程知识。但是,当应用程序打开时,系统会要求用户从滚动视图中选择一个角色,这些角色将在整个应用程序的其余部分中使用。如何让这个选定的角色显示在其他活动屏幕上?

以下是选择字符活动的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>

我希望用户能够在应用程序的未来屏幕上看到所选图像按钮的图像。感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

既然你说自己对编程很陌生,我会在这里详细介绍很多基本细节。

将数据发送到另一个活动很容易 - 当您创建用于启动它的Intent时,您将数据添加为Extra。

那么问题就是要添加什么。一般来说,保存这样的数据就是定义一个类Character。该类将包含有关字符的所有重要信息 - 名称,图像URL等。它还可以对此字符执行操作 - 可能是更改名称或设置其父级名称的函数,或者做任何你需要做的事情。这种技术称为面向对象编程。您通常会为每个按钮创建此类的实例。然后,当按下其中一个按钮时,您将存储该实例。稍后,当您启动下一个Activity时,您将其添加到我之前说过的Intent中。

请记住,要通过Intent传递类,它需要实现Parcelable或Serializable。我建议只使用Serializable-它有点慢,但是这个类小到可以无关紧要并且更容易实现(通常你甚至不需要覆盖任何函数)。