我正在創建一個測驗應用程序,它將泰盧固語與英語進行匹配,我的目標是讓十個問題顯示每個問題6秒,移動下一個問題。但我的應用程序等待整個60秒,並顯示A->整個屏幕。我的測驗應用程序沒有給出正確的結果並進入最終結果
代碼:
public class LetterQuiz extends Activity {
int rightAnswer=0,wrongAnswer=0;
TextView finalResult;
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" };
String telugu[] = { "ఎ", "బి", "సి", "డి", "ఇ", "ఎఫ్", "జి", "ఎచ్",
"ఐ", "జె", "కె", "ఎల్", "య్మ్", "యెన్", "వొ", "పి", "క్యు",
"ఆర్", "ఎస్", "టి", "యు", "వి", "డబల్యు", "ఎక్స్", "వై", "జెడ్" };
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");
top = (TextView) findViewById(R.id.telugu);
eval = (TextView) findViewById(R.id.eval);
answer = (EditText) findViewById(R.id.answer);
finalResult=(TextView)findViewById(R.id.finale);
finalResult.setVisibility(View.GONE);
quiz();
}
public void quiz() {
int wronganswers[]=new int[10];
int j=0;
for(int k=0;k<10;k++) {
int i;
number = rand.nextInt(25 - 0) + 0;
top.setTypeface(font);
top.setTextSize(40.f);
top.setText(telugu[number]);
String user = answer.getText().toString();
for (i = 0; i < english.length; i++) {
if (user.equalsIgnoreCase(english[i])) {
break;
}
}
if (i == number) {
eval.setText("Right Answer");
SystemClock.sleep(1000);
rightAnswer++;
}
else {
eval.setText("Wrong Answer");
SystemClock.sleep(1000);
wrongAnswer++;
wronganswers[j]=number;
j++;
}
SystemClock.sleep(6000);
}
finalResult.setText("No of right answers"+rightAnswer+"\nNo of wrong answers"+wrongAnswer+"\nletters to review");
String review="";
for(int i=0;i<wronganswers.length;i++)
review=english[wronganswers[i]]+"->"+telugu[wronganswers[i]]+"\n";
finalResult.setText(review);
top.setVisibility(View.GONE);
eval.setVisibility(View.GONE);
answer.setVisibility(View.GONE);
finalResult.setVisibility(View.VISIBLE);
}
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>
<TextView
android:id="@+id/eval"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignRight="@+id/telugu"
android:layout_below="@+id/answer"
android:layout_marginRight="20dp"
android:layout_marginTop="54dp"
android:text="TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/finale" />
</RelativeLayout>
聽起來像你應該調試你的程序。通過它,或只是打印一些東西。 – keyser
不要通過調用'SystemClock.sleep(6000)來凍結你的UI;'看看AsyncTask – donfuxx
除了因'SystemClock.sleep()'導致的UI凍結外,你的答案的邏輯有一些問題檢查。你爲什麼把'i'與隨機生成的'number'進行比較?這不會告訴你他們的答案是否正確。 – CheeseWarlock