2

我有这个活动,有问题和 4 个可能的答案,在每个用户回答后,它会重新加载另一个问题。我在右下角放置了一个 textView,起始值为1/10( rezultat.setText(counter+"/10");)。我想在每个问题之后将该值增加到 2/10、3/10 等等,但我不知道在哪里放置 counter++;在我的代码中,我到处尝试。这是我的代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class Kviz extends Activity implements View.OnClickListener{

    Runnable mLaunchTask = new Runnable() {
        public void run() {
            startActivity(getIntent());
        }
     };
     Runnable mLaunchTaskFinish = new Runnable() {
         public void run() {
            finish();
         }
      };

    Button bIzlazIzKviza, bOdgovor1, bOdgovor2, bOdgovor3, bOdgovor4;
    TextView question, proba, rezultat;
    int counter = 1;

    private class Answer {
        public Answer(String opt, boolean correct) {
            option = opt;
            isCorrect = correct;
        }

        String option;
        boolean isCorrect;
    }
    Handler mHandler = new Handler();

    final OnClickListener clickListener = new OnClickListener() {
        public void onClick(View v) {

            Answer ans = (Answer) v.getTag();
            if (ans.isCorrect) {
                mHandler.postDelayed(mLaunchTaskFinish, 1200);
                mHandler.postDelayed(mLaunchTask,1000);
                Intent i = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
                startActivity(i);

            }else{
                mHandler.postDelayed(mLaunchTaskFinish, 2200);
                mHandler.postDelayed(mLaunchTask,2000);
                Intent i = new Intent("rs.androidaplikacijekvizopstekulture.POGRESANODGOVOR");
                startActivity(i);
                }
            }
     };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);   //full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.kviz);

        Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
        Typeface pitanje = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
        bIzlazIzKviza = (Button) findViewById(R.id.bIzlazIzKviza);
        rezultat = (TextView) findViewById(R.id.tvBrojPitanja);
        question = (TextView) findViewById(R.id.tvPitanje);
        bOdgovor1 = (Button) findViewById(R.id.bOdgovor1);
        bOdgovor2 = (Button) findViewById(R.id.bOdgovor2);
        bOdgovor3 = (Button) findViewById(R.id.bOdgovor3);
        bOdgovor4 = (Button) findViewById(R.id.bOdgovor4);
        bOdgovor1.setTypeface(dugmad);
        bOdgovor2.setTypeface(dugmad);
        bOdgovor3.setTypeface(dugmad);
        bOdgovor4.setTypeface(dugmad);
        bIzlazIzKviza.setTypeface(dugmad);
        rezultat.setTypeface(dugmad);
        question.setTypeface(pitanje);

        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();


        try{    //Pokusava da otvori db
        mDbHelper.open();  //baza otvorena

        Cursor c = mDbHelper.getTestData();

        question.setText(c.getString(1));

        List<Answer> labels = new ArrayList<Answer>();

        labels.add(new Answer(c.getString(2), true));
        labels.add(new Answer(c.getString(3), false));
        labels.add(new Answer(c.getString(4), false));
        labels.add(new Answer(c.getString(5), false));

        Collections.shuffle(labels);

        bOdgovor1.setText(labels.get(0).option);
        bOdgovor1.setTag(labels.get(0));
        bOdgovor1.setOnClickListener(clickListener);

        bOdgovor2.setText(labels.get(1).option);
        bOdgovor2.setTag(labels.get(1));
        bOdgovor2.setOnClickListener(clickListener);

        bOdgovor3.setText(labels.get(2).option);
        bOdgovor3.setTag(labels.get(2));
        bOdgovor3.setOnClickListener(clickListener);

        bOdgovor4.setText(labels.get(3).option);
        bOdgovor4.setTag(labels.get(3));
        bOdgovor4.setOnClickListener(clickListener);

        rezultat.setText(counter+"/10");
        }

        finally{    // kada zavrsi sa koriscenjem baze podataka, zatvara db
            mDbHelper.close();
        }

        bIzlazIzKviza.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                finish();   
            }
        });
    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void onClick(View v) {

    }
}
4

3 回答 3

1

“每个用户回答它都会重新加载另一个问题”是什么意思。? 您是否对差异问题使用不同的活动?
您可以对所有问题使用相同的活动,当用户按下任何答案按钮时,只需调用一个方法

public void nextQuestion() {
    counter++;
    question.setText("");
    bOdgovor1.setText("");
    bOdgovor2.setText("");
    bOdgovor3.setText("");
    bOdgovor4.setText("");
    //reset your next question and all four options here
    rezultat.setText(counter + "/10");
}
于 2013-03-02T09:04:32.963 回答
0

从这里很难测试,但看起来像是counter++在行前添加

Answer ans = (Answer) v.getTag();

应该可以正常工作,因为counter只要单击似乎负责回答问题的按钮,它就会增加。请记住,您还需要致电

rezultat.setText(counter+"/10");

再次,更新文本以反映counter.

于 2013-03-02T02:45:26.537 回答
0

稍微扩展我的问题。我确实设法用计数器类计算问题,它达到 2/10,但因为我在每个问题后重新加载我的活动,我的 textView 也重新加载到 1/10。我认为这是我的问题的主要原因。

于 2013-03-02T15:54:54.907 回答