试图让android studio编辑文本在用户输入正确答案时显示一条消息

时间:2016-02-16 15:27:16

标签: java android

package com.example.kp0938972.brainbuster;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class riddleAnswers extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "Test message";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_riddle_answers);
        String answer1 = "";

        EditText edit = (EditText) findViewById(R.id.Riddle1Answer);
        answer1 = String.valueOf(edit);
        if(answer1.equalsIgnoreCase("Your Eyes"))
        {
            findViewById(R.id.WrongAnswer1).setVisibility(View.INVISIBLE);
            findViewById(R.id.CorrectAnswer1).setVisibility(View.VISIBLE);

        }
        else
        {
            findViewById(R.id.CorrectAnswer1).setVisibility(View.INVISIBLE);
            findViewById(R.id.WrongAnswer1).setVisibility(View.VISIBLE);
        }

    }
}

一旦我尝试运行此代码,它会为您的眼睛编辑框显示null,如果我使用to string方法,则会导致我的应用程序崩溃。我想在另一个页面上显示一条消息,它根本不会这样做,只是显示错误的错误消息

1 个答案:

答案 0 :(得分:0)

您必须从编辑文本中获取文本。

edit.getText().toString();