测验应用与字符串不匹配

时间:2014-02-21 20:46:32

标签: java android string textview android-edittext

我正在尝试将输入英语的字符串与等效的泰卢固语相匹配,但它总是说错误的答案 我的代码是

package vamsi.saran.koli.deepti;
import java.util.Random;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class LetterQuiz extends Activity {

TextView top, eval;
Typeface font;
EditText answer;
String english[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
        "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
        "Y", "Z" };
int number;
static Random rand = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_letter_quiz);
    font = Typeface.createFromAsset(getAssets(),
            "fonts/NotoSansTelugu-Bold.ttf");

    String telugu[] = { "ఎ", "బి", "సి", "డి", "ఇ", "ఎఫ్", "జి", "ఎచ్",
            "ఐ", "జె", "కె", "ఎల్", "య్మ్", "యెన్", "వొ", "పి", "క్యు",
            "ఆర్", "ఎస్", "టి", "యు", "వి", "డబల్యు", "ఎక్స్", "వై", "జెడ్" };

    number = rand.nextInt(25 - 0) + 0;
    top = (TextView) findViewById(R.id.telugu);
    top.setTypeface(font);
    top.setTextSize(40.f);
    top.setText(telugu[number]);
    eval = (TextView) findViewById(R.id.eval);
    answer = (EditText) findViewById(R.id.answer);

}

public void submit(View v) {
    int i;

    String user = answer.toString();
    for (i = 0; i < english.length; i++) {
        if (user.equals(english[i])) {
            break;
        }
    }
    if (i == number) {
        eval.setText("Right Answer");
    } else {
        eval.setText("Wrong Answer");
    }

}

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

}

xml是

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LetterQuiz" >

<TextView
    android:id="@+id/telugu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="100dp"
    android:layout_marginTop="19dp"
    android:text="TextView" />

<EditText
    android:id="@+id/answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/telugu"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp"
    android:ems="10"
     >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/answer"
    android:layout_below="@+id/answer"
    android:layout_marginTop="21dp"
    android:onClick="submit"
    android:text="submit" />

<TextView
    android:id="@+id/eval"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/answer"
    android:layout_below="@+id/button1"
    android:layout_marginTop="38dp"
    android:layout_toLeftOf="@+id/button1"
    android:text="TextView" 
    />

2 个答案:

答案 0 :(得分:1)

考虑到活动的功能,它应该是这样的:

public void submit(EditView v) {

    String user = v.getText().toString();
    if (english[number].equals(user)) {
        eval.setText("Right Answer");
    } else {
        eval.setText("Wrong Answer");
    }

}

答案 1 :(得分:1)

替换

String user = answer.toString();    

String user = answer.getText().toString();
Log.d("LetterQuiz", user);

您正在将EditText实例转换为字符串。而是获取它的文本值并进行比较。