如果只满足特殊条件,我该怎么做才能转移到另一个活动

时间:2017-04-12 07:34:29

标签: java android xml

我正在制作一个测验应用。我希望在最后一个问题被回答后按下一个按钮时显示退出页面。我有一个activity_questions.xml和一个activity_exit.xmlactivity_questions.xml的xml是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.crystal.questions">

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="29dp"
        android:fontFamily="sans-serif-thin"
        android:padding="10dp"
        android:text=""
        android:textColor="#440027"
        android:textSize="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text3"
        android:layout_margin="30dp"
        android:background="#9775AA"
        android:hint="Answer"
        android:inputType="text"
        android:padding="10dp"
        android:textSize="20dip" />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit2"
        android:gravity="center"

        android:orientation="horizontal">

        <Button
            android:id="@+id/answer"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:onClick="onAnswerClick"
            android:padding="2dp"
            android:text="Okay" />

        <Button
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:onClick="onHintClick"
            android:padding="2dp"
            android:text="Hint" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:orientation="vertical"
        android:layout_marginTop="26dp"
        android:layout_below="@+id/ll"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:id="@+id/tickcross"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@+id/ll"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:src="@drawable/wierdtick" />

        <TextView
            android:id="@+id/correctornot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tickcross"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="Correct!"
            android:textColor="#440027"
            android:textSize="30dp" />

        <Button
            android:id="@+id/nextbutton"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Next"
            android:onClick="onNextClick"
            />
    </LinearLayout>
</RelativeLayout>

java是:

package com.example.android.crystal;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class questions extends AppCompatActivity {
    Random r = new Random();
    private boolean done;
    private int QuestionNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
        findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
        findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
        String[] questions = getResources().getStringArray(R.array.Questions);
        TextView t = (TextView) findViewById(R.id.text3);
        t.setText(questions[QuestionNo]);
    }
    public void onFinishClick(View view){
        Intent intent = new Intent(this, exit.class);
        startActivity(intent);
    }
    public static void hideKeyboardFrom(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
    public void answersubmitted() {
        findViewById(R.id.tickcross).setVisibility(View.VISIBLE);

        TranslateAnimation animation = new TranslateAnimation(0, 0, 2000, 0);
        animation.setDuration(1000);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        findViewById(R.id.tickcross).startAnimation(animation);
        findViewById(R.id.correctornot).setVisibility(View.VISIBLE);
    findViewById(R.id.nextbutton).setVisibility(View.VISIBLE);

        }

public void onHintClick(View view) {
        String[] hints = getResources().getStringArray(R.array.Hints);
        Toast toasty = Toast.makeText(getApplicationContext(), hints[QuestionNo], Toast.LENGTH_SHORT);
        toasty.show();
        }

public void onAnswerClick(View view) {
        if (done == false) {
        hideKeyboardFrom(this);
        String answer = ((EditText) findViewById(R.id.edit2)).getText().toString();
        String[] answers = getResources().getStringArray(R.array.Answers);
        String correctAnswer = answers[QuestionNo];
        correctAnswer = correctAnswer.toUpperCase();
        answer = answer.toUpperCase();
        if (answer.equals(correctAnswer)) {
        TextView t = (TextView) findViewById(R.id.correctornot);
        t.setText("CORRECT!");
        ImageView i = (ImageView) findViewById(R.id.tickcross);
        i.setImageDrawable(getDrawable(R.drawable.wierdtick));
        answersubmitted();
        } else {
        TextView t = (TextView) findViewById(R.id.correctornot);
        t.setText("Correct Answer: " + correctAnswer);
        ImageView i = (ImageView) findViewById(R.id.tickcross);
        i.setImageDrawable(getDrawable(R.drawable.weirdcross));

        answersubmitted();
        }
        done = true;
        }

        }
public void onNextClick(View view){
        if (done){
        String[] questions = getResources().getStringArray(R.array.Questions);
        if (QuestionNo < (questions.length - 1)) {
        QuestionNo += 1;
        TextView t = (TextView) findViewById(R.id.text3);
        t.setText(questions[QuestionNo]);

        findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
        findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
        findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
        EditText et = (EditText) findViewById(R.id.edit2);
        et.setText("");




        done = false;
        }
        else {
        onFinishClick();
        }

        }

        }
        }

但是onFinishClick()会出错 在此先感谢您的帮助

2 个答案:

答案 0 :(得分:1)

如果只满足特殊条件,我该怎么做才能转移到另一项活动

基本上是if语句和Intent。

未添加变量的原始代码:

if(condition(s)){
    Intent i = new Intent(this, TargetActivity.class);
    //Put arguments into the intent if you need them...
    startActivity(i);
}

您可以将它放在onClickListener中的一个线程中,任何地方以及您想要执行代码的任何方式。

关于您收到的错误,在您从Logcat提供堆栈跟踪之前,我无法帮助您解决此问题

答案 1 :(得分:0)

你可以这样做:

mNextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mQuestionCount < questionList.size()){
                // set next question
            } else {
                // call finish
            }
        }
    });