1

我正在创建一个测验应用程序,它将泰卢固语与英语进行匹配,我的目标是有十个问题,每个问题将显示 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>
4

2 回答 2

0

问题是,UIThread 在 quiz() 方法中被冻结,并且永远不会改变用户的视图。

quiz()在不同的线程中调用该方法。例如

@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);

    Thread t=new Thread(){
       @Override
       public void run() {
         quiz();
       } 
    }

    t.start();
}

并在该quiz()方法中,调用runOnUiThread每个更改视图的语句。例如

runOnUiThread(new Runnable(){
public void run() {
       top.setTypeface(font);
        top.setTextSize(40.f);
        top.setText(telugu[number]);
    }

});

于 2014-02-21T23:12:55.573 回答
-1

您只是一遍又一遍地循环和阅读相同的文本字段。当您的代码执行时,该文本字段永远不会改变。

于 2014-02-21T22:53:21.723 回答