当输入为milk
时,我正在尝试获取提交按钮以转到下一页。这是我到目前为止所尝试的内容
package com.example.ephraimcohen.prestwichlanguageschool;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class activitytwomilk2 extends AppCompatActivity {
private EditText Word;
private TextView TryAgain;
private Button Submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitytwomilk2);
Word = (EditText)findViewById(R.id.etword);
TryAgain = (TextView)findViewById(R.id.tvtryagian);
Submit = (Button) findViewById(R.id.btnsubmit);
TryAgain.setText(" ");
Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validate(Word.getText().toString());
}
});
}
private void validate(String userWord){
if((userWord.equals ("milk"))){
Intent intent = new Intent(activitytwomilk2.this, activitytwomilkcorrect.class);
startActivity(intent);
}else{
TryAgain.setText(" " + "Try again");
}
}`
在此之下,还有另一个按钮可以进入主屏幕。该按钮工作正常并带我到主页。
答案 0 :(得分:0)
尝试在您的情况下使用==而不是等于
if(userWord=="milk"){
Intent intent = new Intent(activitytwomilk2.this, activitytwomilkcorrect.class);
startActivity(intent);
}else{
TryAgain.setText(" " + "Try again");
}
}`