如何在完成第一级后自动进入游戏的下一级?

时间:2015-08-30 09:56:44

标签: java android

我正在尝试用水平开发一个谜语游戏。第一级在“OneActivity.java”和“activity_one.xml”第二级“TwoActivity.java”和“activity_two.xml”等等。用户在第一级输入问题答案后,如果答案正确则显示“正确”的吐司显示,如果不正确则显示“错误”。现在,如果答案正确但如何保持在同一级别,直到用户输入正确的答案,我该如何自动进入下一级别。这是我的OneActivity.java和activity_one.xml

OneActivity.java:

package com.golo.user.gaunkhanekatha;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class OneActivity extends Activity {

    public Button check;
    public EditText typeh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        check = (Button)findViewById(R.id.check); //R.id.button is the id on your xml
        typeh = (EditText)findViewById(R.id.typeh); //this is the EditText id
        check.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                //Here you must get the text on your EditText
                String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
                //You can make an if statement to check if it's correct or not
                if(Answer.equals("4") || (Answer.equals("four")))
                {
                    //Create a Toast because it's correct
                    Toast.makeText(OneActivity.this, "Correct!",
                            Toast.LENGTH_LONG).show();
                }
                else{
                    //It's not the correct answer
                    Toast.makeText(OneActivity.this, "Wrong! Try Again",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_aboutus, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_one.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:weightSum="1"
        android:textAlignment="center"
        android:id="@+id/oneque">

        <TextView
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:text="What is 2+2?"
            android:id="@+id/que"
            android:width="255dp"
            android:textSize="30dp"
            android:layout_margin="50dp"
            android:textStyle="italic"
            android:gravity="center" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/typeh"
            android:layout_gravity="center_horizontal"
            android:text="Type Here" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Check Answer"
            android:id="@+id/check"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>

activity_level.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dp"
            android:layout_weight="1"
            android:onClick="moveToOneActivity"
            android:background="@drawable/one" />

        <Button
            android:id="@+id/button2"

            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToTwoActivity"
            android:background="@drawable/two" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToThreeActivity"
            android:background="@drawable/three" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dp"
            android:layout_weight="1"
            android:onClick="moveToFourActivity"
            android:background="@drawable/four" />

        <Button
            android:id="@+id/button5"

            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToFiveActivity"
            android:background="@drawable/five" />

        <Button
            android:id="@+id/button6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToSixActivity"
            android:background="@drawable/six" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dp"
            android:layout_weight="1"
            android:onClick="moveToSevenActivity"
            android:background="@drawable/seven" />

        <Button
            android:id="@+id/button8"

            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToEightActivity"
            android:background="@drawable/eight" />

        <Button
            android:id="@+id/button9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToNineActivity"
            android:background="@drawable/nine" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button10"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dp"
            android:layout_weight="1"
            android:onClick="moveToTenActivity"
            android:background="@drawable/ten" />

        <Button
            android:id="@+id/button11"

            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToElevenActivity"
            android:background="@drawable/eleven" />

        <Button
            android:id="@+id/button12"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="30dip"
            android:layout_weight="1"
            android:onClick="moveToTwelveActivity"
            android:background="@drawable/twelve" />
    </LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:2)

   if(Answer.equals("4") || (Answer.equals("four")))
   {
         //Create a Toast because it's correct
           Toast.makeText(OneActivity.this, "Correct!",
                        Toast.LENGTH_LONG).show();

        //Here create intent to the new activity - for example
        //If you wish the user will have time to see the toast, you can use a Handler with post delayed

        new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
               Intent newLevel= new Intent(OneActivity.this, TwoActivity.class);
               startActivity(newLevel);
               finish(); //finish the activity of the current level
            }
        }, 3000); //the code inside run() will be executed after 3 seconds so the user can see the toast


   }
   else
   {
         //It's not the correct answer
         Toast.makeText(OneActivity.this, "Wrong! Try Again",
                        Toast.LENGTH_LONG).show();
   }

答案 1 :(得分:1)

您可以使用意图切换到另一项活动。

Intent myIntent = new Intent(this, MyActivity.class);
startActivity(myIntent);

你可以简单地使用if语句。如果答案是正确的,请创建一个意图并开始活动二,否则重新启动活动一,或者如果答案错误,则执行您想要做的其他行为。

答案 2 :(得分:1)

要转到第二级,您应该将Intent添加到正确的Toast内,如下所示:

Intent i = new Intent(OneActivity.this, TwoActivity.class);
startActivity(i);
finish();

这应该是你的代码:

check.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            //Here you must get the text on your EditText
            String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
            //You can make an if statement to check if it's correct or not
            if(Answer.equals("4") || (Answer.equals("four")))
            {
                //Create a Toast because it's correct
                Toast.makeText(OneActivity.this, "Correct! Going to Level 2...",
                        Toast.LENGTH_LONG).show();
                Intent i = new Intent(OneActivity.this, TwoActivity.class);
                startActivity(i);
                finish();
            }
            else{
                //It's not the correct answer
                Toast.makeText(OneActivity.this, "Wrong! Try Again",
                        Toast.LENGTH_LONG).show();
            }
        }
    });

修改

当您使用Toast时,此修改将取消TwoActivity.class,您必须更改某些内容,我会向您展示。

1.-创建一个全局Toast变量。

private Toast toast;

2.-在onCreate()上对此进行初始化:

toast = Toast.makeText(OneActivity.this, "", Toast.LENGTH_SHORT);

3.-然后将您的两条Toast消息更改为:

//Correct Toast
toast.setText("Correct! Going to Level 2...");
toast.show();

//Incorrect Toast
toast.setText("Wrong! Try Again");
toast.show();

4.-您想制作finish()以避免返回按钮返回OneActivity(),因此您将调用它,并调用onDestroy(),因此您必须添加此方法以及取消Toast

@Override
protected void onDestroy() {
    super.onDestroy();
    if(toast!= null) {
        toast.cancel();
    }
}