Java 第一关完成后,如何在游戏中自动进入下一关?

Java 第一关完成后,如何在游戏中自动进入下一关?,java,android,Java,Android,我正在尝试开发一个有关卡的谜语游戏。第一级是“OneActivity.java”和“activity_one.xml”,第二级是“TwoActivity.java”和“activity_two.xml”等等。在用户键入第一级问题的答案后,如果答案正确,将显示带有“Correct”的toast显示屏,如果答案不正确,将显示带有“error”的toast显示屏。现在,如果答案正确,我如何自动进入下一个级别,但在用户输入正确答案之前保持在同一级别。这是我的OneActivity.java和activi

我正在尝试开发一个有关卡的谜语游戏。第一级是“OneActivity.java”和“activity_one.xml”,第二级是“TwoActivity.java”和“activity_two.xml”等等。在用户键入第一级问题的答案后,如果答案正确,将显示带有“Correct”的toast显示屏,如果答案不正确,将显示带有“error”的toast显示屏。现在,如果答案正确,我如何自动进入下一个级别,但在用户输入正确答案之前保持在同一级别。这是我的OneActivity.java和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>
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>

activity_level.xml:

您可以使用意图切换到其他活动

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

您可以简单地使用if语句。如果答案正确,创建意图并启动活动2,否则在活动1中重新启动,或者在答案错误的情况下执行任何其他行为。

您可以使用意图切换到其他活动

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

您可以简单地使用if语句。如果答案正确,创建一个意图并开始活动2,否则在活动1中重新启动,或者在答案错误的情况下执行任何其他行为。

要进入第二级,您应该在正确的
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();
            }
        }
    });
编辑 当您在进行
two活动时,此编辑将取消
Toast
。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();
    }
}

要进入第二个级别,您应该在正确的
Toast
中添加此
Intent
,如下所示:

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();
            }
        }
    });
编辑 当您在进行
two活动时,此编辑将取消
Toast
。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();
    }
}


每个级别都有单独的活动?是的,每个级别都有单独的活动。打算参加另一个活动相同的答案是正确的,如果错误
提交时什么都不做。。您检查了我的答案了吗?你不需要做任何其他事情,只需要两行code@Bik在继续处理之前,您可能应该检查getText()是否不返回null。每个级别都在单独的活动中?是,每个级别都是独立的活动。对于另一个活动,相同的答案是正确的,如果错误,提交时什么也不做。。您检查了我的答案了吗?你不需要做任何其他事情,只需要两行code@Bik在继续处理之前,您可能应该检查getText()是否不返回null。谢谢您的回复。它在“new Handler()”中显示了1个错误,表示“'Handler'是抽象的;无法实例化”您可能使用了错误的导入:import java.util.logging.Handler;将其更改为导入android.os.Handler;它起作用了,但当我按下后退按钮时,它又回到了第一级的问题。如何进入“标高选择”窗口。级别选择窗口有一个不同的activity_level.xml。我已经在问题中添加了xml,请看一看,因为我在90分钟内无法发布另一个问题,我需要快速的帮助!只需在活动中调用finish(),我已编辑了我的回答完美!非常感谢!:)谢谢你的回复。它在“new Handler()”中显示了1个错误,表示“'Handler'是抽象的;无法实例化”您可能使用了错误的导入:import java.util.logging.Handler;将其更改为导入android.os.Handler;它起作用了,但当我按下后退按钮时,它又回到了第一级的问题。如何进入“标高选择”窗口。级别选择窗口有一个不同的activity_level.xml。我已经在问题中添加了xml,请看一看,因为我在90分钟内无法发布另一个问题,我需要快速的帮助!只需在活动中调用finish(),我已编辑了我的回答完美!非常感谢!:)是的,你的方式似乎更快了,但我一直看到前面的祝酒词“正确”,即使我用另一个问题进入新的层次。有没有办法缩短toast在屏幕上显示的时间?只需将toast更改为:
toast.makeText(OneActivity.this,“正确!进入2级…”,toast.LENGTH_SHORT.show()它不是更快,只是没有使用处理程序,所以您可以立即导航到下一个活动。处理程序的目的是避免你现在所经历的一切,祝你好运。我已经编辑了我的答案@请随意使用您想要的代码,我们刚刚给您提供了两种不同的方法,所有这两种方法都可以:D所以请随意将任何asnwer标记为正确的!:)非常感谢。就像一个符咒…又是一个小问题。当用户返回到第一级时,他/她必须再次键入答案,即使他/她以前已经解决了这个问题。JAVA中是否有将答案保存在类型面板中的方法(如果用户已经解决了问题)