0

这是我第一次尝试制作 android 应用程序,或处理 java,所以请耐心等待我的问题:(

我想要做的是让屏幕 1 带有几个按钮,当按下时,它将把用户带到带有另一组按钮的屏幕 2。在屏幕 2 中按下按钮后,用户将被带到一个新屏幕,其中包含一些基于他按下的按钮和顺序的文本。

每个屏幕中的其他按钮也是如此。

我已经拥有的是充满按钮的屏幕 1 和它们的 ID。我不知道从哪里开始下一部分。我可以为 screen-1 中的每个按钮创建一个 onClick 活动,但是 screen-3 将如何记住在 screen-1 和 screen-2 上按下了哪些按钮?

activity_main.xml

<Button 
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button1"
            android:onClick="button1OnClick"/>
                    <Button 
                    android:id="@+id/button2"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button2"
                    android:onClick="button1OnClick2"/>

MainActivity.java

public class Click extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main.xml);
        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // open up screen 2? do i use intent??
        }
    });
}

}

activity_main2.xml

<Button 
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button1"
            android:onClick="button1OnClick"/>
                    <Button 
                    android:id="@+id/button2"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button2"
                    android:onClick="button1OnClick2"/>

MainActivity2.java

public class Click extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main2.xml);
        final Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // open up screen 3?  
        }
    });
}

}

screen-1 和 screen-2 将共享同一组按钮。屏幕 3 将显示哪个顺序,以及按下了哪些按钮。

4

2 回答 2

2

您需要学习如何通过intents传递数据和参数。

看看这个:

如何在 Android 应用程序的活动之间传递数据?

于 2013-06-18T20:43:24.697 回答
0

当您开始下一个活动时,您在意图中传递该信息。

例如在你的protected void onCreate(Bundle savedInstanceState)你会做类似的事情:

Bundle extras = getIntent().getExtras();
if (extras != null) {
   // test for the arg you passed in and extract value eg
   String foostr = extras.getString(FOO_STRING);
   int fooint = extras.getInt(FOO_INT);
}

在开始活动的调用者中,

Intent intent = new Intent(this, Screen-2.class);
intent.putExtra(FOO_INT,myInt);
intent.putExtra(FOO_STRING,myString);
startActivity(intent);
于 2013-06-18T20:41:38.910 回答